Skip to content

perf: Avoid constructor setup during resolve#2211

Merged
dcodeIO merged 1 commit intomasterfrom
patch/eager-resolution-perf
May 1, 2026
Merged

perf: Avoid constructor setup during resolve#2211
dcodeIO merged 1 commit intomasterfrom
patch/eager-resolution-perf

Conversation

@dcodeIO
Copy link
Copy Markdown
Member

@dcodeIO dcodeIO commented Apr 30, 2026

Resolving fields currently reads parent.ctor to install prototype defaults. Since ctor is a lazy getter, this also generates a runtime message constructor for every resolved type. That constructor setup then resolves the same fields again.

This PR separates reflection resolution from runtime constructor initialization:

  • Field#resolve updates prototype defaults only for constructors that already exist, avoiding the lazy ctor getter.
  • Type#ctor initializes prototype defaults when a constructor is actually created or registered.
  • wrapper setup reuses the type's runtime ctor instead of creating a second one.
  • additionally, recursive feature resolution is skipped when the feature tree is already clean.

On a synthetic benchmark with many cross-type references, this removes one redundant field-resolution pass and avoids generating constructors during schema loading, improving fromJSON by roughly 40-50% depending on schema size.

For 1500 types x 4 refs, the median local fromJSON time went from 9.150ms to 4.855ms. The relevant call counts changed from 12000 to 6000 Field#resolve calls and from 1500 generated constructors during load to 0.

So instead of rolling back eager resolution as proposed in #2157, this keeps the current eager semantics while reducing redundant work.

Follow-up to #2066 and #2068
Addresses #2123

@dcodeIO dcodeIO changed the title perf: Avoid constructor setup during eager resolve perf: Avoid constructor setup during resolve May 1, 2026
@dcodeIO dcodeIO merged commit 1bf3c07 into master May 1, 2026
4 checks passed
@github-actions github-actions Bot mentioned this pull request Apr 30, 2026
@dcodeIO dcodeIO deleted the patch/eager-resolution-perf branch May 2, 2026 14:02
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.

2 participants