Skip to content

Optimize traversing empty map#297

Merged
greg7mdp merged 1 commit intogreg7mdp:masterfrom
Caulm:master
Apr 8, 2026
Merged

Optimize traversing empty map#297
greg7mdp merged 1 commit intogreg7mdp:masterfrom
Caulm:master

Conversation

@Caulm
Copy link
Copy Markdown
Contributor

@Caulm Caulm commented Apr 8, 2026

Eliminate performance overhead in constructing begin() iterators on empty hash tables.

When iterating over an empty phmap::flat_hash_map, begin() unnecessarily called skip_empty_or_deleted() on the first bucket, causing measurable overhead.

This fix aligns with the approach already taken in Abseil: if the map is empty, simply return end(). See raw_hash_set.h#L2355

Benchmark (traversing empty map, capacity 100M, seconds):

std::unordered_map Original phmap::flat_hash_map Fixed phmap::flat_hash_map
0.000 0.047 0.000

Eliminate performance overhead in constructing begin() iterators on empty hash tables.
@greg7mdp
Copy link
Copy Markdown
Owner

greg7mdp commented Apr 8, 2026

Nice, thanks for the PR @Caulm !

@greg7mdp greg7mdp merged commit 01c838b into greg7mdp:master Apr 8, 2026
10 checks passed
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