Skip to content

fix(object): key prototype transitions by address to break the strong… - #5696

Open
Skyu2231 wants to merge 1 commit into
boa-dev:mainfrom
Skyu2231:fix/issue-5681
Open

Skyu2231 wants to merge 1 commit into
boa-dev:mainfrom
Skyu2231:fix/issue-5681

Conversation

@Skyu2231

Copy link
Copy Markdown

Fixes: #5681

It changes the following:

  • Keys the prototype transition map in ForwardTransition by the prototype's
    address (PrototypeTransitionKey) instead of holding a strong JsPrototype
    key, so the map no longer keeps prototypes alive.
  • Leaves the insertion counter untouched in the opportunistic
    prune_prototype_transitions, so that early cleanup doesn't postpone the
    periodic prune.
  • Adds test_prototype_transition_does_not_keep_prototype_alive,
    test_prototype_transition_is_reused, and makes
    test_prune_prototype_on_counter_limit deterministic (prototypes are kept
    alive while entries are being counted, since dead same-sized allocations can
    reuse an address).

Root cause

ForwardTransition::prototypes used JsPrototype as the key, i.e. a strong
reference to the prototype. A prototype can indirectly reference an object that
uses the transitioned shape, which reaches the shape that owns the map, so the
map closed the cycle and pinned the prototype — and everything reachable from
it — for as long as the owning shape lived. For a long-lived shape (for example
the realm root shape), every prototype ever created against it was retained, so
force_collect got slower with each iteration.

Why an address is a valid key

The map no longer holds a GC reference, but it still identifies prototypes
uniquely: every shape stored under the key strongly references the prototype it
was created for, so an entry that is still alive can only ever match that same
live prototype. The null prototype is represented by address 0, which can
never be the address of an allocation. Dead entries are pruned exactly as
before (bounded, every 255 insertions), which is why the reproducer keeps a
small constant plateau rather than returning to zero.

Testing

Reproducer from the issue (class with Foo.instance = new Foo(), created 1,000
times in one Context, timing force_collect in release mode):

iterations before after
0 0.3 ms 0.33 ms
200 44 ms 40.6 ms
400 89 ms 40.3 ms
600 132 ms 41.3 ms
800 173 ms 49.0 ms
1000 225 ms 42.6 ms
  • cargo test -p boa_engine --lib — 1142 passed, 0 failed
  • cargo test -p boa_engine --tests — 11 passed
  • cargo clippy -p boa_engine --lib -- -D warnings — clean
  • cargo fmt --all -- --check — clean

@Skyu2231
Skyu2231 requested a review from a team as a code owner November 27, 2026 14:54
@github-actions github-actions Bot added the Waiting On Review Waiting on reviews from the maintainers label Nov 27, 2026
@github-actions github-actions Bot added this to the v0.23 milestone Nov 27, 2026
@github-actions github-actions Bot added the C-Tests Issues and PRs related to the tests. label Nov 27, 2026

This branch has not been deployed

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

Labels

C-Tests Issues and PRs related to the tests. Waiting On Review Waiting on reviews from the maintainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prototype transitions keep prototypes alive forever when they can reach their own instances

1 participant