Skip to content

feat: Add DOM tape support for big integers (opt-in) - #2640

Merged
lemire merged 1 commit into
simdjson:masterfrom
harshanagd:feature/dom-big-integer
Mar 25, 2026
Merged

lemire merged 1 commit into
simdjson:masterfrom
harshanagd:feature/dom-big-integer

Conversation

@harshanagd

@harshanagd harshanagd commented Mar 21, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

PR #2139 added BIGINT_ERROR to distinguish big integers from invalid numbers. This works well for On Demand users who can call raw_json_token() after the error. However, DOM users have no recovery path — the parse
fails 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

dom::parser parser;
parser.number_as_string(true);  // opt-in, default false
dom::element doc = parser.parse(json);

for (dom::element value : doc) {
 switch (value.type()) {
   case dom::element_type::BIGINT:
     std::cout << value.get_bigint().value() << std::endl;
     break;
   // ... other types unchanged
 }
}

Tape format: Big integers use tag 'Z' with the same string buffer layout as '"' (4-byte LE length + UTF-8 bytes + null). Reuses on_end_string() for buffer writes — no duplicated logic. Documented in tape.md .

Zero cost when disabled: The check is on the error path only — parse_number returns BIGINT_ERROR, then visit_number checks the flag. The normal number parsing path (SUCCESS) is untouched. No new branches
in the hot path.

Serialization: Big integers are output as raw digits (no quotes), preserving valid JSON.

API

Method Returns Description
element::type() element_type::BIGINT Type identification
element::is_bigint() bool Type check
element::get_bigint() simdjson_result<std::string_view> Raw digit string
get_int64() / get_uint64() / get_double() on BIGINT INCORRECT_TYPE Graceful error
parser.number_as_string(true/false) void Opt-in toggle
parser.number_as_string() bool Query current setting

Changes

File Change
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 Handle BIGINT_ERROR when flag is set
tape_writer.h Add append_bigint helper
element.h Add element_type::BIGINT, get_bigint(), is_bigint()
element-inl.h Implementations + operator<< + simdjson_result wrappers
serialization-inl.h Handle BIGINT tag (raw digits, no quotes)
document-inl.h dump_raw_tape support for BIGINT
tape.md Document big integer tape format
big_integer_tests.cpp 9 tests covering both paths
readme_examples.cpp Add BIGINT case to tree walker
fuzz_dump.cpp Add BIGINT case to fuzzer

Tests

9 tests covering:

  • option_off_by_default — flag is false
  • default_returns_bigint_error — without opt-in, existing behavior preserved
  • opt_in_parses_big_integer — parse, type check, get_bigint(), streaming
  • negative_big_integer — signed big integer with raw digits including -
  • big_integer_in_array — mixed array with big integer
  • serialization_preserves_digits — raw digits present, not quoted
  • normal_numbers_unaffected — int64/uint64/double work with flag enabled
  • type_checks — is_bigint() true, all other is_* false, get_int64/uint64/double/string return INCORRECT_TYPE
  • element_type_output — operator<< prints "bigint"

Performance

No regression — the flag check is only reached on the BIGINT_ERROR path, which is already an error exit. Normal number parsing is unchanged.

Notes

@harshanagd
harshanagd force-pushed the feature/dom-big-integer branch 2 times, most recently from ef92604 to 466a7b2 Compare March 23, 2026 17:44
Comment thread src/generic/stage2/tape_builder.h Outdated
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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (err == BIGINT_ERROR && iter.dom_parser._number_as_string) {
if (simdjson_unlikely(err == BIGINT_ERROR && iter.dom_parser._number_as_string)) {

@lemire

lemire commented Mar 23, 2026

Copy link
Copy Markdown
Member

@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.

@lemire

lemire commented Mar 23, 2026

Copy link
Copy Markdown
Member
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).

@lemire

lemire commented Mar 23, 2026

Copy link
Copy Markdown
Member

@harshanagd You do not have to do it with this PR, but we'll need to document that feature in doc/dom.md.

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
@harshanagd
harshanagd force-pushed the feature/dom-big-integer branch from 466a7b2 to be9aace Compare March 23, 2026 22:09
@harshanagd

Copy link
Copy Markdown
Contributor Author

Thank you for your suggestions. I have actioned them. I have also updated the dom.md documentation along with the PR. Let me know if there is anything more if you spot.

@lemire lemire left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I approve but will let it sit a bit to see if others have comments.

@lemire lemire left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I approve but will give a chance of others to comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handle "large integers"

2 participants