Skip to content

Pass small objects in registers - #955

Open
dylan-conway wants to merge 3 commits into
mainfrom
claude/small-objects-in-registers
Open

dylan-conway wants to merge 3 commits into
mainfrom
claude/small-objects-in-registers

Conversation

@dylan-conway

Copy link
Copy Markdown
Member

What does this PR do?

Three changes that let small objects travel in registers, where they were passed and returned through memory. One commit per change.

Bun's linux-x64 release binary, stripped, at 8c6c963: -524,288 B. The estimates below are per change, from the size of the affected code in a sample of object files.

Mark 18 small owning types TRIVIAL_ABI. Ref, RefPtr, String, AtomString, CString, Identifier, HashTable, HashMap, HashSet, BitVector, Box, MallocPtr, WeakPtr, Strong, Weak, CodeOrigin and MacroAssemblerCodeRef have a destructor, so they are passed and returned through memory: the caller makes a temporary, passes its address and destroys it afterwards. None of them points into itself. With TRIVIAL_ABI they travel in registers and an argument is destroyed by the callee, once, instead of at every call site.

This changes the calling convention of every function that takes or returns one of these by value. Bun's own C++ is compiled against the same headers.

Estimate: 0.33 to 0.36 MB.

Make AssemblerLabel trivially copyable where its offset is not signed. AssemblerLabel's copy and move operations go through setOffset(other.offset()). That only matters with JIT_SIGN_ASSEMBLER_BUFFER, where the offset is signed with its own address. Elsewhere they are now defaulted, so AssemblerLabel and what holds one (Jump, Call, Label) are passed and returned in registers.

Estimate: 0.15 to 0.18 MB.

Return HashTable::add's result in registers. HashTable::add returns an iterator and a bool. The iterator is two pointers (position and end of table), so the result is 24 bytes and comes back through memory. Nearly every caller only dereferences it. AddResult::iterator is now a HashTableAddedEntry, which holds the position only: 16 bytes, two registers. The few callers that need a real iterator (to remove, or to build another iterator) ask the table for it with iteratorAt.

Estimate: 45 KB.

Verification

These changes were compiled into a linux-x64 release build of Bun on top of 8c6c963, together with other size changes. Against the same build without them: 58 Bun test files gave the same results. Everything in JSTests/stress (5,949 files, three modes) that passed still passed, apart from 14 files affected by one of the other changes. In JSTests/wasm the same 626 of 797 files passed.

This branch has not been built locally.

@coderabbitai

coderabbitai Bot commented Oct 31, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 35 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 81c36619-52a5-406a-a584-2b7005c1068b
Commits

Reviewing files that changed from the base of the PR and between e669162 and 510fa46.

Files selected for processing: 25
  • Source/JavaScriptCore/aot/object/AOTObjectModel.cpp
  • Source/JavaScriptCore/assembler/AssemblerBuffer.h
  • Source/JavaScriptCore/assembler/MacroAssemblerCodeRef.h
  • Source/JavaScriptCore/b3/B3BasicBlockUtils.h
  • Source/JavaScriptCore/bytecode/CodeOrigin.h
  • Source/JavaScriptCore/heap/Strong.h
  • Source/JavaScriptCore/heap/Weak.h
  • Source/JavaScriptCore/runtime/AbstractModuleRecord.cpp
  • Source/JavaScriptCore/runtime/Identifier.h
  • Source/JavaScriptCore/runtime/JSObject.cpp
  • Source/JavaScriptCore/runtime/SparseArrayValueMap.cpp
  • Source/JavaScriptCore/runtime/SparseArrayValueMap.h
  • Source/WTF/wtf/BitVector.h
  • Source/WTF/wtf/Box.h
  • Source/WTF/wtf/HashMap.h
  • Source/WTF/wtf/HashSet.h
  • Source/WTF/wtf/HashTable.h
  • Source/WTF/wtf/MallocPtr.h
  • Source/WTF/wtf/Ref.h
  • Source/WTF/wtf/RefPtr.h
  • Source/WTF/wtf/WeakHashMap.h
  • Source/WTF/wtf/WeakPtr.h
  • Source/WTF/wtf/text/AtomString.h
  • Source/WTF/wtf/text/CString.h
  • Source/WTF/wtf/text/WTFString.h
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@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 the other things that could break under these changes: the remaining remove(addResult.iterator) call sites are all in WebCore/WebKit, outside the Bun build and on the unchanged #else AddResult; OrderedHashMap/OrderedHashSet, RobinHoodHashTable, ListHashSet and InlineMap keep their own AddResult types so they are unaffected; AssemblerLabel's m_offset is a plain uint32_t when not signed, so the defaulted copies are safe; and CodePtr is tag-diversified rather than address-diversified, so relocating MacroAssemblerCodeRef is fine. One note in the same vein as the HashTable debug-build finding: under ENABLE(REFTRACKER) (ASSERT_ENABLED builds) Strong carries a RefTrackerMixin member (Source/JavaScriptCore/heap/Strong.h:145) that is non-trivial and stores originalThis, so clang will drop the attribute on Strong there too; worth folding into the same fix.

Extended reasoning...

The change adds TRIVIAL_ABI to 18 WTF/JSC value types, defaults AssemblerLabel's copy operations when the offset is not pointer-signed, and shrinks HashTable::AddResult to a one-pointer wrapper plus an iteratorAt() escape hatch, with Bun-specific parts gated behind USE(BUN_JSC_ADDITIONS). It touches no injection, auth, or data-exposure surface, but it changes the C++ calling convention of core types and the PR states it has not been built locally. The inline findings (debug-build attribute drop and loss of stale-iterator checking) plus the ABI breadth mean a human should review; I ruled out the remaining iterator-typed AddResult call sites in the Bun build, the other hash table families, and address-dependent state in AssemblerLabel and CodePtr.


