Skip to content

DescriptorPool name lookup with HashMap instead of BTreeMap #809

Open

Description

@anuraaga

I was experimenting with resolving descriptors by name from DescriptorPool (for context, it is prototyping C++ message validation with buffa, so there is no direct handle to a descriptor and needs a string lookup) and noticed relatively poor performance for the base case lookup up a message index since it uses BTreeMap.

by_name: BTreeMap<String, Definition>,

I can't think of a reason for this to be ordered, both because ordered Vecs are also stored but also that the map combines all types, which by nature can't really be ordered anymore.

Would it make sense to switch these to a HashMap? As a workaround I can layer my own cache on top but it seems like this change could be broadly useful.

Activity

  1. harshitwandhare commented on Nov 11, 2026

    @harshitwandhare
    Contributor

    I measured this on main at 1047169. Pool of 5,000 messages across 50 files with realistic names (com.example.service17.v1.RequestMessage42), message_by_name over every name 400 times, release build, Linux x86_64, three runs each:

    by_name and the other name maps ns per lookup
    BTreeMap (main) 124, 130, 177
    buffa::Map (hashbrown + foldhash) 27, 30, 33

    Pool build time was the same within noise (5-12 ms either way).

    no_std doesn't block the switch. buffa::Map is already hashbrown::HashMap there (and std's HashMap with foldhash otherwise), so buffa-descriptor needs no new dependency. cargo check -p buffa-descriptor --no-default-features --features reflect passes with it.

    Which maps can change:

    • by_name, symbols, service_by_name, extension_by_name, file_by_name and symbol_file are only used for point lookups and inserts. The one iteration is file_name_of, a find_map over unique indices, so order doesn't matter there.
    • extension_by_extendee has to stay ordered. extensions_of does a range scan on it and documents field-number order.

    With those six swapped, cargo test -p buffa-descriptor --features reflect gives the same result as main. The visible side effect is that {:?} on a DescriptorPool no longer prints those maps in a stable order.

    @iainmcgin if you're happy with that, I can send the six-line change with a test. @anuraaga if you'd rather send it yourself, go ahead.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions