Skip to content

Optimize hot paths of encoding and decoding - #63

Open
letko-dmitry wants to merge 20 commits into
almazrafi:mainfrom
letko-dmitry:feature/local-optimizations
Open

letko-dmitry wants to merge 20 commits into
almazrafi:mainfrom
letko-dmitry:feature/local-optimizations

Conversation

@letko-dmitry

@letko-dmitry letko-dmitry commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Builds on #62: only the last 8 commits are new. Each change is a separate commit, measured on its own, so any of them can be dropped.

Encoding and decoding get about 15% faster, decoding Foundation dictionaries 19%. Behavior doesn't change.

  • Small functions that run for every value are inlined, and stored arguments are consumed rather than copied (−1%).
  • No more linear key scan in contains, indirect boxes and a temporary array per coding path (−12% encoding, −6% decoding).
  • The keyed decoding container is final, as nothing subclasses it (−1.6% decoding).
  • NSNull is checked by type, so values aren't bridged to Objective-C just to find out they aren't null (−5% decoding).
  • With the default float strategy, numbers aren't cast to String first (−1…2% decoding).
  • Unkeyed containers reserve room for two elements, which saves a reallocation in most of them (−2.6% encoding).
  • decodeIfPresent looks a key up once instead of three times (−0.5% decoding).
  • Foundation numbers, strings and data are bridged directly instead of through dynamic casts (−6% decoding Foundation dictionaries).

Measured in retired instructions with alternating A/B runs, release builds on an M4; noise is within ±0.3%.

@letko-dmitry
letko-dmitry marked this pull request as ready for review October 5, 2026 18:44
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