Skip to content

Use std::less<> for Labels to enable heterogeneous lookup on C++14+#750

Open
aeroyorch wants to merge 1 commit into
jupp0r:masterfrom
aeroyorch:labels-transparent-comparator
Open

Use std::less<> for Labels to enable heterogeneous lookup on C++14+#750
aeroyorch wants to merge 1 commit into
jupp0r:masterfrom
aeroyorch:labels-transparent-comparator

Conversation

@aeroyorch

Copy link
Copy Markdown

No description provided.

@aeroyorch aeroyorch force-pushed the labels-transparent-comparator branch 2 times, most recently from 859b829 to 5947bd0 Compare June 4, 2026 19:58
@gjasny

gjasny commented Jun 20, 2026

Copy link
Copy Markdown
Collaborator

Hello,

do you have a benchmark that shows improvements with this change? I'm a little bit hesitant to make the public ABI dependent on __cpp_lib_generic_associative_lookup. If the change is worth it, I'd raise the required C++ standard to 14 (or newer).

But most of the comparisons happen with Labels and thus with std::string.

Thanks,
Gregor

@aeroyorch

Copy link
Copy Markdown
Author

Hello,

do you have a benchmark that shows improvements with this change? I'm a little bit hesitant to make the public ABI dependent on __cpp_lib_generic_associative_lookup. If the change is worth it, I'd raise the required C++ standard to 14 (or newer).

But most of the comparisons happen with Labels and thus with std::string.

Thanks,
Gregor

Ok, I'll try to put together a benchmark next week. I don't expect a big improvement inside the library itself, but it could help users who use the Labels alias directly, especially when doing lookups with std::string_view or string literals.

@aeroyorch aeroyorch force-pushed the labels-transparent-comparator branch from 5947bd0 to d207beb Compare June 25, 2026 22:11
@aeroyorch

Copy link
Copy Markdown
Author

Added a benchmark (core/benchmarks/labels_bench.cc), plain std::map vs std::less<>, looked up with a std::string_view. g++ 13, release, medians:

entries / key len plain (temp string) std::less<>
4 / 8 24.5 ns 17.6 ns
16 / 8 30.3 ns 27.0 ns
4 / 32 38.5 ns 17.1 ns
16 / 32 44.1 ns 24.0 ns

So roughly 1.1 to 1.4x faster for short keys, and about 2x for keys past the SSO size, where the temporary std::string also heap-allocates (1 alloc/lookup vs 0). With a std::string key the two are identical within noise, so there is no regression on the path the library itself uses.

No win inside the library then, but a real one for users looking up Labels with a string_view or literal.

@aeroyorch aeroyorch force-pushed the labels-transparent-comparator branch from d207beb to 7729398 Compare June 25, 2026 22:17
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