Skip to content

[JSC] A transition from a dictionary structure takes its property table instead of copying it - #803

Closed
robobun wants to merge 1 commit into
mainfrom
robobun/a5aa7b8b/dictionary-delete-in-place
Closed

robobun wants to merge 1 commit into
mainfrom
robobun/a5aa7b8b/dictionary-delete-in-place

Conversation

@robobun

@robobun robobun commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

Related to oven-sh/bun#44583

Problem

  • A delete on an object with more than 128 named properties costs O(n) and allocates a table of n entries: delete object.key0; object.key0 = i on 1000 properties runs 2278 ms per 100000 pairs, node 50 to 115 ms.
  • The object has a cacheable dictionary structure with a pinned table. A delete moves it to a new structure, and Structure::takePropertyTableOrCloneIfPinned (Structure.cpp:929) copied the table. Attribute changes, seal, freeze, preventExtensions, prototype changes, private brands and the uncacheable transition copied it too.

Fix

  • Structure::takePropertyTableFrom: every transition gets its table through it. A dictionary's table moves into the transition and is pinned there. Other pinned tables are still copied.
  • Correct because a dictionary structure belongs to one object, and nothing rebuilds a table through a dictionary after the object leaves: a dictionary has no previousID chain.
  • The dictionary is departed then (isDepartedDictionary). The DFG status computations skip it, no PropertyCondition holds or is watchable on it, and the type profiler reads the object's structure instead.
  • Verified: two new tests (the stress test fails on main with allocated 24755 bytes per iteration), about 25000 filtered run-javascriptcore-tests runs locally with the same failures as main, and the CI test lanes (every mode of the runner).

