Repository navigation
feat: Add DOM tape support for big integers (opt-in) - #2640
Conversation
ef92604 to
466a7b2
Compare
| iter.log_value("number"); | ||
| return numberparsing::parse_number(value, tape); | ||
| error_code err = numberparsing::parse_number(value, tape); | ||
| if (err == BIGINT_ERROR && iter.dom_parser._number_as_string) { |
There was a problem hiding this comment.
| if (err == BIGINT_ERROR && iter.dom_parser._number_as_string) { | |
| if (simdjson_unlikely(err == BIGINT_ERROR && iter.dom_parser._number_as_string)) { |
|
@harshanagd This is reasonable, but I am concerned that the added error path could mess up inlining decisions and we might end up making all users pay for the extra functionality. Hence my suggestion to add an unlikely hint. |
std::cout << value.get_bigint().value() << std::endl;This throws on error and will not compile if exceptions are disabled (which is common, e.g., see Node.js). |
|
@harshanagd You do not have to do it with this PR, but we'll need to document that feature in |
When parser.number_as_string(true) is set and a number exceeds uint64
range, write the raw digits to the string buffer and emit a BIGINT ('Z')
tape tag instead of returning BIGINT_ERROR. Default behavior unchanged.
Changes:
- tape_type.h: add BIGINT = 'Z'
- dom_parser_implementation.h: add _number_as_string flag
- parser.h: add number_as_string() getter/setter
- parser-inl.h: propagate flag before parse
- tape_builder.h: visit_number checks flag on BIGINT_ERROR
- tape_writer.h: add append_bigint helper
- serialization-inl.h: handle BIGINT in serialization (raw digits)
- tape.md: document big integer tape format
Builds on PR simdjson#2139 which added BIGINT_ERROR detection.
All 119 existing tests pass — default behavior unchanged.
Closes simdjson#167
466a7b2 to
be9aace
Compare
|
Thank you for your suggestions. I have actioned them. I have also updated the |
lemire
left a comment
There was a problem hiding this comment.
I approve but will let it sit a bit to see if others have comments.
lemire
left a comment
There was a problem hiding this comment.
I approve but will give a chance of others to comment.
Motivation
PR #2139 added
BIGINT_ERRORto distinguish big integers from invalid numbers. This works well for On Demand users who can callraw_json_token()after the error. However, DOM users have no recovery path — the parsefails entirely.
This PR adds opt-in DOM support: when enabled, big integers are written to the string buffer and exposed through a complete element API.
Design
Tape format: Big integers use tag
'Z'with the same string buffer layout as'"'(4-byte LE length + UTF-8 bytes + null). Reuseson_end_string()for buffer writes — no duplicated logic. Documented intape.md.Zero cost when disabled: The check is on the error path only —
parse_numberreturnsBIGINT_ERROR, thenvisit_numberchecks the flag. The normal number parsing path (SUCCESS) is untouched. No new branchesin the hot path.
Serialization: Big integers are output as raw digits (no quotes), preserving valid JSON.
API
element::type()element_type::BIGINTelement::is_bigint()boolelement::get_bigint()simdjson_result<std::string_view>get_int64()/get_uint64()/get_double()on BIGINTINCORRECT_TYPEparser.number_as_string(true/false)voidparser.number_as_string()boolChanges
tape_type.hBIGINT = 'Z'dom_parser_implementation.h_number_as_stringflagparser.hnumber_as_string()getter/setterparser-inl.htape_builder.hBIGINT_ERRORwhen flag is settape_writer.happend_biginthelperelement.helement_type::BIGINT,get_bigint(),is_bigint()element-inl.hoperator<<+simdjson_resultwrappersserialization-inl.hBIGINTtag (raw digits, no quotes)document-inl.hdump_raw_tapesupport forBIGINTtape.mdbig_integer_tests.cppreadme_examples.cppBIGINTcase to tree walkerfuzz_dump.cppBIGINTcase to fuzzerTests
9 tests covering:
option_off_by_default— flag is falsedefault_returns_bigint_error— without opt-in, existing behavior preservedopt_in_parses_big_integer— parse, type check,get_bigint(), streamingnegative_big_integer— signed big integer with raw digits including-big_integer_in_array— mixed array with big integerserialization_preserves_digits— raw digits present, not quotednormal_numbers_unaffected— int64/uint64/double work with flag enabledtype_checks—is_bigint()true, all otheris_*false,get_int64/uint64/double/stringreturnINCORRECT_TYPEelement_type_output—operator<<prints"bigint"Performance
No regression — the flag check is only reached on the
BIGINT_ERRORpath, which is already an error exit. Normal number parsing is unchanged.Notes
#ifdefif a runtime flag is undesirable.BIGINT_ERRORdetection.