← LeaderboardThe Skill Leaderboard

rust-rayon-par-bridge-order

Preserve source order when Rust Rayon par_bridge processes a sequential iterator. Retain input positions and verify the output under out-of-order completion.

Published by sickn33 ↗sickn33/agentic-awesome-skills

Try your first task

A source-grounded starter task has not been verified for this skill yet.

SKILL.md

name: rust-rayon-par-bridge-orderdescription: Preserve source order when Rust Rayon par_bridge processes a sequential iterator. Retain input positions and verify the output under out-of-order completion.category: developmentrisk: criticalsource: selfsource_type: selfdate_added: "2026-12-03"author: ShianMiketags: [rust, rayon, par-bridge, concurrency, ordering]

Rust Rayon par_bridge result ordering

When to Use

  • A pipeline switches from a sequential iterator to par_bridge() and its output can appear out of source order.
  • A channel receives events from parallel workers and later consumers expect records to align by source position.
  • A finite batch or streaming writer needs a stable order while CPU-bound transformation runs in Rayon.

Prerequisites

  • Locate the source iterator, worker result type, collector or channel, and consumer that relies on ordering.
  • Identify the exact contract. It may be source order, a sorted business key with a tie break, or independent records.
  • Use the project's resolved Rayon version. The example uses Rayon 1.12.0 and two worker threads.

1. Trace where order is lost

  1. Locate where positions are assigned and where output order is fixed. Assign source indices on the sequential iterator before calling par_bridge().
  2. A bridged iterator distributes items from a sequential source to Rayon workers. It does not guarantee the original order. A channel carrying worker results can expose completion order.
  3. Inspect closures for externally visible effects. Ordering the collected values cannot reorder writes, messages, or mutations that already happened inside workers.
  4. If the source iterator has unstable order, establish a deterministic source order or use the application's defined key and tie break before expecting repeatable output.

2. Choose a collection path

  • For a finite iterator with one output per input, carry (index, output) through the worker. Collect, restore index order, and hand the ordered output to the consumer. Use unique indices directly from the source.
  • If each input can emit several records, retain the per-input output order alongside the source index. If inputs can be filtered or fail, decide how the consumer observes skipped positions or errors.
  • For a slice or vector that already supports indexed iteration, use par_iter().map(...) and an indexed collector such as collect_into_vec. Keep external publication in a subsequent ordered step.
  • For long or unbounded sources, use bounded batches or a bounded reorder buffer. Design its admission and error handling so a missing or slow early item cannot cause unbounded memory growth or leave later results waiting indefinitely.

3. Verify ordering with reversed completion

  1. Use a controlled test that makes a later input complete while an earlier input is waiting. Confirm the order actually observed by the collector or receiver.
  2. Assert the final payload sequence at the output boundary. Also verify that the count and source positions match the contract.
  3. If an indexed path is available, compare it against the sequential order on the same input.
  4. After an implementation change, run the focused downstream check and state whether publication moves to an ordered consumer.

Example

Restore source order after a Rayon bridge waits for the second result before letting the first worker send. Publishing directly from the receiver reverses the two records. Sorting by original position restores the intended order.

Limitations

  • The fixture controls completion of two CPU-worker results on a two-thread pool. It does not measure throughput, reorder window size, or scheduling fairness.
  • par_bridge pulls from a sequential iterator under synchronization. If input acquisition or output buffering dominates runtime, profile the real pipeline.
  • Blocking workers while waiting for other work on the same pool can exhaust the pool. The fixture uses a bounded wait to expose a scheduling failure.

Safety and Side Effects

Keep transformation results private until the order is established if publication order matters. Use a disposable output sink for tests. The example only sends synthetic values through in-process channels and assembles vectors.

Related Skills and Documentation

rust-rayon-par-bridge-order by sickn33 · The Skill Leaderboard