Repository navigation
Fix map::operator[] passing an unusable insertion hint (#592) - #603
Open
devteamaegis wants to merge 1 commit into
Open
devteamaegis wants to merge 1 commit into
devteamaegis wants to merge 1 commit into
Conversation
…s#592) Both non-template map::operator[] overloads passed lower_bound(key) as the hint to rbtree::DoInsertKey(true_type, position, key). The rbtree hint must be the element *before* the insertion point (DoGetKeyInsertionPosition- UniqueKeysHint requires *position < key < *next(position)), but lower_bound returns the element after it. The hint was therefore rejected on every miss that wasn't at end(), and the key was located with a second full tree search. Pass the predecessor of lower_bound instead. When lower_bound is begin() (including the empty map), the new node is inserted directly as the left child of the leftmost node, which is always a valid position. On a 100k-element map<int,int>, average comparisons per missing-key operator[] drop from 38.2 to 20.6 (lower_bound alone is 18.1), and filling a map in descending order drops from 58.5 to 28.7 comparisons per insert. Adds a test that counts comparator calls to verify operator[] (lvalue and rvalue keys) only does a constant amount of work beyond lower_bound.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #592.
map::operator[](const Key&)andoperator[](Key&&)passlower_bound(key)as the insertion hint torbtree::DoInsertKey.DoGetKeyInsertionPositionUniqueKeysHintexpects the hint to be the element before the insertion point (*hint < key < *next(hint)). On a miss,lower_boundis the element after it, so the hint is rejected unless the key goes atend(), and the key is found again with a full tree search.Change: when the key is missing, pass
prev(lower_bound)as the hint. Whenlower_bound == begin()(including an empty map), insert directly as the left child of the leftmost node, which has no left child, so no extra comparisons are needed. The existing hint validation (EASTL_VALIDATE_COMPARE) still runs on the predecessor path.Not affected: the template
operator[](KX&&)(it goes throughtry_emplace, one search);vector_map::operator[](its hinted insert uses "insert before" semantics, solower_boundis already correct there).fixed_mapinherits the fix.Numbers (Apple clang 17, arm64, -O2; standalone program compiled against master and against this branch):
operator[]on a 100k-elementmap<int,int>: 38.2 → 20.6 (lower_boundalone: 18.1).Test:
TestMap.cppadds a counting comparator and checks that every missing-keyoperator[](lvalue and rvalue, scrambled middle keys, new min, new max, descending fill) uses at most a small constant number of comparisons beyondlower_bound. It also checks contents andvalidate(). The test fails on master (3 failures) and passes with the fix. The full suite passes in Release and Debug.Testing note: I ran the suite with Apple clang targeting x86_64. On arm64 macOS the test suite doesn't build on master because of #584 (128-bit atomics); the changed files compile cleanly there.
This was investigated and written with an AI coding agent (Claude) working on my behalf.