Skip to content

feat: add adaptive capacity scaling to ReorderBuffer - #3392

Merged
oferchen merged 2 commits into
masterfrom
feat/reorder-buffer-adaptive-capacity
Apr 26, 2026
Merged

oferchen merged 2 commits into
masterfrom
feat/reorder-buffer-adaptive-capacity

Conversation

@oferchen

Copy link
Copy Markdown
Owner

Summary

  • Introduce AdaptiveCapacityPolicy (sibling module concurrent_delta::adaptive) and compose it into ReorderBuffer via a new ReorderBuffer::with_adaptive_policy constructor. Default ReorderBuffer::new(capacity) behaviour is unchanged.
  • Buffer grows by growth_factor (capped at max) when insert() finds utilization >= 80% AND the gap window > current capacity / 2. The grow path also kicks in pre-insert when an incoming sequence would otherwise be rejected, up to max.
  • Buffer shrinks back toward min when the moving average of the last sample_window insert utilization samples falls below 25%, never evicting buffered items.
  • ReorderStats { grow_events, shrink_events, capacity } is exposed through ReorderBuffer::stats().

Test plan

  • cargo fmt --all (clean against changed files).
  • CI: fmt+clippy, nextest (stable), Windows (stable), macOS (stable), Linux musl (stable).
  • New unit tests:
    • adaptive_tests::fixed_capacity_default_unchanged - default constructor is unaffected.
    • adaptive_tests::adaptive_buffer_starts_at_min_capacity
    • adaptive_tests::grows_under_load
    • adaptive_tests::never_exceeds_max
    • adaptive_tests::shrinks_when_idle
    • adaptive_tests::never_drops_below_min
    • adaptive_tests::stats_track_both_events
    • adaptive_tests::ordering_preserved_through_resize
  • Module-level tests in adaptive.rs cover policy bound validation, grow/shrink predicates, and rolling-window semantics.
  • All pre-existing ReorderBuffer tests retained and continue to pass under the unchanged fixed-capacity default path.

Introduce AdaptiveCapacityPolicy as a sibling type composed into
ReorderBuffer. The buffer can now grow under sustained pressure
(>= 80% utilization with a wide gap window) and shrink back toward
the configured minimum once a rolling window of inserts averages
below 25% utilization. ReorderStats exposes grow/shrink event
counters via a stats() getter. Default fixed-capacity construction
is unchanged so existing callers see no behaviour change.
Bind the AdaptiveCapacityPolicy constructor returns to `let _` so the
`#[must_use]` attribute is satisfied even though the test bodies are
expected to panic before any value is observed.
@oferchen
oferchen force-pushed the feat/reorder-buffer-adaptive-capacity branch from f1e29ab to 85b4c27 Compare April 26, 2026 09:52
@oferchen
oferchen merged commit 5f06641 into master Apr 26, 2026
40 of 41 checks passed
@oferchen
oferchen deleted the feat/reorder-buffer-adaptive-capacity branch April 26, 2026 12:56
oferchen added a commit that referenced this pull request Apr 28, 2026
)

Task #1853 verification. The concurrent_delta::ReorderBuffer was already
converted to a Box<[Option<T>]> ring buffer in #1734 (with adaptive
capacity scaling added in #3392), so the BTreeMap-vs-ring decision is
already settled in production code. This change adds the scaling
benchmark called for in #1853 to confirm O(1) per-item behaviour at the
10M-file scale that motivated the task, and corrects the stale
"BTreeMap-backed" doc comment in the concurrent_delta module overview.

The benchmark compares the production ring buffer against a self-
contained BTreeMap baseline at 10K, 100K, 1M, and 10M items. The 10M
case is gated behind BENCH_REORDER_10M=1 so default invocations stay
fast; CI can opt in when targeting the scaling regression dataset.

No production behaviour changes. Existing reorder_buffer tests already
cover gap-larger-than-capacity (force_insert grows the ring) and
ring wrap-around.
oferchen added a commit that referenced this pull request May 1, 2026
* feat: add adaptive capacity scaling to ReorderBuffer

Introduce AdaptiveCapacityPolicy as a sibling type composed into
ReorderBuffer. The buffer can now grow under sustained pressure
(>= 80% utilization with a wide gap window) and shrink back toward
the configured minimum once a rolling window of inserts averages
below 25% utilization. ReorderStats exposes grow/shrink event
counters via a stats() getter. Default fixed-capacity construction
is unchanged so existing callers see no behaviour change.

* fix: silence unused_must_use clippy in should_panic tests

Bind the AdaptiveCapacityPolicy constructor returns to `let _` so the
`#[must_use]` attribute is satisfied even though the test bodies are
expected to panic before any value is observed.
oferchen added a commit that referenced this pull request May 1, 2026
)

Task #1853 verification. The concurrent_delta::ReorderBuffer was already
converted to a Box<[Option<T>]> ring buffer in #1734 (with adaptive
capacity scaling added in #3392), so the BTreeMap-vs-ring decision is
already settled in production code. This change adds the scaling
benchmark called for in #1853 to confirm O(1) per-item behaviour at the
10M-file scale that motivated the task, and corrects the stale
"BTreeMap-backed" doc comment in the concurrent_delta module overview.

The benchmark compares the production ring buffer against a self-
contained BTreeMap baseline at 10K, 100K, 1M, and 10M items. The 10M
case is gated behind BENCH_REORDER_10M=1 so default invocations stay
fast; CI can opt in when targeting the scaling regression dataset.

No production behaviour changes. Existing reorder_buffer tests already
cover gap-larger-than-capacity (force_insert grows the ring) and
ring wrap-around.
oferchen added a commit that referenced this pull request May 5, 2026
* feat: add adaptive capacity scaling to ReorderBuffer

Introduce AdaptiveCapacityPolicy as a sibling type composed into
ReorderBuffer. The buffer can now grow under sustained pressure
(>= 80% utilization with a wide gap window) and shrink back toward
the configured minimum once a rolling window of inserts averages
below 25% utilization. ReorderStats exposes grow/shrink event
counters via a stats() getter. Default fixed-capacity construction
is unchanged so existing callers see no behaviour change.

* fix: silence unused_must_use clippy in should_panic tests

Bind the AdaptiveCapacityPolicy constructor returns to `let _` so the
`#[must_use]` attribute is satisfied even though the test bodies are
expected to panic before any value is observed.
oferchen added a commit that referenced this pull request May 5, 2026
)

Task #1853 verification. The concurrent_delta::ReorderBuffer was already
converted to a Box<[Option<T>]> ring buffer in #1734 (with adaptive
capacity scaling added in #3392), so the BTreeMap-vs-ring decision is
already settled in production code. This change adds the scaling
benchmark called for in #1853 to confirm O(1) per-item behaviour at the
10M-file scale that motivated the task, and corrects the stale
"BTreeMap-backed" doc comment in the concurrent_delta module overview.

The benchmark compares the production ring buffer against a self-
contained BTreeMap baseline at 10K, 100K, 1M, and 10M items. The 10M
case is gated behind BENCH_REORDER_10M=1 so default invocations stay
fast; CI can opt in when targeting the scaling regression dataset.

No production behaviour changes. Existing reorder_buffer tests already
cover gap-larger-than-capacity (force_insert grows the ring) and
ring wrap-around.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant