From 464f4813e39449811f02f1672cc44d927649d470 Mon Sep 17 00:00:00 2001 From: John Keiser Date: Thu, 16 Jul 2020 12:56:02 -0700 Subject: [PATCH] Define namespaces inside generic files --- src/arm64/dom_parser_implementation.cpp | 66 +++---- src/arm64/dom_parser_implementation.h | 9 - src/arm64/numberparsing.h | 6 +- src/arm64/stringparsing.h | 4 +- src/fallback/dom_parser_implementation.cpp | 13 +- src/fallback/dom_parser_implementation.h | 9 - src/fallback/numberparsing.h | 12 +- src/fallback/stringparsing.h | 4 +- src/generic/dom_parser_implementation.h | 15 ++ src/generic/stage1/allocate.h | 4 + src/generic/stage1/buf_block_reader.h | 6 + src/generic/stage1/find_next_document_index.h | 6 + src/generic/stage1/json_minifier.h | 4 + src/generic/stage1/json_scanner.h | 4 + src/generic/stage1/json_string_scanner.h | 6 +- src/generic/stage1/json_structural_indexer.h | 4 + .../stage1/utf8_fastvalidate_algorithm.h | 6 + src/generic/stage1/utf8_lookup2_algorithm.h | 6 + src/generic/stage1/utf8_lookup3_algorithm.h | 6 + src/generic/stage1/utf8_lookup4_algorithm.h | 10 +- src/generic/stage1/utf8_lookup_algorithm.h | 6 + src/generic/stage1/utf8_range_algorithm.h | 6 + src/generic/stage1/utf8_validator.h | 6 +- src/generic/stage1/utf8_zwegner_algorithm.h | 5 + src/generic/stage2/allocate.h | 4 + src/generic/stage2/atomparsing.h | 4 + src/generic/stage2/logger.h | 5 +- src/generic/stage2/numberparsing.h | 4 + .../stage2/streaming_structural_parser.h | 168 ------------------ src/generic/stage2/stringparsing.h | 6 +- src/generic/stage2/structural_iterator.h | 4 + src/generic/stage2/structural_parser.h | 12 +- src/generic/stage2/tape_writer.h | 8 + src/haswell/dom_parser_implementation.cpp | 61 ++++--- src/haswell/dom_parser_implementation.h | 9 - src/haswell/numberparsing.h | 6 +- src/haswell/stringparsing.h | 4 +- src/westmere/dom_parser_implementation.cpp | 61 ++++--- src/westmere/dom_parser_implementation.h | 9 - src/westmere/numberparsing.h | 6 +- src/westmere/stringparsing.h | 4 +- 41 files changed, 266 insertions(+), 332 deletions(-) delete mode 100755 src/generic/stage2/streaming_structural_parser.h diff --git a/src/arm64/dom_parser_implementation.cpp b/src/arm64/dom_parser_implementation.cpp index e7debac2d..448ffc5ee 100644 --- a/src/arm64/dom_parser_implementation.cpp +++ b/src/arm64/dom_parser_implementation.cpp @@ -101,39 +101,16 @@ really_inline simd8 must_be_2_3_continuation(const simd8 prev2, c return is_third_byte ^ is_fourth_byte; } +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson + #include "generic/stage1/buf_block_reader.h" #include "generic/stage1/json_string_scanner.h" #include "generic/stage1/json_scanner.h" - -namespace stage1 { -really_inline uint64_t json_string_scanner::find_escaped(uint64_t backslash) { - // On ARM, we don't short-circuit this if there are no backslashes, because the branch gives us no - // benefit and therefore makes things worse. - // if (!backslash) { uint64_t escaped = prev_escaped; prev_escaped = 0; return escaped; } - return find_escaped_branchless(backslash); -} -} - #include "generic/stage1/json_minifier.h" -WARN_UNUSED error_code implementation::minify(const uint8_t *buf, size_t len, uint8_t *dst, size_t &dst_len) const noexcept { - return arm64::stage1::json_minifier::minify<64>(buf, len, dst, dst_len); -} - #include "generic/stage1/find_next_document_index.h" #include "generic/stage1/utf8_lookup4_algorithm.h" #include "generic/stage1/json_structural_indexer.h" -WARN_UNUSED error_code dom_parser_implementation::stage1(const uint8_t *_buf, size_t _len, bool streaming) noexcept { - this->buf = _buf; - this->len = _len; - return arm64::stage1::json_structural_indexer::index<64>(buf, len, *this, streaming); -} -#include "generic/stage1/utf8_validator.h" -WARN_UNUSED bool implementation::validate_utf8(const char *buf, size_t len) const noexcept { - return simdjson::arm64::stage1::generic_validate_utf8(buf,len); -} - -} // namespace SIMDJSON_IMPLEMENTATION -} // namespace simdjson // // Stage 2 @@ -141,15 +118,44 @@ WARN_UNUSED bool implementation::validate_utf8(const char *buf, size_t len) cons #include "arm64/stringparsing.h" #include "arm64/numberparsing.h" - -namespace simdjson { -namespace SIMDJSON_IMPLEMENTATION { - #include "generic/stage2/logger.h" #include "generic/stage2/atomparsing.h" #include "generic/stage2/structural_iterator.h" #include "generic/stage2/structural_parser.h" +// +// Implementation-specific overrides +// +namespace simdjson { +namespace SIMDJSON_IMPLEMENTATION { + +namespace stage1 { + +really_inline uint64_t json_string_scanner::find_escaped(uint64_t backslash) { + // On ARM, we don't short-circuit this if there are no backslashes, because the branch gives us no + // benefit and therefore makes things worse. + // if (!backslash) { uint64_t escaped = prev_escaped; prev_escaped = 0; return escaped; } + return find_escaped_branchless(backslash); +} + +} // namespace stage1 + +WARN_UNUSED error_code implementation::minify(const uint8_t *buf, size_t len, uint8_t *dst, size_t &dst_len) const noexcept { + return arm64::stage1::json_minifier::minify<64>(buf, len, dst, dst_len); +} + +WARN_UNUSED error_code dom_parser_implementation::stage1(const uint8_t *_buf, size_t _len, bool streaming) noexcept { + this->buf = _buf; + this->len = _len; + return arm64::stage1::json_structural_indexer::index<64>(buf, len, *this, streaming); +} + +#include "generic/stage1/utf8_validator.h" + +WARN_UNUSED bool implementation::validate_utf8(const char *buf, size_t len) const noexcept { + return simdjson::arm64::stage1::generic_validate_utf8(buf,len); +} + WARN_UNUSED error_code dom_parser_implementation::parse(const uint8_t *_buf, size_t _len, dom::document &_doc) noexcept { error_code err = stage1(_buf, _len, false); if (err) { return err; } diff --git a/src/arm64/dom_parser_implementation.h b/src/arm64/dom_parser_implementation.h index 29174f80a..888b34225 100644 --- a/src/arm64/dom_parser_implementation.h +++ b/src/arm64/dom_parser_implementation.h @@ -1,15 +1,6 @@ #ifndef SIMDJSON_ARM64_DOM_PARSER_IMPLEMENTATION_H #define SIMDJSON_ARM64_DOM_PARSER_IMPLEMENTATION_H -#include "simdjson.h" -#include "isadetection.h" - -namespace simdjson { -namespace arm64 { - #include "generic/dom_parser_implementation.h" -} // namespace arm64 -} // namespace simdjson - #endif // SIMDJSON_ARM64_DOM_PARSER_IMPLEMENTATION_H \ No newline at end of file diff --git a/src/arm64/numberparsing.h b/src/arm64/numberparsing.h index 0854b469a..758a350f7 100644 --- a/src/arm64/numberparsing.h +++ b/src/arm64/numberparsing.h @@ -28,11 +28,11 @@ static really_inline uint32_t parse_eight_digits_unrolled(const uint8_t *chars) return uint32_t((val & 0x0000FFFF0000FFFF) * 42949672960001 >> 32); } +} // namespace arm64 +} // namespace simdjson + #define SWAR_NUMBER_PARSING #include "generic/stage2/numberparsing.h" -} // namespace arm64 -} // namespace simdjson - #endif // SIMDJSON_ARM64_NUMBERPARSING_H diff --git a/src/arm64/stringparsing.h b/src/arm64/stringparsing.h index 98decef22..39a1af128 100644 --- a/src/arm64/stringparsing.h +++ b/src/arm64/stringparsing.h @@ -44,9 +44,9 @@ really_inline backslash_and_quote backslash_and_quote::copy_and_find(const uint8 }; } -#include "generic/stage2/stringparsing.h" - } // namespace arm64 } // namespace simdjson +#include "generic/stage2/stringparsing.h" + #endif // SIMDJSON_ARM64_STRINGPARSING_H diff --git a/src/fallback/dom_parser_implementation.cpp b/src/fallback/dom_parser_implementation.cpp index 44e98b4e5..444ef69ec 100644 --- a/src/fallback/dom_parser_implementation.cpp +++ b/src/fallback/dom_parser_implementation.cpp @@ -4,13 +4,12 @@ // // Stage 1 // +#include "generic/stage1/find_next_document_index.h" + namespace simdjson { namespace SIMDJSON_IMPLEMENTATION { - namespace stage1 { -#include "generic/stage1/find_next_document_index.h" - class structural_scanner { public: @@ -180,7 +179,6 @@ private: } // namespace stage1 - WARN_UNUSED error_code dom_parser_implementation::stage1(const uint8_t *_buf, size_t _len, bool partial) noexcept { this->buf = _buf; this->len = _len; @@ -316,15 +314,14 @@ WARN_UNUSED bool implementation::validate_utf8(const char *buf, size_t len) cons // #include "fallback/stringparsing.h" #include "fallback/numberparsing.h" - -namespace simdjson { -namespace SIMDJSON_IMPLEMENTATION { - #include "generic/stage2/logger.h" #include "generic/stage2/atomparsing.h" #include "generic/stage2/structural_iterator.h" #include "generic/stage2/structural_parser.h" +namespace simdjson { +namespace SIMDJSON_IMPLEMENTATION { + WARN_UNUSED error_code dom_parser_implementation::parse(const uint8_t *_buf, size_t _len, dom::document &_doc) noexcept { error_code err = stage1(_buf, _len, false); if (err) { return err; } diff --git a/src/fallback/dom_parser_implementation.h b/src/fallback/dom_parser_implementation.h index b648c36cd..d7c83e073 100644 --- a/src/fallback/dom_parser_implementation.h +++ b/src/fallback/dom_parser_implementation.h @@ -1,15 +1,6 @@ #ifndef SIMDJSON_FALLBACK_DOM_PARSER_IMPLEMENTATION_H #define SIMDJSON_FALLBACK_DOM_PARSER_IMPLEMENTATION_H -#include "simdjson.h" -#include "isadetection.h" - -namespace simdjson { -namespace fallback { - #include "generic/dom_parser_implementation.h" -} // namespace fallback -} // namespace simdjson - #endif // SIMDJSON_FALLBACK_DOM_PARSER_IMPLEMENTATION_H \ No newline at end of file diff --git a/src/fallback/numberparsing.h b/src/fallback/numberparsing.h index 757606558..36586a212 100644 --- a/src/fallback/numberparsing.h +++ b/src/fallback/numberparsing.h @@ -15,7 +15,7 @@ void found_float(double result, const uint8_t *buf); #endif namespace simdjson { -namespace fallback { +namespace SIMDJSON_IMPLEMENTATION { static really_inline uint32_t parse_eight_digits_unrolled(const char *chars) { uint32_t result = 0; for (int i=0;i<8;i++) { @@ -27,12 +27,10 @@ static really_inline uint32_t parse_eight_digits_unrolled(const uint8_t *chars) return parse_eight_digits_unrolled((const char *)chars); } -#define SWAR_NUMBER_PARSING - -#include "generic/stage2/numberparsing.h" - -} // namespace fallback - +} // namespace SIMDJSON_IMPLEMENTATION } // namespace simdjson +#define SWAR_NUMBER_PARSING +#include "generic/stage2/numberparsing.h" + #endif // SIMDJSON_FALLBACK_NUMBERPARSING_H diff --git a/src/fallback/stringparsing.h b/src/fallback/stringparsing.h index 599dc8ec1..0501ae5a1 100644 --- a/src/fallback/stringparsing.h +++ b/src/fallback/stringparsing.h @@ -27,9 +27,9 @@ really_inline backslash_and_quote backslash_and_quote::copy_and_find(const uint8 return { src[0] }; } -#include "generic/stage2/stringparsing.h" - } // namespace fallback } // namespace simdjson +#include "generic/stage2/stringparsing.h" + #endif // SIMDJSON_FALLBACK_STRINGPARSING_H diff --git a/src/generic/dom_parser_implementation.h b/src/generic/dom_parser_implementation.h index ca45b98ec..ee59694b2 100644 --- a/src/generic/dom_parser_implementation.h +++ b/src/generic/dom_parser_implementation.h @@ -1,3 +1,9 @@ +#include "simdjson.h" +#include "isadetection.h" + +namespace simdjson { +namespace SIMDJSON_IMPLEMENTATION { + // expectation: sizeof(scope_descriptor) = 64/8. struct scope_descriptor { uint32_t tape_index; // where, on the tape, does the scope ([,{) begins @@ -38,9 +44,15 @@ public: WARN_UNUSED error_code set_max_depth(size_t max_depth) noexcept final; }; +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson + #include "generic/stage1/allocate.h" #include "generic/stage2/allocate.h" +namespace simdjson { +namespace SIMDJSON_IMPLEMENTATION { + really_inline dom_parser_implementation::dom_parser_implementation() {} // Leaving these here so they can be inlined if so desired @@ -57,3 +69,6 @@ WARN_UNUSED error_code dom_parser_implementation::set_max_depth(size_t max_depth _max_depth = max_depth; return SUCCESS; } + +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson diff --git a/src/generic/stage1/allocate.h b/src/generic/stage1/allocate.h index ab6fdb8ab..e9ecb07e4 100644 --- a/src/generic/stage1/allocate.h +++ b/src/generic/stage1/allocate.h @@ -1,3 +1,5 @@ +namespace simdjson { +namespace SIMDJSON_IMPLEMENTATION { namespace stage1 { namespace allocate { @@ -15,3 +17,5 @@ really_inline error_code set_capacity(internal::dom_parser_implementation &parse } // namespace allocate } // namespace stage1 +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson diff --git a/src/generic/stage1/buf_block_reader.h b/src/generic/stage1/buf_block_reader.h index fb5c07e7f..6eb28cc71 100644 --- a/src/generic/stage1/buf_block_reader.h +++ b/src/generic/stage1/buf_block_reader.h @@ -1,3 +1,6 @@ +namespace simdjson { +namespace SIMDJSON_IMPLEMENTATION { + // Walks through a buffer in block-sized increments, loading the last part with spaces template struct buf_block_reader { @@ -81,3 +84,6 @@ template really_inline void buf_block_reader::advance() { idx += STEP_SIZE; } + +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson diff --git a/src/generic/stage1/find_next_document_index.h b/src/generic/stage1/find_next_document_index.h index 41d6bdb14..7e42d7f32 100644 --- a/src/generic/stage1/find_next_document_index.h +++ b/src/generic/stage1/find_next_document_index.h @@ -1,3 +1,6 @@ +namespace simdjson { +namespace SIMDJSON_IMPLEMENTATION { + /** * This algorithm is used to quickly identify the last structural position that * makes up a complete document. @@ -84,3 +87,6 @@ really_inline size_t trim_partial_utf8(const uint8_t *buf, size_t len) { if (buf[len-3] >= 0b11110000) { return len-3; } // 4-byte characters with only 3 bytes left return len; } + +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson diff --git a/src/generic/stage1/json_minifier.h b/src/generic/stage1/json_minifier.h index 4055d90ae..46bfd4e52 100644 --- a/src/generic/stage1/json_minifier.h +++ b/src/generic/stage1/json_minifier.h @@ -3,6 +3,8 @@ // We assume the file in which it is included already includes // "simdjson/stage1.h" (this simplifies amalgation) +namespace simdjson { +namespace SIMDJSON_IMPLEMENTATION { namespace stage1 { class json_minifier { @@ -75,3 +77,5 @@ error_code json_minifier::minify(const uint8_t *buf, size_t len, uint8_t *dst, s } } // namespace stage1 +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson diff --git a/src/generic/stage1/json_scanner.h b/src/generic/stage1/json_scanner.h index 0834c4bd7..8544a8cff 100644 --- a/src/generic/stage1/json_scanner.h +++ b/src/generic/stage1/json_scanner.h @@ -1,3 +1,5 @@ +namespace simdjson { +namespace SIMDJSON_IMPLEMENTATION { namespace stage1 { /** @@ -102,3 +104,5 @@ really_inline error_code json_scanner::finish(bool streaming) { } } // namespace stage1 +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson diff --git a/src/generic/stage1/json_string_scanner.h b/src/generic/stage1/json_string_scanner.h index f37855c11..24d24d8ce 100644 --- a/src/generic/stage1/json_string_scanner.h +++ b/src/generic/stage1/json_string_scanner.h @@ -1,3 +1,5 @@ +namespace simdjson { +namespace SIMDJSON_IMPLEMENTATION { namespace stage1 { struct json_string_block { @@ -136,4 +138,6 @@ really_inline error_code json_string_scanner::finish(bool streaming) { return SUCCESS; } -} // namespace stage1 \ No newline at end of file +} // namespace stage1 +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson diff --git a/src/generic/stage1/json_structural_indexer.h b/src/generic/stage1/json_structural_indexer.h index cf6ec2617..a2663fe88 100644 --- a/src/generic/stage1/json_structural_indexer.h +++ b/src/generic/stage1/json_structural_indexer.h @@ -3,6 +3,8 @@ // We assume the file in which it is included already includes // "simdjson/stage1.h" (this simplifies amalgation) +namespace simdjson { +namespace SIMDJSON_IMPLEMENTATION { namespace stage1 { class bit_indexer { @@ -196,3 +198,5 @@ really_inline error_code json_structural_indexer::finish(dom_parser_implementati } } // namespace stage1 +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson diff --git a/src/generic/stage1/utf8_fastvalidate_algorithm.h b/src/generic/stage1/utf8_fastvalidate_algorithm.h index ea6a88ce4..fb9b95dc4 100644 --- a/src/generic/stage1/utf8_fastvalidate_algorithm.h +++ b/src/generic/stage1/utf8_fastvalidate_algorithm.h @@ -1,3 +1,6 @@ +namespace simdjson { +namespace SIMDJSON_IMPLEMENTATION { + /* * legal utf-8 byte sequence * http://www.unicode.org/versions/Unicode6.0.0/ch03.pdf - page 94 @@ -176,3 +179,6 @@ struct utf8_checker { return this->has_error.any_bits_set_anywhere() ? simdjson::UTF8_ERROR : simdjson::SUCCESS; } }; // struct utf8_checker + +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson diff --git a/src/generic/stage1/utf8_lookup2_algorithm.h b/src/generic/stage1/utf8_lookup2_algorithm.h index a5d7ca9a0..6badfa077 100644 --- a/src/generic/stage1/utf8_lookup2_algorithm.h +++ b/src/generic/stage1/utf8_lookup2_algorithm.h @@ -1,3 +1,6 @@ +namespace simdjson { +namespace SIMDJSON_IMPLEMENTATION { + // // Detect Unicode errors. // @@ -216,3 +219,6 @@ namespace utf8_validation { } using utf8_validation::utf8_checker; + +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson diff --git a/src/generic/stage1/utf8_lookup3_algorithm.h b/src/generic/stage1/utf8_lookup3_algorithm.h index e49cffccf..c8e439c29 100644 --- a/src/generic/stage1/utf8_lookup3_algorithm.h +++ b/src/generic/stage1/utf8_lookup3_algorithm.h @@ -1,3 +1,6 @@ +namespace simdjson { +namespace SIMDJSON_IMPLEMENTATION { + // // Detect Unicode errors. // @@ -236,3 +239,6 @@ namespace utf8_validation { } using utf8_validation::utf8_checker; + +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson diff --git a/src/generic/stage1/utf8_lookup4_algorithm.h b/src/generic/stage1/utf8_lookup4_algorithm.h index 745735100..1bb0fcac2 100644 --- a/src/generic/stage1/utf8_lookup4_algorithm.h +++ b/src/generic/stage1/utf8_lookup4_algorithm.h @@ -1,4 +1,5 @@ -using namespace simd; +namespace simdjson { +namespace SIMDJSON_IMPLEMENTATION { namespace utf8_validation { @@ -173,6 +174,9 @@ using namespace simd; } }; // struct utf8_checker -} +} // namespace utf8_validation -using utf8_validation::utf8_checker; \ No newline at end of file +using utf8_validation::utf8_checker; + +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson diff --git a/src/generic/stage1/utf8_lookup_algorithm.h b/src/generic/stage1/utf8_lookup_algorithm.h index ad2b97a56..4ad9b1fb8 100644 --- a/src/generic/stage1/utf8_lookup_algorithm.h +++ b/src/generic/stage1/utf8_lookup_algorithm.h @@ -1,3 +1,6 @@ +namespace simdjson { +namespace SIMDJSON_IMPLEMENTATION { + // // Detect Unicode errors. // @@ -297,3 +300,6 @@ struct utf8_checker { } }; // struct utf8_checker + +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson diff --git a/src/generic/stage1/utf8_range_algorithm.h b/src/generic/stage1/utf8_range_algorithm.h index 4caba6dc2..ac301bb8f 100644 --- a/src/generic/stage1/utf8_range_algorithm.h +++ b/src/generic/stage1/utf8_range_algorithm.h @@ -1,3 +1,6 @@ +namespace simdjson { +namespace SIMDJSON_IMPLEMENTATION { + /* * legal utf-8 byte sequence * http://www.unicode.org/versions/Unicode6.0.0/ch03.pdf - page 94 @@ -178,3 +181,6 @@ struct utf8_checker { return this->has_error.any() ? simdjson::UTF8_ERROR : simdjson::SUCCESS; } }; // struct utf8_checker + +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson diff --git a/src/generic/stage1/utf8_validator.h b/src/generic/stage1/utf8_validator.h index cd05cd09f..2851d793c 100644 --- a/src/generic/stage1/utf8_validator.h +++ b/src/generic/stage1/utf8_validator.h @@ -1,3 +1,5 @@ +namespace simdjson { +namespace SIMDJSON_IMPLEMENTATION { namespace stage1 { /** * Validates that the string is actual UTF-8. @@ -23,4 +25,6 @@ bool generic_validate_utf8(const char * input, size_t length) { return generic_validate_utf8((const uint8_t *)input,length); } -} // namespace stage1 \ No newline at end of file +} // namespace stage1 +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson diff --git a/src/generic/stage1/utf8_zwegner_algorithm.h b/src/generic/stage1/utf8_zwegner_algorithm.h index eb9bd2980..be81648cd 100644 --- a/src/generic/stage1/utf8_zwegner_algorithm.h +++ b/src/generic/stage1/utf8_zwegner_algorithm.h @@ -1,3 +1,5 @@ +namespace simdjson { +namespace SIMDJSON_IMPLEMENTATION { // // Detect UTF-8 errors. // @@ -358,3 +360,6 @@ struct utf8_checker { return (this->special_case_errors.any_bits_set_anywhere() | this->length_errors) ? simdjson::UTF8_ERROR : simdjson::SUCCESS; } }; // struct utf8_checker + +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson diff --git a/src/generic/stage2/allocate.h b/src/generic/stage2/allocate.h index ad35df111..e33a48472 100644 --- a/src/generic/stage2/allocate.h +++ b/src/generic/stage2/allocate.h @@ -1,3 +1,5 @@ +namespace simdjson { +namespace SIMDJSON_IMPLEMENTATION { namespace stage2 { namespace allocate { @@ -16,3 +18,5 @@ really_inline error_code set_max_depth(dom_parser_implementation &parser, size_t } // namespace allocate } // namespace stage2 +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson diff --git a/src/generic/stage2/atomparsing.h b/src/generic/stage2/atomparsing.h index 2a23c525b..c0d5f78fc 100644 --- a/src/generic/stage2/atomparsing.h +++ b/src/generic/stage2/atomparsing.h @@ -1,3 +1,5 @@ +namespace simdjson { +namespace SIMDJSON_IMPLEMENTATION { namespace stage2 { namespace atomparsing { @@ -60,3 +62,5 @@ really_inline bool is_valid_null_atom(const uint8_t *src, size_t len) { } // namespace atomparsing } // namespace stage2 +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson diff --git a/src/generic/stage2/logger.h b/src/generic/stage2/logger.h index 2f5438404..9da6c1ef9 100644 --- a/src/generic/stage2/logger.h +++ b/src/generic/stage2/logger.h @@ -1,5 +1,7 @@ // This is for an internal-only stage 2 specific logger. // Set LOG_ENABLED = true to log what stage 2 is doing! +namespace simdjson { +namespace SIMDJSON_IMPLEMENTATION { namespace logger { static constexpr const char * DASHES = "----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"; @@ -61,4 +63,5 @@ namespace logger { } } } // namespace logger - +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson diff --git a/src/generic/stage2/numberparsing.h b/src/generic/stage2/numberparsing.h index 16255baf9..14e8d9576 100644 --- a/src/generic/stage2/numberparsing.h +++ b/src/generic/stage2/numberparsing.h @@ -1,3 +1,5 @@ +namespace simdjson { +namespace SIMDJSON_IMPLEMENTATION { namespace stage2 { namespace numberparsing { @@ -472,3 +474,5 @@ really_inline bool parse_number(const uint8_t *const src, W &writer) { } // namespace numberparsing } // namespace stage2 +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson diff --git a/src/generic/stage2/streaming_structural_parser.h b/src/generic/stage2/streaming_structural_parser.h deleted file mode 100755 index 8e63d0287..000000000 --- a/src/generic/stage2/streaming_structural_parser.h +++ /dev/null @@ -1,168 +0,0 @@ -namespace stage2 { - -struct streaming_structural_parser: structural_parser { - really_inline streaming_structural_parser(dom_parser_implementation &_parser) : structural_parser(_parser, _parser.next_structural_index) {} - - // override to add streaming - WARN_UNUSED really_inline error_code start(ret_address_t finish_parser) { - // If there are no structurals left, return EMPTY - if (structurals.at_end(parser.n_structural_indexes)) { - return parser.error = EMPTY; - } - - log_start(); - init(); - - // Capacity ain't no thang for streaming, so we don't check it. - // Advance to the first character as soon as possible - advance_char(); - // Push the root scope (there is always at least one scope) - if (start_document(finish_parser)) { - return parser.error = DEPTH_ERROR; - } - return SUCCESS; - } - - // override to add streaming - WARN_UNUSED really_inline error_code finish() { - if ( structurals.past_end(parser.n_structural_indexes) ) { - log_error("IMPOSSIBLE: past the end of the JSON!"); - return parser.error = TAPE_ERROR; - } - end_document(); - parser.next_structural_index = uint32_t(structurals.next_structural_index()); - if (depth != 0) { - log_error("Unclosed objects or arrays!"); - return parser.error = TAPE_ERROR; - } - if (parser.containing_scope[depth].tape_index != 0) { - log_error("IMPOSSIBLE: root scope tape index did not start at 0!"); - return parser.error = TAPE_ERROR; - } - return SUCCESS; - } -}; - -} // namespace stage2 - -/************ - * The JSON is parsed to a tape, see the accompanying tape.md file - * for documentation. - ***********/ -WARN_UNUSED error_code dom_parser_implementation::stage2_next(dom::document &_doc) noexcept { - this->doc = &_doc; - static constexpr stage2::unified_machine_addresses addresses = INIT_ADDRESSES(); - stage2::streaming_structural_parser parser(*this); - error_code result = parser.start(addresses.finish); - if (result) { return result; } - // - // Read first value - // - switch (parser.structurals.current_char()) { - case '{': - FAIL_IF( parser.start_object(addresses.finish) ); - goto object_begin; - case '[': - FAIL_IF( parser.start_array(addresses.finish) ); - goto array_begin; - case '"': - FAIL_IF( parser.parse_string() ); - goto finish; - case 't': case 'f': case 'n': - FAIL_IF( parser.parse_single_atom() ); - goto finish; - case '0': case '1': case '2': case '3': case '4': - case '5': case '6': case '7': case '8': case '9': - FAIL_IF( - parser.structurals.with_space_terminated_copy([&](const uint8_t *copy, size_t idx) { - return parser.parse_number(©[idx], false); - }) - ); - goto finish; - case '-': - FAIL_IF( - parser.structurals.with_space_terminated_copy([&](const uint8_t *copy, size_t idx) { - return parser.parse_number(©[idx], true); - }) - ); - goto finish; - default: - parser.log_error("Document starts with a non-value character"); - goto error; - } - -// -// Object parser parsers -// -object_begin: - switch (parser.advance_char()) { - case '"': { - FAIL_IF( parser.parse_string(true) ); - goto object_key_parser; - } - case '}': - parser.end_object(); - goto scope_end; - default: - parser.log_error("Object does not start with a key"); - goto error; - } - -object_key_parser: - if (parser.advance_char() != ':' ) { parser.log_error("Missing colon after key in object"); goto error; } - parser.increment_count(); - parser.advance_char(); - GOTO( parser.parse_value(addresses, addresses.object_continue) ); - -object_continue: - switch (parser.advance_char()) { - case ',': - if (parser.advance_char() != '"' ) { parser.log_error("Key string missing at beginning of field in object"); goto error; } - FAIL_IF( parser.parse_string(true) ); - goto object_key_parser; - case '}': - parser.end_object(); - goto scope_end; - default: - parser.log_error("No comma between object fields"); - goto error; - } - -scope_end: - CONTINUE( parser.parser.ret_address[parser.depth] ); - -// -// Array parser parsers -// -array_begin: - if (parser.advance_char() == ']') { - parser.end_array(); - goto scope_end; - } - parser.increment_count(); - -main_array_switch: - /* we call update char on all paths in, so we can peek at parser.c on the - * on paths that can accept a close square brace (post-, and at start) */ - GOTO( parser.parse_value(addresses, addresses.array_continue) ); - -array_continue: - switch (parser.advance_char()) { - case ',': - parser.increment_count(); - parser.advance_char(); - goto main_array_switch; - case ']': - parser.end_array(); - goto scope_end; - default: - parser.log_error("Missing comma between array values"); - goto error; - } - -finish: - return parser.finish(); - -error: - return parser.error(); -} diff --git a/src/generic/stage2/stringparsing.h b/src/generic/stage2/stringparsing.h index 3c8b64197..c777cfe7e 100644 --- a/src/generic/stage2/stringparsing.h +++ b/src/generic/stage2/stringparsing.h @@ -3,6 +3,8 @@ // We assume the file in which it is include already includes // "stringparsing.h" (this simplifies amalgation) +namespace simdjson { +namespace SIMDJSON_IMPLEMENTATION { namespace stage2 { namespace stringparsing { @@ -120,4 +122,6 @@ WARN_UNUSED really_inline uint8_t *parse_string(const uint8_t *src, uint8_t *dst } } // namespace stringparsing -} // namespace stage2 \ No newline at end of file +} // namespace stage2 +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson diff --git a/src/generic/stage2/structural_iterator.h b/src/generic/stage2/structural_iterator.h index 2682b6d0c..c1c7b759c 100644 --- a/src/generic/stage2/structural_iterator.h +++ b/src/generic/stage2/structural_iterator.h @@ -1,3 +1,5 @@ +namespace simdjson { +namespace SIMDJSON_IMPLEMENTATION { namespace stage2 { class structural_iterator { @@ -44,3 +46,5 @@ public: }; } // namespace stage2 +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson diff --git a/src/generic/stage2/structural_parser.h b/src/generic/stage2/structural_parser.h index d6ca1acab..adaa89133 100644 --- a/src/generic/stage2/structural_parser.h +++ b/src/generic/stage2/structural_parser.h @@ -3,11 +3,14 @@ // We assume the file in which it is include already includes // "simdjson/stage2.h" (this simplifies amalgation) -namespace stage2 { -namespace { // Make everything here private - #include "generic/stage2/tape_writer.h" +namespace simdjson { +namespace SIMDJSON_IMPLEMENTATION { +namespace stage2 { + +namespace { // Make everything here private + #ifdef SIMDJSON_USE_COMPUTED_GOTO #define INIT_ADDRESSES() { &&array_begin, &&array_continue, &&error, &&finish, &&object_begin, &&object_continue } #define GOTO(address) { goto *(address); } @@ -489,3 +492,6 @@ WARN_UNUSED error_code dom_parser_implementation::stage2(dom::document &_doc) no WARN_UNUSED error_code dom_parser_implementation::stage2_next(dom::document &_doc) noexcept { return stage2::parse_structurals(*this, _doc); } + +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson diff --git a/src/generic/stage2/tape_writer.h b/src/generic/stage2/tape_writer.h index 47282d6af..aed7e91f3 100644 --- a/src/generic/stage2/tape_writer.h +++ b/src/generic/stage2/tape_writer.h @@ -1,3 +1,7 @@ +namespace simdjson { +namespace SIMDJSON_IMPLEMENTATION { +namespace stage2 { + struct tape_writer { /** The next place to write to tape */ uint64_t *next_tape_loc; @@ -93,3 +97,7 @@ really_inline void tape_writer::append2(uint64_t val, T val2, internal::tape_typ really_inline void tape_writer::write(uint64_t &tape_loc, uint64_t val, internal::tape_type t) noexcept { tape_loc = val | ((uint64_t(char(t))) << 56); } + +} // namespace stage2 +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson diff --git a/src/haswell/dom_parser_implementation.cpp b/src/haswell/dom_parser_implementation.cpp index b104754e0..40c4256f2 100644 --- a/src/haswell/dom_parser_implementation.cpp +++ b/src/haswell/dom_parser_implementation.cpp @@ -66,53 +66,58 @@ really_inline simd8 must_be_2_3_continuation(const simd8 prev2, c return simd8(is_third_byte | is_fourth_byte) > int8_t(0); } +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson #include "generic/stage1/buf_block_reader.h" #include "generic/stage1/json_string_scanner.h" #include "generic/stage1/json_scanner.h" - -namespace stage1 { -really_inline uint64_t json_string_scanner::find_escaped(uint64_t backslash) { - if (!backslash) { uint64_t escaped = prev_escaped; prev_escaped = 0; return escaped; } - return find_escaped_branchless(backslash); -} -} - #include "generic/stage1/json_minifier.h" -WARN_UNUSED error_code implementation::minify(const uint8_t *buf, size_t len, uint8_t *dst, size_t &dst_len) const noexcept { - return haswell::stage1::json_minifier::minify<128>(buf, len, dst, dst_len); -} - #include "generic/stage1/find_next_document_index.h" #include "generic/stage1/utf8_lookup4_algorithm.h" #include "generic/stage1/json_structural_indexer.h" -WARN_UNUSED error_code dom_parser_implementation::stage1(const uint8_t *_buf, size_t _len, bool streaming) noexcept { - this->buf = _buf; - this->len = _len; - return haswell::stage1::json_structural_indexer::index<128>(_buf, _len, *this, streaming); -} -#include "generic/stage1/utf8_validator.h" -WARN_UNUSED bool implementation::validate_utf8(const char *buf, size_t len) const noexcept { - return simdjson::haswell::stage1::generic_validate_utf8(buf,len); -} - -} // namespace SIMDJSON_IMPLEMENTATION -} // namespace simdjson // // Stage 2 // #include "haswell/stringparsing.h" #include "haswell/numberparsing.h" - -namespace simdjson { -namespace SIMDJSON_IMPLEMENTATION { - #include "generic/stage2/logger.h" #include "generic/stage2/atomparsing.h" #include "generic/stage2/structural_iterator.h" #include "generic/stage2/structural_parser.h" +// +// Implementation-specific overrides +// +namespace simdjson { +namespace SIMDJSON_IMPLEMENTATION { + +namespace stage1 { + +really_inline uint64_t json_string_scanner::find_escaped(uint64_t backslash) { + if (!backslash) { uint64_t escaped = prev_escaped; prev_escaped = 0; return escaped; } + return find_escaped_branchless(backslash); +} + +} // namespace stage1 + +WARN_UNUSED error_code implementation::minify(const uint8_t *buf, size_t len, uint8_t *dst, size_t &dst_len) const noexcept { + return haswell::stage1::json_minifier::minify<128>(buf, len, dst, dst_len); +} + +WARN_UNUSED error_code dom_parser_implementation::stage1(const uint8_t *_buf, size_t _len, bool streaming) noexcept { + this->buf = _buf; + this->len = _len; + return haswell::stage1::json_structural_indexer::index<128>(_buf, _len, *this, streaming); +} + +#include "generic/stage1/utf8_validator.h" + +WARN_UNUSED bool implementation::validate_utf8(const char *buf, size_t len) const noexcept { + return simdjson::haswell::stage1::generic_validate_utf8(buf,len); +} + WARN_UNUSED error_code dom_parser_implementation::parse(const uint8_t *_buf, size_t _len, dom::document &_doc) noexcept { error_code err = stage1(_buf, _len, false); if (err) { return err; } diff --git a/src/haswell/dom_parser_implementation.h b/src/haswell/dom_parser_implementation.h index 180be00a3..15d7808aa 100644 --- a/src/haswell/dom_parser_implementation.h +++ b/src/haswell/dom_parser_implementation.h @@ -1,15 +1,6 @@ #ifndef SIMDJSON_HASWELL_DOM_PARSER_IMPLEMENTATION_H #define SIMDJSON_HASWELL_DOM_PARSER_IMPLEMENTATION_H -#include "simdjson.h" -#include "isadetection.h" - -namespace simdjson { -namespace SIMDJSON_IMPLEMENTATION { - #include "generic/dom_parser_implementation.h" -} // namespace SIMDJSON_IMPLEMENTATION -} // namespace simdjson - #endif // SIMDJSON_HASWELL_DOM_PARSER_IMPLEMENTATION_H \ No newline at end of file diff --git a/src/haswell/numberparsing.h b/src/haswell/numberparsing.h index 472b62d05..6bb0b0c22 100644 --- a/src/haswell/numberparsing.h +++ b/src/haswell/numberparsing.h @@ -36,11 +36,11 @@ static really_inline uint32_t parse_eight_digits_unrolled(const uint8_t *chars) t4); // only captures the sum of the first 8 digits, drop the rest } +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson + #define SWAR_NUMBER_PARSING #include "generic/stage2/numberparsing.h" -} // namespace SIMDJSON_IMPLEMENTATION -} // namespace simdjson - #endif // SIMDJSON_HASWELL_NUMBERPARSING_H diff --git a/src/haswell/stringparsing.h b/src/haswell/stringparsing.h index a7b52495e..ce33f8e83 100644 --- a/src/haswell/stringparsing.h +++ b/src/haswell/stringparsing.h @@ -39,9 +39,9 @@ really_inline backslash_and_quote backslash_and_quote::copy_and_find(const uint8 }; } -#include "generic/stage2/stringparsing.h" - } // namespace SIMDJSON_IMPLEMENTATION } // namespace simdjson +#include "generic/stage2/stringparsing.h" + #endif // SIMDJSON_HASWELL_STRINGPARSING_H diff --git a/src/westmere/dom_parser_implementation.cpp b/src/westmere/dom_parser_implementation.cpp index ff156970c..69fb7b9dd 100644 --- a/src/westmere/dom_parser_implementation.cpp +++ b/src/westmere/dom_parser_implementation.cpp @@ -71,53 +71,58 @@ really_inline simd8 must_be_2_3_continuation(const simd8 prev2, c return simd8(is_third_byte | is_fourth_byte) > int8_t(0); } +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson #include "generic/stage1/buf_block_reader.h" #include "generic/stage1/json_string_scanner.h" #include "generic/stage1/json_scanner.h" - -namespace stage1 { -really_inline uint64_t json_string_scanner::find_escaped(uint64_t backslash) { - if (!backslash) { uint64_t escaped = prev_escaped; prev_escaped = 0; return escaped; } - return find_escaped_branchless(backslash); -} -} - #include "generic/stage1/json_minifier.h" -WARN_UNUSED error_code implementation::minify(const uint8_t *buf, size_t len, uint8_t *dst, size_t &dst_len) const noexcept { - return westmere::stage1::json_minifier::minify<64>(buf, len, dst, dst_len); -} - #include "generic/stage1/find_next_document_index.h" #include "generic/stage1/utf8_lookup4_algorithm.h" #include "generic/stage1/json_structural_indexer.h" -WARN_UNUSED error_code dom_parser_implementation::stage1(const uint8_t *_buf, size_t _len, bool streaming) noexcept { - this->buf = _buf; - this->len = _len; - return westmere::stage1::json_structural_indexer::index<64>(_buf, _len, *this, streaming); -} -#include "generic/stage1/utf8_validator.h" -WARN_UNUSED bool implementation::validate_utf8(const char *buf, size_t len) const noexcept { - return simdjson::westmere::stage1::generic_validate_utf8(buf,len); -} - -} // namespace SIMDJSON_IMPLEMENTATION -} // namespace simdjson // // Stage 2 // #include "westmere/stringparsing.h" #include "westmere/numberparsing.h" - -namespace simdjson { -namespace SIMDJSON_IMPLEMENTATION { - #include "generic/stage2/logger.h" #include "generic/stage2/atomparsing.h" #include "generic/stage2/structural_iterator.h" #include "generic/stage2/structural_parser.h" +// +// Implementation-specific overrides +// +namespace simdjson { +namespace SIMDJSON_IMPLEMENTATION { + +namespace stage1 { + +really_inline uint64_t json_string_scanner::find_escaped(uint64_t backslash) { + if (!backslash) { uint64_t escaped = prev_escaped; prev_escaped = 0; return escaped; } + return find_escaped_branchless(backslash); +} + +} // namespace stage1 + +WARN_UNUSED error_code implementation::minify(const uint8_t *buf, size_t len, uint8_t *dst, size_t &dst_len) const noexcept { + return westmere::stage1::json_minifier::minify<64>(buf, len, dst, dst_len); +} + +WARN_UNUSED error_code dom_parser_implementation::stage1(const uint8_t *_buf, size_t _len, bool streaming) noexcept { + this->buf = _buf; + this->len = _len; + return westmere::stage1::json_structural_indexer::index<64>(_buf, _len, *this, streaming); +} + +#include "generic/stage1/utf8_validator.h" + +WARN_UNUSED bool implementation::validate_utf8(const char *buf, size_t len) const noexcept { + return simdjson::westmere::stage1::generic_validate_utf8(buf,len); +} + WARN_UNUSED error_code dom_parser_implementation::parse(const uint8_t *_buf, size_t _len, dom::document &_doc) noexcept { error_code err = stage1(_buf, _len, false); if (err) { return err; } diff --git a/src/westmere/dom_parser_implementation.h b/src/westmere/dom_parser_implementation.h index c36d9c8a8..56e518b03 100644 --- a/src/westmere/dom_parser_implementation.h +++ b/src/westmere/dom_parser_implementation.h @@ -1,15 +1,6 @@ #ifndef SIMDJSON_WESTMERE_DOM_PARSER_IMPLEMENTATION_H #define SIMDJSON_WESTMERE_DOM_PARSER_IMPLEMENTATION_H -#include "simdjson.h" -#include "isadetection.h" - -namespace simdjson { -namespace westmere { - #include "generic/dom_parser_implementation.h" -} // namespace westmere -} // namespace simdjson - #endif // SIMDJSON_WESTMERE_DOM_PARSER_IMPLEMENTATION_H \ No newline at end of file diff --git a/src/westmere/numberparsing.h b/src/westmere/numberparsing.h index f10fc77c3..ea9c04785 100644 --- a/src/westmere/numberparsing.h +++ b/src/westmere/numberparsing.h @@ -36,11 +36,11 @@ static really_inline uint32_t parse_eight_digits_unrolled(const uint8_t *chars) t4); // only captures the sum of the first 8 digits, drop the rest } +} // namespace SIMDJSON_IMPLEMENTATION +} // namespace simdjson + #define SWAR_NUMBER_PARSING #include "generic/stage2/numberparsing.h" -} // namespace SIMDJSON_IMPLEMENTATION -} // namespace simdjson - #endif // SIMDJSON_WESTMERE_NUMBERPARSING_H diff --git a/src/westmere/stringparsing.h b/src/westmere/stringparsing.h index 09f34318f..1cc796d51 100644 --- a/src/westmere/stringparsing.h +++ b/src/westmere/stringparsing.h @@ -41,9 +41,9 @@ really_inline backslash_and_quote backslash_and_quote::copy_and_find(const uint8 }; } -#include "generic/stage2/stringparsing.h" - } // namespace SIMDJSON_IMPLEMENTATION } // namespace simdjson +#include "generic/stage2/stringparsing.h" + #endif // SIMDJSON_WESTMERE_STRINGPARSING_H