Use active implementation for stage 1 on demand

This commit is contained in:
John Keiser 2021-01-06 21:18:03 -08:00
parent 7dbe4caf3f
commit 6fed8d2a26
4 changed files with 19 additions and 14 deletions

View File

@ -19,8 +19,8 @@ simdjson_really_inline json_iterator &json_iterator::operator=(json_iterator &&o
return *this; return *this;
} }
simdjson_really_inline json_iterator::json_iterator(ondemand::parser *_parser) noexcept simdjson_really_inline json_iterator::json_iterator(const uint8_t *buf, ondemand::parser *_parser) noexcept
: token(_parser->dom_parser.buf, _parser->dom_parser.structural_indexes.get()), : token(buf, _parser->dom_parser->structural_indexes.get()),
parser{_parser}, parser{_parser},
_string_buf_loc{parser->string_buf.get()}, _string_buf_loc{parser->string_buf.get()},
_depth{1} _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 // 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) { while (token.index <= end) {
switch (*advance()) { switch (*advance()) {
case '[': case '{': 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 { 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_really_inline void json_iterator::assert_at_root() const noexcept {
SIMDJSON_ASSUME( _depth == 1 ); SIMDJSON_ASSUME( _depth == 1 );
// Visual Studio Clang treats unique_ptr.get() as "side effecting." // Visual Studio Clang treats unique_ptr.get() as "side effecting."
#ifndef SIMDJSON_CLANG_VISUAL_STUDIO #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 #endif
} }
simdjson_really_inline bool json_iterator::at_eof() const noexcept { 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 { simdjson_really_inline bool json_iterator::is_alive() const noexcept {

View File

@ -167,7 +167,7 @@ public:
simdjson_really_inline void restore_checkpoint(const uint32_t *target_checkpoint) noexcept; simdjson_really_inline void restore_checkpoint(const uint32_t *target_checkpoint) noexcept;
protected: 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 document;
friend class object; friend class object;

View File

@ -10,8 +10,12 @@ simdjson_warn_unused simdjson_really_inline error_code parser::allocate(size_t n
_max_depth = 0; _max_depth = 0;
size_t string_capacity = SIMDJSON_ROUNDUP_N(5 * new_capacity / 3 + SIMDJSON_PADDING, 64); size_t string_capacity = SIMDJSON_ROUNDUP_N(5 * new_capacity / 3 + SIMDJSON_PADDING, 64);
string_buf.reset(new (std::nothrow) uint8_t[string_capacity]); string_buf.reset(new (std::nothrow) uint8_t[string_capacity]);
SIMDJSON_TRY( dom_parser.set_capacity(new_capacity) ); if (dom_parser) {
SIMDJSON_TRY( dom_parser.set_max_depth(DEFAULT_MAX_DEPTH) ); 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; _capacity = new_capacity;
_max_depth = new_max_depth; _max_depth = new_max_depth;
return SUCCESS; return SUCCESS;
@ -24,8 +28,8 @@ simdjson_warn_unused simdjson_really_inline simdjson_result<document> parser::it
} }
// Run stage 1. // Run stage 1.
SIMDJSON_TRY( dom_parser.stage1((const uint8_t *)buf.data(), buf.size(), false) ); SIMDJSON_TRY( dom_parser->stage1((const uint8_t *)buf.data(), buf.size(), false) );
return document::start(this); return document::start({ (const uint8_t *)buf.data(), this });
} }
simdjson_warn_unused simdjson_really_inline simdjson_result<document> parser::iterate(const simdjson_result<padded_string> &result) & noexcept { simdjson_warn_unused simdjson_really_inline simdjson_result<document> parser::iterate(const simdjson_result<padded_string> &result) & noexcept {
@ -42,8 +46,8 @@ simdjson_warn_unused simdjson_really_inline simdjson_result<json_iterator> parse
} }
// Run stage 1. // Run stage 1.
SIMDJSON_TRY( dom_parser.stage1((const uint8_t *)buf.data(), buf.size(), false) ); SIMDJSON_TRY( dom_parser->stage1((const uint8_t *)buf.data(), buf.size(), false) );
return json_iterator(this); return json_iterator((const uint8_t *)buf.data(), this);
} }
} // namespace ondemand } // namespace ondemand

View File

@ -104,7 +104,8 @@ public:
simdjson_warn_unused simdjson_result<json_iterator> iterate_raw(const padded_string &json) & noexcept; simdjson_warn_unused simdjson_result<json_iterator> iterate_raw(const padded_string &json) & noexcept;
private: private:
dom_parser_implementation dom_parser{}; /** @private [for benchmarking access] The implementation to use */
std::unique_ptr<internal::dom_parser_implementation> dom_parser{};
size_t _capacity{0}; size_t _capacity{0};
size_t _max_depth{0}; size_t _max_depth{0};
std::unique_ptr<uint8_t[]> string_buf{}; std::unique_ptr<uint8_t[]> string_buf{};