template<typename Key, typename Value, typename Extractor, typename HashFunctions, typename Traits, typename KeyTraits, typename Malloc>
class HashTable {
class TRIVIAL_ABI HashTable {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 nit (optional): in debug builds (NDEBUG undefined) clang drops the new TRIVIAL_ABI on HashTable and prints an ignored-attribute warning in every translation unit that includes HashTable.h. CHECK_HASHTABLE_ITERATORS (HashTable.h:71) adds the member std::unique_ptr m_mutex at HashTable.h:696, which is not trivial for calls, so 'trivial_abi' cannot be applied. HashMap and HashSet then lose it too, since their m_impl is that HashTable. Fix: either gate the attribute on !CHECK_HASHTABLE_ITERATORS (or hold the Lock in a trivial-abi-safe way) so debug and release agree on whether these three types travel in registers, and no warning is emitted.

Why this was flagged

A debug lane (for example -debug-aot in .github/scripts/lanes.mjs) compiles any file that includes Source/WTF/wtf/HashTable.h. Because NDEBUG is not defined, HashTable.h:71 sets CHECK_HASHTABLE_ITERATORS to 1 and HashTable.h:696 declares mutable std::unique_ptr<Lock> m_mutex;. std::unique_ptr is not trivial for calls, so clang's checkIllFormedTrivialABIStruct removes the attribute added at HashTable.h:387 and emits 'trivial_abi' cannot be applied to 'HashTable' under -Wignored-attributes once per translation unit. HashMap (HashMap.h:43) and HashSet (HashSet.h:34) hold that HashTable as m_impl, so their TRIVIAL_ABI is silently dropped as well. On the base branch no attribute exists so there is no warning; after the change debug builds get thousands of warning lines and none of the register-passing the commit intends, while release builds do get it. No existing safeguard suppresses -Wignored-attributes.

Verification: Source/WTF/wtf/HashTable.h:67-73 sets CHECK_HASHTABLE_ITERATORS to 1 when NDEBUG is not defined; HashTable.h:696 declares mutable std::unique_ptr<Lock> m_mutex; in the class marked TRIVIAL_ABI at HashTable.h:387. Clang drops the attribute and emits "'trivial_abi' cannot be applied to 'HashTable'". No -Werror is set in Source/cmake/WebKitCompilerFlags.cmake, so it is warning noise, not a build break.

Comment on lines +398 to +399
typedef HashTableAddResult<HashTableAddedEntry<ValueType>> AddResult;
iterator iteratorAt(HashTableAddedEntry<ValueType> entry) LIFETIME_BOUND { return makeKnownGoodIterator(entry.get()); }

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 (optional) Debug builds lose the stale-iterator assertion for every add() result: using addResult.iterator after the table mutates now silently reads freed or moved memory instead of asserting. Under USE(BUN_JSC_ADDITIONS) AddResult::iterator is a bare pointer wrapper (HashTable.h:398) with no CHECK_HASHTABLE_ITERATORS link, and iteratorAt (HashTable.h:399) mints a fresh valid iterator from any stale pointer, so remove(iteratorAt(stale)) passes too. Fix: keep the one-word entry only where CHECK_HASHTABLE_ITERATORS is 0 (the release lanes that want the size win) and fall back to the #else iterator-based AddResult with identity iteratorAt when it is 1, or give HashTableAddedEntry the table registration under that macro. [also at: Source/WTF/wtf/HashTable.h:333 - Debug builds no longer catch code that keeps an add() result across a later mutation of the same table; such a stale use now silently reads or writes a freed bucket.]

Why this was flagged

Trigger: any build without NDEBUG, where CHECK_HASHTABLE_ITERATORS is 1 (HashTable.h:71), and code of the form auto r = map.add(a, x); map.add(b, y); r.iterator->value = z; or map.remove(map.iteratorAt(r.iterator)) after another mutation. On the base branch AddResult::iterator is HashTable::iterator (HashTable.h:401); every add/remove/rehash calls invalidateIterators(this) (HashTable.h:925, 1106, 1113, 1476), which nulls m_table on all live iterators (HashTable.h:1618), so the later get() hits ASSERT(m_table) in checkValidity (HashTable.h:198, 230) and the bug is caught at the site. After the change, under USE(BUN_JSC_ADDITIONS) the entry is HashTableAddedEntry (HashTable.h:398) whose operator-> and operator* return the raw m_position (HashTable.h:328-329) with no table link, so the stale dereference proceeds silently in debug exactly as in release; and iteratorAt (HashTable.h:399) calls makeKnownGoodIterator(entry.get()) (HashTable.h:656), registering a brand-new iterator on the current table, so HashTable::remove's it == end() validity check also passes for a pointer into a freed or already-deleted bucket.

Verification: After the change, under USE(BUN_JSC_ADDITIONS) HashTableAddedEntry is a bare ValueType* m_position whose get()/operator*/operator-> do no check (HashTable.h:319-332); iteratorAt(entry) (HashTable.h:399) calls makeKnownGoodIterator(entry.get()) (HashTable.h:656), so remove(iteratorAt(stale)) passes the it == end() check. The #else branch retains the check.

@github-actions

Copy link
Copy Markdown

Preview build of 510fa46: autobuild-preview-pr-955-510fa46d

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