Skip to content

fix: reserve CSR storage when building from sorted edges - #671

Merged
Becheler merged 2 commits into
boostorg:developfrom
Becheler:fix/csr-sorted-reserve
Oct 28, 2026
Merged

Becheler merged 2 commits into
boostorg:developfrom
Becheler:fix/csr-sorted-reserve

Conversation

@Becheler

@Becheler Becheler commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

Building a CSR graph from sorted edges without numedges used to reserve the edge storage anyway when the range is random access, since the count is then available in O(1).

Regression: commit 50bc92b (2009, "Refactored CSR graph code to get ready for bidirectional support") moved that count from the constructors into assign_from_sorted_edges and broke the reservation in both overloads. The overload without edge properties never computes the count. The overload with edge properties computes it and then still reserves the caller's zero. So, the column vector, and the edge property vector in the second overload grow by repeated reallocation. They end with capacity up to about 2x the edge count, and in the worst case peak at about 3x during the last reallocation. Nobody noticed because reserve does not change the resulting graph and no test checked capacity.

What this PR does

Both overloads now reserve the computed edge count when the caller passes zero. A new test in csr_graph_test.cpp builds directed and bidirectional graphs with and without an edge property iterator, and checks that the column capacity equals the number of edges. Forward and input iterators are unaffected, and so are callers that pass an explicit edge count.

  1. first commit adds test to highlight bug (expect red CI)
  2. Fix bug (expect green CI)

Before submitting

  • I searched for an existing PR or issue covering the same change.
  • This PR targets the develop branch.
  • My contribution is licensed under the Boost Software License 1.0.

Kind of change

Tick exactly one. Each kind is reviewed differently, so mixing them in one PR makes the diff difficult to review (why).

  • Reformat (layout only, no token added or removed)
  • Refactor (no observable behavior change)
  • Behavior change (observable to users of the library)
  • No code change (documentation, build, CI, or tooling)

Change category

  • Bug fix
  • New algorithm or API addition
  • Deprecation or removal
  • Performance
  • Tests
  • Documentation
  • Build, CI, or tooling
  • Other (specify below)

@Becheler Becheler self-assigned this Oct 26, 2026
@Becheler Becheler added performance Performant use of time and space. fix labels Oct 26, 2026
@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Boost dependency footprint vs develop (auto-generated).
PR run 40703291189 vs develop run 40666148232 (a694dd589d).

Header-inclusion weights (graph files pulling each direct dependency in):

No header-inclusion-weight changes.

Transitive Boost modules: 47 → 47 (0)

@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Compiler-warning counts vs develop (auto-generated).
PR run 40478670129 vs develop run 39949899676 (fec57e725b).

Job Baseline After Delta
macos (clang, 14) 256 256 0
macos (clang, 17) 255 255 0
macos (clang, 20) 255 255 0
ubuntu (clang-19, 14) 256 256 0
ubuntu (clang-19, 17) 255 255 0
ubuntu (clang-19, 20) 255 255 0
ubuntu (clang-19, 23) 255 255 0
ubuntu (gcc-14, 14) 352 352 0
ubuntu (gcc-14, 17) 348 348 0
ubuntu (gcc-14, 20) 348 348 0
ubuntu (gcc-14, 23) 348 348 0
windows_msvc_14_3 (msvc-14.3) 961 961 0

@codecov

codecov Bot commented Oct 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Becheler
Becheler marked this pull request as ready for review October 27, 2026 12:33
@Becheler
Becheler force-pushed the fix/csr-sorted-reserve branch 2 times, most recently from a694dd5 to 9516e2d Compare October 28, 2026 09:56
@Becheler
Becheler merged commit 897aa19 into boostorg:develop Oct 28, 2026
60 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

data structure fix performance Performant use of time and space.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant