Skip to content

Comments

chore(flat): replace std::map with std::unordered_map for key_id_mapping#153

Open
longway-code wants to merge 1 commit intoalibaba:mainfrom
longway-code:chore/flat-key-id-mapping-unordered-map
Open

chore(flat): replace std::map with std::unordered_map for key_id_mapping#153
longway-code wants to merge 1 commit intoalibaba:mainfrom
longway-code:chore/flat-key-id-mapping-unordered-map

Conversation

@longway-code
Copy link

key_id_mapping_ is only used for point lookups — no range scans, no ordering needed. std::unordered_map is a better fit.

  • get_id() lookup: O(log N) → O(1)
  • Index load: O(N log N) → O(N)
  • Memory: slightly lower — three tree pointers (parent/left/right) per node replaced by one next pointer plus a bucket array; net ~12MB savings at N=1M

Microbenchmark — isolated map::find vs unordered_map::find, random key order, all hits:

N std::map std::unordered_map speedup
10k 57.3 ns/op 7.0 ns/op 8.2x
100k 88.3 ns/op 10.6 ns/op 8.3x
1M 138.5 ns/op 15.1 ns/op 9.2x

…ing_

Reduces key lookup in get_id() from O(log N) to O(1) average, and index
load time from O(N log N) to O(N). Also lowers per-entry memory usage by
eliminating the three tree-node pointers (parent/left/right).
@longway-code longway-code marked this pull request as ready for review February 20, 2026 11:29
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.

2 participants