Background

  • Most structures share one property table along a chain of transitions and rebuild it when the GC clears it. A dictionary owns a pinned table and has no chain. A cacheable dictionary keeps inline caches valid by changing structure on a delete. A status computation is how the DFG reads those caches.
  • Considered converting the object to an uncacheable dictionary on the first delete (V8's dictionary mode, 50 ms). It needs a new structure bit to spare the global object, and after one flatten a second delete leaves every read on the generic path for good.

Downsides

  • Each delete on a dictionary still allocates a Structure cell: 160 bytes, 0.8 us per delete plus re-add. Objects with 100 properties are unchanged: 265 ms on main, 264 ms here per 1000000 pairs (noise about 20%).
  • Inline-cached reads after deletes, bare global reads after delete globalThis.x, and a read site the DFG compiles after a delete: same as main (Notes).
  • Text grows by 348 bytes over six object files (size). The common transition path gets one isDictionary() test.
Notes

Measurements. Release jsc shell on Linux x64, this branch against its base 5d2ceba, interleaved, 3 to 5 runs each, medians. Times from $vm.monotonicTimeNow(), bytes from $vm.heapTotalBytesAllocated(), which counts a 16 KB block when it is taken, so it is an average over many iterations.

loop main this PR
delete + re-add, 1000 properties, 100000 pairs 2278 ms 78 ms
same, JIT off 3471 ms 84 ms
delete + re-add, 2000 properties, 100000 pairs 4987 ms 105 ms
delete + re-add, 100 properties, 1000000 pairs 265 ms 264 ms
bytes allocated per delete + re-add pair, 1000 properties 25090 160
bytes allocated per add + attribute change pair, 1000 properties 46051 570
10 million reads through an IC, 200 properties, after 1 delete 77 ms 78 ms
same, after 2 deletes 69 ms 82 ms (both 51 to 87)
10 million bare global reads after delete globalThis.x 32 ms 38 ms (both 32 to 49)
10 million reads, site cached before the delete, DFG compiled after it 81 ms 83 ms

process.env. In Bun, process.env with more than 128 variables is such an object: one table per delete process.env.X with 236 variables, covered by the Bun test.

Why the per-delete cost was O(n). Structure::Structure(VM&, StructureVariant, Structure* previous) copies the dictionary kind, so the structure that removeNewPropertyTransition creates from a cacheable dictionary is a cacheable dictionary too, unpinned, with previousID set to the old one. The add that follows pins it in place. The next delete finds a pinned table and copies it again. So a delete plus re-add loop copies the table once per pair, and the GC frees one table per pair (the reporter's PropertyTable::destroy samples). Attribute changes with no add in between take the unpinned table along a growing chain and do not copy, which is why the sibling case is an attribute change after an add.

Rebase. The branch was first based on 5d2ceba and measured there. It is now rebased onto main after the upstream merge (#814); the patch applied without conflicts. The first CI run failed the new stress test in the eager-JIT, no-LLInt and collect-continuously modes: the compilers' own allocations inside the measured loops, and the delete inline cache flattening the dictionary before the chain check. The test now takes the least of several rounds, deletes with Reflect.deleteProperty in the chain check, and sizes its loops with testLoopCount. A departed structure in a PutByIdReplace cache now yields the slow status instead of NoInformation, which the DFG reads as a site that never ran (review comment).

Self-review. A review of the diff raised 8 concerns, all addressed: the transitions that pinned their table with pin() on main (seal, freeze, preventExtensions, prototype, brand, dictionary kind) cut the chain to the dictionary again, so a delete chain is released at those transitions as before ($vm.getStructureTransitionList length 1, checked in the stress test); PropertyCondition::isWatchableWhenValid no longer asserts on a property that a departed dictionary lost between its two unlocked reads on a compiler thread; the stress test covers every transition kind, the copyPropertyTableForPinning path (setPrototypeOf, $vm.toCacheableDictionary) and the one-shot transitions on fresh objects; the type profiler reads a departed structure's shape from the object, with a test; process.env is named above and covered by the Bun test.

Who could read a departed dictionary's table. GetByStatus, PutByStatus, InByStatus and ComplexGetStatus call getConcurrently on structures from inline caches and bytecode metadata: they now skip a departed structure, and without the skip they returned the slow variant for the whole access. PropertyCondition::isStillValid and the adaptive watchpoints read object()->structure(), the live one, but a compiler thread holds that pointer across the two reads of isWatchable, and the object can leave the structure in between: the second read then finds no property and the condition is not watchable, where it asserted before. The RELEASE_ASSERT(isWatchable) callers in DFGAdaptiveStructureWatchpoint and LLIntPrototypeLoadAdaptiveStructureWatchpoint run on the main thread with the live structure. TypeProfilerLog::processLogEntries reads a logged structure later on the main thread through forEachPropertyConcurrently, which does not materialize: a departed one gave an empty shape, now it uses the object's structure. The structureEnsuresValidity callers in the DFG pass structures with Concurrency::ConcurrentThread, which never materializes. ObjectPropertyConditionSet generation, presenceConditionIfConsistent and tryGetConstantProperty read live structures. The inline cache compiler calls startWatchingPropertyForReplacements with an offset, not a name. tryCacheDeleteBy and tryCachePutBy return before any table read when the old structure is a dictionary. checkOffsetConsistency runs in release builds and would crash on a table rebuilt from a dictionary with no table, so the stress run above is also a check that nothing does.

Differential runs against the base build, JIT on and off, identical output: delete and re-add with reads and collections on objects of 10 to 5000 properties, defineProperty attribute flips mixed with adds and deletes, setPrototypeOf, seal, freeze and preventExtensions from a dictionary, symbol keys and accessors, delete globalThis.x in a loop with built-in reads, inline-cached reads in optimized code across deletes, deleting every property then re-adding, private brands on a dictionary, for-in with deletes and attribute changes in the loop body, spread, Object.assign, Reflect.ownKeys, JSON.stringify.

Review. A design review weighed three places for the fix: the two sites of the symptom (delete and attribute change), the dictionary policy (convert to an uncacheable dictionary on the first delete), and the helper every transition gets its table through. It picked the helper, with the departed marker for the status layer. A self-review of the diff follows in the PR.

@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Preview build of 6297c8b: autobuild-preview-pr-803-6297c8be

@coderabbitai

coderabbitai Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Important

Review skipped

We couldn't safely recover the incremental review. No full review was started, and the last reviewed checkpoint was preserved. Retry later, or explicitly request a full review by commenting @coderabbitai full review.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 07d3a7c5-73d4-43dc-be0d-4688512a1d1b
📥 Commits

Reviewing files that changed from the base of the PR and between 4b724e9 and b095a24.

📒 Files selected for processing: 10
  • JSTests/stress/dictionary-transition-takes-the-property-table.js
  • JSTests/typeProfiler/dictionary-mode-after-delete.js
  • Source/JavaScriptCore/bytecode/ComplexGetStatus.cpp
  • Source/JavaScriptCore/bytecode/GetByStatus.cpp
  • Source/JavaScriptCore/bytecode/InByStatus.cpp
  • Source/JavaScriptCore/bytecode/PropertyCondition.cpp
  • Source/JavaScriptCore/bytecode/PutByStatus.cpp
  • Source/JavaScriptCore/runtime/Structure.cpp
  • Source/JavaScriptCore/runtime/Structure.h
  • Source/JavaScriptCore/runtime/TypeProfilerLog.cpp

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

Walkthrough

Structure transitions now transfer property tables through dictionary-specific handling. Bytecode status checks and type-profiler logging account for departed dictionaries. New tests cover transition allocation, property behavior, inline caches, and type profiling after deletion.

Changes

Dictionary Property-Table Transitions

Layer / File(s) Summary
Property-table transfer
Source/JavaScriptCore/runtime/Structure.h, Source/JavaScriptCore/runtime/Structure.cpp
Structure transitions now use transfer methods that handle dictionary predecessors separately. Dictionary tables are taken and adopted as pinned tables. The existing transition paths use the new transfer method.
Departed-dictionary handling
Source/JavaScriptCore/bytecode/*Status.cpp, Source/JavaScriptCore/bytecode/PropertyCondition.cpp, Source/JavaScriptCore/runtime/TypeProfilerLog.cpp
Bytecode status checks skip or return no information for departed dictionaries in specified paths. Property-condition checks reject departed dictionaries and return false for missing offsets in two watchability cases. Type-profiler logging uses a cell’s current structure when its logged structure is a departed dictionary.
Transition and profiling tests
JSTests/stress/dictionary-transition-takes-the-property-table.js, JSTests/typeProfiler/dictionary-mode-after-delete.js
Stress tests check allocation bounds, property behavior across transitions and garbage collection, transition-chain lengths, and inline-cache operations. The type-profiler test checks logged structures before and after deletion.

Priority: ➖ Normal

Merge Risk: ⚪ Minimal · up to b095a

No confirmed issue currently blocks merging. The concurrent-reader question remains unverified.

Pre-merge checks: Passed 4
Check name Status Explanation
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Title check Passed The title is concise, specific, and accurately describes the main change: transitions from dictionary structures now take the property table instead of copying it.
Description check Passed The description is detailed, relevant, and explains the problem, fix, design rationale, testing, performance results, and scope. It does not follow the repository template exactly because it omits a B…
Full details: Description check

Explanation

The description is detailed, relevant, and explains the problem, fix, design rationale, testing, performance results, and scope. It does not follow the repository template exactly because it omits a Bugzilla URL, the Reviewed by line, and an explicit changed-file list, but the substantive information is complete.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Git: CodeRabbit could not clone the repository, so clone-backed analysis was skipped and this review may be incomplete. Verify repository clone access, such as SSH credentials, before requesting another full review. If clone access is intentionally unavailable, use path_filters to narrow the review scope.

Comment @coderabbitai help to get the list of available commands.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Beyond the inline findings, I also checked two things in Source/JavaScriptCore/runtime/Structure.cpp and ruled them out: the hand-off in takePropertyTableFrom (the dictionary drops its table and the transition pins it with no allocation in between, the same window the pre-existing unpinned move already had), and whether isDepartedDictionary() can be true for a live structure (every path that creates a dictionary-kind structure pins its table before the structure is published, and the GC only clears unpinned tables, so an unpinned dictionary can only be one whose table was taken).

Extended reasoning...

The change rewires how every Structure transition in JSC obtains its PropertyTable, steals the table from dictionary structures, and teaches the DFG status computations, PropertyCondition and the type profiler to skip the resulting departed dictionaries; it touches no security-sensitive surface such as auth or injection. It is core engine internals with concurrency and GC interactions, so the inline findings plus a human read are warranted; this note only records what else was examined.

@robobun
robobun force-pushed the robobun/a5aa7b8b/dictionary-delete-in-place branch 2 times, most recently from cf96a63 to 6297c8b Compare October 17, 2026 18:22
robobun added a commit to oven-sh/bun that referenced this pull request Oct 17, 2026
…rty table instead of copying it

A delete, an attribute change, a prototype change, seal, freeze or a
private brand on an object with more than 128 named properties copied the
object's property table. The loop of #44583 (delete and re-add one key
of a 1000 key object) ran about 100 times slower than node.

WEBKIT_VERSION points at the preview build of oven-sh/WebKit#803.
Comment on lines 937 to +947
{
// This must always return a property table. It can't return null.
ASSERT(isDictionary());
ASSERT(isPinnedPropertyTable());
PropertyTable* result = propertyTableOrNull();
if (result) {
if (isPinnedPropertyTable())
return result->copy(vm, result->size() + 1);
ASSERT(result);
ConcurrentJSLocker locker(m_lock);
setIsPinnedPropertyTable(false);
setPropertyTable(vm, nullptr);
return result;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 JSON.stringify of a large object can now read a freed property-name string when a toJSON, getter or replacer deletes and adds properties on that object. Stringifier::Holder keeps raw PropertyName keys from the dictionary's table (JSONObject.cpp:550), and takePropertyTableFromDictionary (Structure.cpp:937-947) now moves that table into the delete transition, whose remove() derefs the key; the following add pins and clears m_transitionPropertyName, freeing it. Fix: a departed dictionary must not let its former keys die while raw-key holders exist, e.g. keep copying when the structure may be observed, or have holders ref the keys they copy out of a table.

Why this was flagged

JSON.stringify(o) where o has over 128 computed-key properties (a cacheable dictionary structure D), the temporary key JSStrings have been collected, and one property value has a toJSON (or o has a getter, or a replacer is used) that runs delete o[k]; o.z = 1 for a not-yet-visited key k.…

Verification: normal — security-relevant (use-after-free of a UniquedStringImpl reachable from plain JS). Trigger: JSON.stringify of an object whose structure D is a cacheable dictionary (>128 properties), where a toJSON/getter/replacer runs delete o[k]; o.z = 1 with a computed key k for a not-yet-visited property, and a GC runs before k is visited (collecting the temp JSString that holds the atom).

…le instead of copying it

A dictionary structure belongs to one object. When that object deletes a
property, changes an attribute, is sealed, frozen, made non-extensible,
gets a new prototype or a private brand, it moves to a new structure.
Structure::takePropertyTableOrCloneIfPinned and copyPropertyTableForPinning
copied the dictionary's pinned table for that transition, so each of these
operations cost O(n) in the number of properties and allocated a table of
n entries. On an object with 1000 properties, delete plus re-add ran about
25 us and allocated 25 KB per pair, 60 to 130 times slower than V8.

Nothing needs a dictionary's table after its object leaves: no structure
rebuilds a table through a dictionary, as a dictionary has no previousID
chain of its own. So Structure::takePropertyTableFrom, which every
transition now gets its table through, takes the table and pins it in the
transition, as the transition cannot rebuild it either. A delete or an
attribute change keeps its previousID and name, so that it is still
listed and counted; the other transitions cut the chain with pin() as
they did before.

The dictionary is departed after that (Structure::isDepartedDictionary):
no object has it and it has no table. An inline cache, a profile or the
type profiler log can still hold it. The status computations of the DFG
skip such a structure instead of taking the slow path for the whole
access, which keeps the compiled code for a site that saw the object
before a delete as it was. No property condition holds on it, and a
condition that found the property before the dictionary departed is not
watchable on it. The type profiler reads the shape from the object's
structure instead.

Delete plus re-add on a 1000 property object: 2278 ms to 78 ms per 100000
pairs, 25090 to 160 bytes allocated per pair (the Structure cell).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Assignees

Couldn't load assignees.