Repository navigation
Pass small objects in registers - #955
dylan-conway wants to merge 3 commits into
Conversation
|
Warning Review limit reachedYou'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. View limit details
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. |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
🟡 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.
| typedef HashTableAddResult<HashTableAddedEntry<ValueType>> AddResult; | ||
| iterator iteratorAt(HashTableAddedEntry<ValueType> entry) LIFETIME_BOUND { return makeKnownGoodIterator(entry.get()); } |
There was a problem hiding this comment.
🟡 (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.
|
Preview build of 510fa46: |
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,CodeOriginandMacroAssemblerCodeRefhave 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. WithTRIVIAL_ABIthey 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 throughsetOffset(other.offset()). That only matters withJIT_SIGN_ASSEMBLER_BUFFER, where the offset is signed with its own address. Elsewhere they are now defaulted, soAssemblerLabeland 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::addreturns 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::iteratoris now aHashTableAddedEntry, which holds the position only: 16 bytes, two registers. The few callers that need a real iterator (toremove, or to build another iterator) ask the table for it withiteratorAt.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.