Skip to content

perf: avoid unnecessary copies in ConjugateGraph::Serialize loop#2161

Open
LHT129 wants to merge 1 commit into
antgroup:mainfrom
LHT129:claude-opus/optimize-conjugategraph-serialize-copy
Open

perf: avoid unnecessary copies in ConjugateGraph::Serialize loop#2161
LHT129 wants to merge 1 commit into
antgroup:mainfrom
LHT129:claude-opus/optimize-conjugategraph-serialize-copy

Conversation

@LHT129
Copy link
Copy Markdown
Collaborator

@LHT129 LHT129 commented Jun 8, 2026

Closes #2160

Summary

Use const auto& references in ConjugateGraph::Serialize loop to avoid unnecessary copies:

  • Outer loop: auto itemconst auto& item (eliminates atomic shared_ptr ref-count increment per iteration)
  • Inner: auto neighbor_set = *item.secondconst auto& neighbor_set = *item.second (eliminates full UnorderedSet copy 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 references

Test

All existing ConjugateGraph unit tests pass (4 test cases, 72 assertions). No behavior change — pure performance optimization.

Copilot AI review requested due to automatic review settings June 8, 2026 07:53
@LHT129 LHT129 self-assigned this Jun 8, 2026
@LHT129 LHT129 added kind/improvement Code improvements (variable/function renaming, refactoring, etc. ) version/1.0 labels Jun 8, 2026
@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented Jun 8, 2026

Merge Protections

Your pull request matches the following merge protections and will not be merged until they are valid.

🟢 Require kind label

Wonderful, this rule succeeded.
  • label~=^kind/

🟢 Require version label

Wonderful, this rule succeeded.
  • label~=^version/

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/impl/conjugate_graph.cpp Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_ by const auto& to avoid copying map entries (and associated shared_ptr refcount ops).
  • Bind the dereferenced neighbor set as const auto& to avoid copying the entire UnorderedSet per 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>
@LHT129 LHT129 force-pushed the claude-opus/optimize-conjugategraph-serialize-copy branch from 0af2cb0 to 3d67ab6 Compare June 8, 2026 12:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/improvement Code improvements (variable/function renaming, refactoring, etc. ) size/XS version/1.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: avoid unnecessary copies in ConjugateGraph::Serialize loop

2 participants