perf: avoid unnecessary copies in ConjugateGraph::Serialize loop#2161
perf: avoid unnecessary copies in ConjugateGraph::Serialize loop#2161LHT129 wants to merge 1 commit into
Conversation
Merge ProtectionsYour pull request matches the following merge protections and will not be merged until they are valid. 🟢 Require kind labelWonderful, this rule succeeded.
🟢 Require version labelWonderful, this rule succeeded.
|
There was a problem hiding this comment.
Code Review
This pull request optimizes the serialization of ConjugateGraph by using const references in the loop to avoid unnecessary copies. The reviewer suggested using C++17 structured bindings to improve readability and maintain consistency with other parts of the codebase.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
There was a problem hiding this comment.
Pull request overview
This PR optimizes ConjugateGraph::Serialize(std::ostream&) by avoiding unnecessary value copies while iterating the internal conjugate_graph_ map, reducing per-node overhead during serialization for large graphs.
Changes:
- Iterate
conjugate_graph_byconst auto&to avoid copying map entries (and associatedshared_ptrrefcount ops). - Bind the dereferenced neighbor set as
const auto&to avoid copying the entireUnorderedSetper node.
…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 <tianlan.lht@antgroup.com> Co-authored-by: opencode <opencode@anthropic.com> Signed-off-by: LHT129 <tianlan.lht@antgroup.com>
0af2cb0 to
3d67ab6
Compare
Closes #2160
Summary
Use
const auto&references inConjugateGraph::Serializeloop to avoid unnecessary copies:auto item→const auto& item(eliminates atomicshared_ptrref-count increment per iteration)auto neighbor_set = *item.second→const auto& neighbor_set = *item.second(eliminates fullUnorderedSetcopy with heap allocation per iteration)For a graph with N nodes, this eliminates N atomic operations and N heap allocations during serialization.
Changes
src/impl/conjugate_graph.cpp: 2-line change, value copies → const referencesTest
All existing
ConjugateGraphunit tests pass (4 test cases, 72 assertions). No behavior change — pure performance optimization.