Replace hash_gid std::stringstream with FNV-1a - #1028
Conversation
hash_gid runs on the per-message (SubscriptionData::add_new_message) and per-request (ServiceData::take_request/send_response) hot paths and allocated a std::stringstream on every call. Replace with FNV-1a over the raw GID bytes, which is allocation-free. The return value is used only as an in-memory map key, so the algorithm is not part of any wire or persisted format. Signed-off-by: Zachery Aaron Shores-Chmielewski <zacheryasc@gmail.com>
|
Tick the box to add this pull request to the merge queue (same as
|
|
@zacheryasc thanks for the PR. Lastly kindly update the PR description to follow our template and specifically acknowledge AI usage if any. |
I will set this to a draft, to be revisited if and or when a decision is found.
I filled the template that was given when I opened the PR. Is there a better template? Where? AI was used for git archaeology, however code changes are my own. |
Description
hash_gidhashes a 16-byte GID (RMW_GID_STORAGE_SIZE) into asize_tmapkey. It runs on rmw_zenoh's hot paths:
SubscriptionData::add_new_message— every message receivedServiceData::take_request/send_response— every request / responseThe current implementation builds a
std::stringstream(withstd::hex) onevery call, heap-allocating a stream buffer and a
std::stringperinvocation. This replaces it with FNV-1a over the raw GID bytes, which is
allocation-free.
The returned value is used only as an in-memory map key (
Entity::gid_hash_and the
gid_hash()-keyed event/discovery maps); it is never serialized intoattachments, key expressions, or config. The algorithm is therefore not part
of any wire or persisted format, so swapping it is externally invisible.
Related
Narrower alternative to #422 ("Replace
hash_gidwith FNV-1a"), which hasbeen open but not marked ready-for-review since Jan 2025. #422 additionally
rekeys the two message-path maps from
size_tto aGidalias andspecializes
std::hash<std::array<uint8_t, RMW_GID_STORAGE_SIZE>>. This changekeeps the maps keyed on
size_t(minimal blast radius) and avoids thatspecialization, which is formally disallowed by
[namespace.std]— a standardtemplate may only be specialized when it depends on a program-defined type, and
std::array<uint8_t, N>does not.Performance
Per-call heap allocations on the message/request hot paths are eliminated
(
2 -> 0, deterministic and machine-independent).How was it tested
(
ros-rolling-zenoh-cpp-vendor).colcon test --packages-select test_rmw_zenoh_cpp: 15/15 pass, includingcpplint and uncrustify.
rmw_zenoh: aros2talker/listener (messagereceived) and an
add_two_intsservice call (sum=42), exercising bothchanged call sites through
hash_gid.Checklist