diff --git a/include/simdjson/generic/ondemand/json_iterator-inl.h b/include/simdjson/generic/ondemand/json_iterator-inl.h index 447373fc..b72a6d43 100644 --- a/include/simdjson/generic/ondemand/json_iterator-inl.h +++ b/include/simdjson/generic/ondemand/json_iterator-inl.h @@ -19,8 +19,8 @@ simdjson_really_inline json_iterator &json_iterator::operator=(json_iterator &&o return *this; } -simdjson_really_inline json_iterator::json_iterator(ondemand::parser *_parser) noexcept - : token(_parser->dom_parser.buf, _parser->dom_parser.structural_indexes.get()), +simdjson_really_inline json_iterator::json_iterator(const uint8_t *buf, ondemand::parser *_parser) noexcept + : token(buf, _parser->dom_parser->structural_indexes.get()), parser{_parser}, _string_buf_loc{parser->string_buf.get()}, _depth{1} @@ -69,7 +69,7 @@ simdjson_warn_unused simdjson_really_inline error_code json_iterator::skip_child } // Now that we've considered the first value, we only increment/decrement for arrays/objects - auto end = &parser->dom_parser.structural_indexes[parser->dom_parser.n_structural_indexes]; + auto end = &parser->dom_parser->structural_indexes[parser->dom_parser->n_structural_indexes]; while (token.index <= end) { switch (*advance()) { case '[': case '{': @@ -102,19 +102,19 @@ simdjson_really_inline bool json_iterator::at_root() const noexcept { } simdjson_really_inline const uint32_t *json_iterator::root_checkpoint() const noexcept { - return parser->dom_parser.structural_indexes.get(); + return parser->dom_parser->structural_indexes.get(); } simdjson_really_inline void json_iterator::assert_at_root() const noexcept { SIMDJSON_ASSUME( _depth == 1 ); // Visual Studio Clang treats unique_ptr.get() as "side effecting." #ifndef SIMDJSON_CLANG_VISUAL_STUDIO - SIMDJSON_ASSUME( token.index == parser->dom_parser.structural_indexes.get() ); + SIMDJSON_ASSUME( token.index == parser->dom_parser->structural_indexes.get() ); #endif } simdjson_really_inline bool json_iterator::at_eof() const noexcept { - return token.index == &parser->dom_parser.structural_indexes[parser->dom_parser.n_structural_indexes]; + return token.index == &parser->dom_parser->structural_indexes[parser->dom_parser->n_structural_indexes]; } simdjson_really_inline bool json_iterator::is_alive() const noexcept { diff --git a/include/simdjson/generic/ondemand/json_iterator.h b/include/simdjson/generic/ondemand/json_iterator.h index 944a9959..98907ae7 100644 --- a/include/simdjson/generic/ondemand/json_iterator.h +++ b/include/simdjson/generic/ondemand/json_iterator.h @@ -167,7 +167,7 @@ public: simdjson_really_inline void restore_checkpoint(const uint32_t *target_checkpoint) noexcept; protected: - simdjson_really_inline json_iterator(ondemand::parser *parser) noexcept; + simdjson_really_inline json_iterator(const uint8_t *buf, ondemand::parser *parser) noexcept; friend class document; friend class object; diff --git a/include/simdjson/generic/ondemand/parser-inl.h b/include/simdjson/generic/ondemand/parser-inl.h index 4b4770a0..e6b5ff84 100644 --- a/include/simdjson/generic/ondemand/parser-inl.h +++ b/include/simdjson/generic/ondemand/parser-inl.h @@ -10,8 +10,12 @@ simdjson_warn_unused simdjson_really_inline error_code parser::allocate(size_t n _max_depth = 0; size_t string_capacity = SIMDJSON_ROUNDUP_N(5 * new_capacity / 3 + SIMDJSON_PADDING, 64); string_buf.reset(new (std::nothrow) uint8_t[string_capacity]); - SIMDJSON_TRY( dom_parser.set_capacity(new_capacity) ); - SIMDJSON_TRY( dom_parser.set_max_depth(DEFAULT_MAX_DEPTH) ); + if (dom_parser) { + SIMDJSON_TRY( dom_parser->set_capacity(new_capacity) ); + SIMDJSON_TRY( dom_parser->set_max_depth(new_max_depth) ); + } else { + SIMDJSON_TRY( simdjson::active_implementation->create_dom_parser_implementation(new_capacity, new_max_depth, dom_parser) ); + } _capacity = new_capacity; _max_depth = new_max_depth; return SUCCESS; @@ -24,8 +28,8 @@ simdjson_warn_unused simdjson_really_inline simdjson_result parser::it } // Run stage 1. - SIMDJSON_TRY( dom_parser.stage1((const uint8_t *)buf.data(), buf.size(), false) ); - return document::start(this); + SIMDJSON_TRY( dom_parser->stage1((const uint8_t *)buf.data(), buf.size(), false) ); + return document::start({ (const uint8_t *)buf.data(), this }); } simdjson_warn_unused simdjson_really_inline simdjson_result parser::iterate(const simdjson_result &result) & noexcept { @@ -42,8 +46,8 @@ simdjson_warn_unused simdjson_really_inline simdjson_result parse } // Run stage 1. - SIMDJSON_TRY( dom_parser.stage1((const uint8_t *)buf.data(), buf.size(), false) ); - return json_iterator(this); + SIMDJSON_TRY( dom_parser->stage1((const uint8_t *)buf.data(), buf.size(), false) ); + return json_iterator((const uint8_t *)buf.data(), this); } } // namespace ondemand diff --git a/include/simdjson/generic/ondemand/parser.h b/include/simdjson/generic/ondemand/parser.h index f91e6ec5..291d5141 100644 --- a/include/simdjson/generic/ondemand/parser.h +++ b/include/simdjson/generic/ondemand/parser.h @@ -104,7 +104,8 @@ public: simdjson_warn_unused simdjson_result iterate_raw(const padded_string &json) & noexcept; private: - dom_parser_implementation dom_parser{}; + /** @private [for benchmarking access] The implementation to use */ + std::unique_ptr dom_parser{}; size_t _capacity{0}; size_t _max_depth{0}; std::unique_ptr string_buf{};