Skip to content

Fix map::operator[] passing an unusable insertion hint (#592) - #603

Open
devteamaegis wants to merge 1 commit into
electronicarts:masterfrom
devteamaegis:fix/map-operator-bracket-hint
Open

devteamaegis wants to merge 1 commit into
electronicarts:masterfrom
devteamaegis:fix/map-operator-bracket-hint

Conversation

@devteamaegis

Copy link
Copy Markdown

Fixes #592.

map::operator[](const Key&) and operator[](Key&&) pass lower_bound(key) as the insertion hint to rbtree::DoInsertKey. DoGetKeyInsertionPositionUniqueKeysHint expects the hint to be the element before the insertion point (*hint < key < *next(hint)). On a miss, lower_bound is the element after it, so the hint is rejected unless the key goes at end(), and the key is found again with a full tree search.

Change: when the key is missing, pass prev(lower_bound) as the hint. When lower_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 through try_emplace, one search); vector_map::operator[] (its hinted insert uses "insert before" semantics, so lower_bound is already correct there). fixed_map inherits the fix.

Numbers (Apple clang 17, arm64, -O2; standalone program compiled against master and against this branch):

  • Average comparisons per missing-key operator[] on a 100k-element map<int,int>: 38.2 → 20.6 (lower_bound alone: 18.1).
  • Descending fill: 58.5 → 28.7 comparisons per insert.
  • Wall time for 100k scrambled misses after 100k ascending inserts: about −12% with int keys and about −14% with string keys. 200k descending inserts: about −37%.

Test: TestMap.cpp adds a counting comparator and checks that every missing-key operator[] (lvalue and rvalue, scrambled middle keys, new min, new max, descending fill) uses at most a small constant number of comparisons beyond lower_bound. It also checks contents and validate(). 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.

…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

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

Labels

None yet

Projects

None yet

1 participant