Skip to content

Avoid property-name allocations in fast-path JSON readers - #554

Merged
eiriktsarpalis merged 1 commit into
mainfrom
eiriktsarpalis-fast-path-property-dispatch
Oct 28, 2026
Merged

eiriktsarpalis merged 1 commit into
mainfrom
eiriktsarpalis-fast-path-property-dispatch

Conversation

@eiriktsarpalis

Copy link
Copy Markdown
Owner

Generated object readers currently allocate a string for every JSON property name before dispatching. This removes those temporary allocations by matching unescaped UTF-8 spans directly.

Approach

  • Borrow contiguous, unescaped reader input; use Utf8JsonReader.CopyString() with stack or pooled buffers for escaped and segmented names, preserving invalid-text validation.
  • Generate length and discriminating-byte patterns followed by UTF-8 literal SequenceEqual checks. Full byte-list patterns did not receive equivalent wide-comparison optimizations in the inspected ARM64 code.
  • Omit per-reader rental state when every known name's maximum escaped representation fits the stack buffer. Oversized unknown names still undergo validation.
  • Add regression coverage for escaping, Unicode, segmented input, invalid text, allocations, and stack-buffer boundaries; refresh Debug and Release snapshots.

Performance

Refresh all 99 measurements, eight charts, and the exported dataset in the performance documentation. Fast-path allocations fall from 576 B to 440 B for the small POCO, from 3.06 KiB to 2.02 KiB for the large POCO, and from 10.80 MiB to 5.73 MiB for the CITM batch.

Separate lookup experiments favored generated UTF-8 dispatch over full byte-list patterns and frozen character/byte alternate lookups for the measured four- and 24-property schemas. Scratch benchmarks are not included in this PR.

Validation

  • Full make test: CLR and Native AOT suites passed.
  • Debug and Release generated-source snapshots passed.
  • All nine JSON fixtures passed serializer-output and round-trip validation; all 99 benchmarks completed using six warmups and twelve measurement iterations.
  • Chart-generation checks and git diff --check passed.

Dispatch on unescaped UTF-8 spans with length and byte discriminators, preserving validation and using stack or pooled buffers for escaped and segmented names. Add regression coverage and refresh the full performance dataset and charts.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@eiriktsarpalis
eiriktsarpalis merged commit 8f1116a into main Oct 28, 2026
7 checks passed
@eiriktsarpalis
eiriktsarpalis deleted the eiriktsarpalis-fast-path-property-dispatch branch October 28, 2026 00:11
@codecov

codecov Bot commented Oct 28, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.72727% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.47%. Comparing base (429bae8) to head (3f8a4a1).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
.../PolyType.FastPathJson/Converters/ScalarHelpers.cs 66.66% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #554      +/-   ##
==========================================
+ Coverage   94.46%   94.47%   +0.01%     
==========================================
  Files         299      299              
  Lines       19556    19637      +81     
  Branches     3820     3797      -23     
==========================================
+ Hits        18473    18553      +80     
  Misses        795      795              
- Partials      288      289       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

1 participant