add tests for core/list#2
Merged
Merged
Conversation
mizchi
added a commit
to mizchi/core
that referenced
this pull request
May 26, 2026
Three related allocation removals along the JSON number lex chain, all stemming from the same observation: the native target boxes every returned struct / tuple / enum payload by default. ## 1. Drop the `(Double, StringView?)` tuple The number-lex chain (`lex_zero` / `lex_decimal_*` / `lex_number_end` / `lex_integer_end`) returned `(Double, StringView?)`, where the second component is the source-text view that's only ever `Some(_)` on the infinity-overflow path. Native boxes every tuple, so every parsed JSON number cost one heap allocation just to carry a value that was structurally `None`. Side-channel the optional view through a new private `ParseContext.last_number_repr` field. Each leaf return writes it (usually `None`); `lex_main.mbt` reads + clears when constructing the `Number` token. ## 2. Stack-allocate `JsonNumberScan` via `#valtype` `scan_json_number` builds + returns a five-field `JsonNumberScan` struct on every JSON number, used immediately by `lex_number_end` and discarded. Without `#valtype` the native target boxes that struct as a ~32-byte heap object per call. ## 3. NaN sentinel for `try_fast_double` `JsonNumberScan::try_fast_double` returned `Double?` — `Some(d)` on the fast path, `None` to fall back to strconv. The fast path can only produce 0 or a finite `Double` (the `checked_mul` guard rules out infinity), so `NaN` is a free sentinel. Returning a plain `Double` (with NaN meaning "not handled") skips the boxed `Option<Double>` allocation on every JSON number that hits this path. ## Numbers Measured on a native-target alloc profiler over mizchi/pprof-mbt's bench suite (`--sample-rate 100`): | bench | metric | before | after | Δ | |---|---|---|---|---| | `json_numbers` (10 k integers × 30) | allocs | 1 200 200 | 600 200 | **−50 %** | | | bytes | 18.31 MB | 9.16 MB | **−50 %** | | `json_parse` (1 000-obj × 50) | allocs | 3 250 200 | 2 300 200 | **−29 %** | | | bytes | 58.56 MB | 46.93 MB | **−20 %** | (`moonbitlang#3` only fires for non-integer / non-overflow numbers, so it adds 100 k allocs / 681 kB on top of #1+moonbitlang#2 on `json_parse` and nothing on `json_numbers`.) Tests: `moon test --target native -p json` and `moon test --target wasm-gc -p json` both pass (171 / 171 each). `moon fmt --check` clean.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.