Skip to content

feat: parallel v2 phase 2 — Ordered() via batch-aligned re-sequencing - #16

Merged
tr1v3r merged 1 commit into
masterfrom
feat/parallel-v2-phase2
Aug 23, 2026
Merged

tr1v3r merged 1 commit into
masterfrom
feat/parallel-v2-phase2

Conversation

@tr1v3r

@tr1v3r tr1v3r commented Aug 23, 2026

Copy link
Copy Markdown
Owner

Summary

Phase 2 of docs/proposals/parallel-v2.md: Ordered() — opt-in order preservation for parallel sections.

How it works

  • Parallel(n).Ordered()... — feeder stamps input indices, workers forward Filter-dropped elements as holes (batch boundaries/lengths stay intact), consumer re-sequenences batch-keyed: an aligned batch yields straight through with zero per-element bookkeeping; holes advance the expected index.
  • Ordered() without a parallel section is a no-op; Parallel(n) resets to unordered.

Design journey (documented in the commit)

Three candidate re-sequencers were built and measured:

  1. next-index map — deadlocks on Filter holes (caught by the A3 property test)
  2. smallest-pending — emits early under batch arrival races (caught by property test)
  3. ring buffer — corrupts when in-flight distance exceeds window (consumer-held partial batches extend it; caught by a sum-mismatch stress)
  4. batch-keyed pending + hole placeholders — correct (30-round adversarial stress) and fast

Acceptance (Apple M3 Pro)

gate target measured
A3 order property ordered == serial, element-for-element ✅ 30 random pipelines + adversarial batch-head filtering + all-filtered
ordered overhead (near-free, gate ≤3×) ≤3× serial 2.0× (naive map was 8×)
A2 heavy speedup ≥3× ✅ 3.6× unchanged
short-circuit/cancel no hang, no leak ✅ incl. 100k-element Limit(5)

Verification

  • go test ./... -race green; golangci-lint 0 issues
  • Coverage ~100% (one benign timing branch in the ordered closer occasionally reads uncovered)

…ncing

New Streamer.Ordered(): marks a parallel section order-preserving. The feeder stamps indices, workers forward Filter-dropped elements as holes (keeping batch boundaries intact), and the consumer re-sequenences batch-keyed: aligned batches yield straight through, holes advance the expected index — no stalling on filtered gaps, no jumping ahead of in-flight batches. Design notes from the implementation journey: naive next-index map waits deadlock on holes; smallest-pending emits early under batch races; ring slots corrupt when in-flight distance exceeds the window (measured: consumer-held partial batches extend it); batch-keyed pending map with hole placeholders is the correct and fast structure. Acceptance (M3 Pro): A3 property test 30 random pipelines match serial element-for-element incl. adversarial batch-head filtering and all-filtered streams; ordered overhead on near-free work 2x serial (gate <=3x, was 8x with the naive map); heavy-load speedup unchanged 3.6x; short-circuit and cancellation lean. race/lint clean; coverage ~100% (one benign timing branch in orderedSeq closer occasionally uncovered).
@tr1v3r
tr1v3r merged commit 745ef5a into master Aug 23, 2026
2 checks passed
@tr1v3r
tr1v3r deleted the feat/parallel-v2-phase2 branch August 23, 2026 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant