From 3d67ab69287829fe4a9ae605124694e860503c40 Mon Sep 17 00:00:00 2001 From: LHT129 Date: Mon, 8 Jun 2026 15:50:31 +0800 Subject: [PATCH] perf: use const reference in ConjugateGraph::Serialize loop to avoid unnecessary copies Replace value copy with const reference for both the map entry and the dereferenced UnorderedSet in the Serialize loop. This eliminates N atomic shared_ptr increments and N full UnorderedSet copies when serializing a conjugate graph with N nodes. Signed-off-by: tianlan.lht Co-authored-by: opencode Signed-off-by: LHT129 --- src/impl/conjugate_graph.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/impl/conjugate_graph.cpp b/src/impl/conjugate_graph.cpp index e2f4cf0fdb..dc638d4725 100644 --- a/src/impl/conjugate_graph.cpp +++ b/src/impl/conjugate_graph.cpp @@ -155,11 +155,11 @@ tl::expected ConjugateGraph::Serialize(std::ostream& out_stream) const { out_stream.write((char*)&memory_usage_, sizeof(memory_usage_)); - for (auto item : conjugate_graph_) { - auto neighbor_set = *item.second; + for (const auto& [tag_id, neighbor_ptr] : conjugate_graph_) { + const auto& neighbor_set = *neighbor_ptr; uint64_t neighbor_set_size = neighbor_set.size(); - out_stream.write((char*)&item.first, sizeof(item.first)); + out_stream.write((char*)&tag_id, sizeof(tag_id)); out_stream.write((char*)&neighbor_set_size, sizeof(neighbor_set_size)); for (auto neighbor_tag_id : neighbor_set) {