Concurrent price lookup with fallback
Requirement
Price a six-line order. Each SKU is looked up in the live pricing
service — at most three lookups in flight — but the
service is missing some SKUs and returns null for them;
those lines fall back to the catalog list price carried on the item.
Print each priced line in order (flagging fallbacks), the fallback
count, the order total, and the maximum observed concurrency. All data
is in the code below.
The FxDart chain does the lookup under concurrent(3), then
a second map applies the fallback with plain
?? — the recovery policy is just another pipeline step
downstream of the bounded fetch.
Expected output
priced 6 items, 3 lookups at a time: SKU-100 x2 @ $8.49 SKU-205 x1 @ $24.50 (catalog fallback) SKU-317 x3 @ $3.99 SKU-408 x1 @ $119.00 SKU-512 x4 @ $2.80 (catalog fallback) SKU-620 x2 @ $14.20 fallbacks used: 2 order total: $212.05 max lookups in flight: 3
Side by side
Native Dart
FxDart
Why they differ
The fallback itself is easy in both versions — ?? is Dart.
What plain Dart lacks is the step before it: three-at-a-time lookups
with results in input order force the worker-pool idiom (shared cursor,
pre-sized slots, Future.wait), and the fallback logic ends
up buried inside the worker body where it is hardest to see and test.
In the FxDart version fetch and recovery are two separate, visible
stages of one chain — concurrent(3) owns the limit,
the next map owns the policy — and the summary lines
(filter + size for fallbacks,
sumBy for the total) reuse the same vocabulary the rest of
the site teaches.
Benchmark
Async case: the headline scale is N = 100,000, not 1,000,000. Every element costs an event-loop turn on both sides, so a million real awaits would measure Dart's event loop for minutes — not the pipeline. Delays are zero-length and the example's concurrency limit is kept; what the bars compare is the pipeline machinery.
N = 100
Time Tie
Peak memory Tie
N = 10,000
Time Tie
Peak memory FxDart wins
N = 100,000
Time Tie
Peak memory Tie
Bars are medians of repeated timed iterations in fresh processes per side (small N is batched for timer resolution). Sides within 5% of each other — or within 0.6 ms, a difference no person can perceive — count as a tie; close relative races are re-measured up to 5 runs. In an app, anything under a few milliseconds is invisible to the user regardless of which bar is shorter. Memory is peak process RSS. The Dart VM and the dataset are identical on both sides, so the difference between the two bars is what the pipeline itself holds onto.