Sixteen real-world shapes
examples/graph_suite rebuilds 16 functions from popular Rust repositories. Each one is taken from
the concurrency audit and named with its repository, file and finding id. Every I/O call is replaced
by a timer with realistic latency. For each function, the suite:
- prints the original code and times it;
- prints the code
#[psync::graph]generates and times it; - asserts that both versions return the same result.
Both versions run on psync’s executor.
cargo run --example graph_suite --no-default-features # everything
cargo run --example graph_suite --no-default-features -- --brief # timings only
cargo run --example graph_suite --no-default-features -- kube # matching examples
Results
From one run (--brief):
pattern initial rewrite predicted speedup origin
P1 independent awaits 205.6 ms 60.2 ms 60.1 ms 3.41x anza-xyz/agave
P1 independent awaits 271.3 ms 60.3 ms 60.3 ms 4.50x matrix-org/matrix-rust-sdk
P1 independent awaits 55.3 ms 35.2 ms 35.1 ms 1.57x loco-rs/loco
P1 independent awaits 48.8 ms 8.1 ms 8.2 ms 5.99x LemmyNet/lemmy
P2 await in a loop 241.8 ms 40.4 ms 40.3 ms 5.99x delta-io/delta-rs
P2 await in a loop 302.0 ms 50.5 ms 50.3 ms 5.98x apache/datafusion
P2 await in a loop 225.9 ms 45.2 ms 45.2 ms 4.99x LemmyNet/lemmy
P2 await in a loop (fission) 211.0 ms 60.5 ms 60.3 ms 3.49x jdx/mise
P2 await in a loop (while-loop fission, hinted) 270.9 ms 45.2 ms 45.2 ms 5.99x launchbadge/sqlx
P2 await in a loop (fission) 160.8 ms 40.3 ms 40.2 ms 3.99x apache/iggy
P3 per-item chains 267.0 ms 45.4 ms 45.3 ms 5.88x kube-rs/kube
P3 per-item chains 604.2 ms 191.6 ms 191.4 ms 3.15x neondatabase/neon
P3 per-item chains 347.2 ms 50.4 ms 50.3 ms 6.89x quickwit-oss/quickwit
P4 late start 110.4 ms 70.3 ms 70.3 ms 1.57x GreptimeTeam/greptimedb
P4 late start 200.6 ms 120.5 ms 120.4 ms 1.66x sigp/lighthouse
P4 late start 176.0 ms 70.4 ms 70.3 ms 2.50x zed-industries/zed
geometric mean speedup over 16 examples: 3.79x
predicted is the rewrite’s time as psync predicts it from a third, profiled run of the
original, before the rewrite runs (Predicting the speedup).
Four examples carry speculate hints since the effect analysis landed:
matrix-rust-sdk’s eight account-data PUTs, mise’s content-addressed uploads, iggy’s per-table
writes, and GreptimeDB’s SST uploads. Without them, psync keeps those writes behind the ?s before
them, and the four run at about 1.0×. GreptimeDB also dropped from 1.83× to 1.57×. Its two uploads
for the same file_id now keep their order, because tokens cannot show that they write different
files.
Timings come from simulated latency, so they show the schedule psync builds, not network behaviour. For real networks, see kube-rs and DataFusion.
What each pattern shows
- P1, independent awaits. These are straight-line regions with one level. The speedup is the sum of the waits over the longest one.
- P2, await in a loop. Loop prefetch is bounded by
bound. Three of these need fission:- mise dedupes blob digests before uploading;
- iggy takes each table’s batch out of a routing map before writing;
- sqlx reserves connection slots in a
whileloop before connecting, which needs#[psync::independent].
- P3, per-item chains. Each item’s requests depend on each other, but items do not.
- kube’s
Discovery::runlists API groups, then queries each queryable group. - neon’s tenant scan runs list, then index parts, then the manifest, for each shard.
- quickwit’s delete planner is an early-
continueguard between two awaits per split. Guard inversion lets the second await run ahead too.
- kube’s
- P4, late start. A request that needs little is written after a long chain. psync starts it in
the first level:
- GreptimeDB’s upload does not need the index;
- Lighthouse’s genesis download needs only the network config;
- Zed’s database read needs nothing from S3.
One shape in full
Here is kube’s discovery loop, reduced to its dependency structure. It is the
guarded_loop_bodies_prefetch_with_field_sensitivity test:
impl Discovery {
#[graph(bound = 8)]
async fn run(mut self, all: Vec<u32>) -> Result<Self, Error> {
self.groups.clear();
for g in all {
let key = g * 10;
if self.mode.is_queryable(g) {
let versions = io(T, if g == 99 { Err(Error("bad group")) } else { Ok(self.client + g) }).await?;
self.groups.push((key, versions));
}
}
Ok(self)
}
}
Two features make this loop concurrent:
- The guard: queries run ahead only for queryable groups.
- Field sensitivity:
self.groups.pushwritesself.groups, and the prefix readsself.modeandself.client. Those do not overlap.
An error on group 99 is still returned as the first failing group, in order.
Exercises
- A P1 region awaits three independent calls of 20, 30 and 50 ms. What does it take before and after the rewrite, and what is the speedup?
- A P2 loop looks up 6 keys at 40 ms each. What does it take with the default
bound, and withbound = 4? - Why does the suite report a geometric mean, not an arithmetic one?
- Without its
speculatehint, matrix-rust-sdk’s example runs at about 1.0×. What does psync keep in order there, and why does the hint make that safe?
Answers
- 100 ms before, 50 ms after (the longest wait), so 2×: the sum of the waits over the longest.
- 240 ms sequentially. With the default of 16 all six run at once: 40 ms, 6×. With
bound = 4the first four take 40 ms, then the last two another 40 ms: 80 ms, 3×. - Speedups are ratios. A geometric mean treats 2× faster and 2× slower as cancelling out, and is not dominated by one large ratio the way an arithmetic mean is.
- The PUTs go to distinct event types, so they do not conflict with each other. But each PUT
writes, and each one comes after a
?that may leave. psync does not start a write before an earlier statement that may exit, so the eight PUTs stay one after another. The PUTs set account data to fixed values, so doing one that sequential code would have skipped after an error is harmless. That is whatspeculatesays.