Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

What is new, and what is not

psync stands on a lot of earlier work. This chapter says, idea by idea, who did it first, what psync does differently, and where its speedups come from. Every chapter that introduces one of these ideas ends with a short Prior work and novelty section that goes into more detail.

Each source below was read from the paper or the official documentation (the research notes, with a quote and section for each, are in the repository at research/prior-work-verified.md). Where a source could not be read, this chapter says so rather than guess.

The short version

Almost every single idea in psync has been done before, often decades ago:

  • Overlapping independent awaits automatically. ReSynchronizer did it for JavaScript in 2021, with a side-effect analysis and a table of file-system and network calls.
  • Joining independent chains at compile time. GHC’s ApplicativeDo does it for Haskell.
  • Raising failures in their sequential order. Haxl and Safe Futures both do it.
  • Prefetching I/O in loops. Compilers have done it since 1996.
  • Proving that a concurrent schedule equals the serial one. Safe Futures proves it.
  • Cross-crate summaries over MIR. These exist for memory-safety analysis.
  • Predicting a speedup from a dependency graph. WebProphet does it for web pages.

Measured on the same 16 real-world functions, scheduling them by ReSynchronizer’s rules gives a geometric mean of 1.28×, and psync 3.79× (details below).

What psync adds is the combination, and a few specific differences in each idea that the earlier systems do not have:

  1. Effects compared by target, not by kind. ReSynchronizer keeps every file read behind every file write. psync compares paths, URLs and keys, so two writes to different files overlap. When the targets are only known at run time, a gate compares the values, file identity included, and falls back to the original order when they collide. The closest precursor, Quasi-Static Scheduling for Safe Futures, places barriers by static alias analysis, for memory only.
  2. Rust’s control flow. ?, return, break and continue inside chains, and loops turned into bounded prefetch. ReSynchronizer never moves code across a return or a loop test; ApplicativeDo never reorders effects at all.
  3. No new runtime, no threads, no annotations. The rewrite is one future that polls its chains on the caller’s task. It works on Tokio, embassy or a microcontroller, with borrowed data. Haxl needs its own monad, Safe Futures a modified JVM, Æminium a new language, DPJ annotations.
  4. A certificate per function, checked by a proven-sound checker, against a model of the outside world. Translation validation (Pnueli, Necula) checks a compiler’s output once per run; Safe Futures proves schedules equivalent for memory. psync’s Lean checker validates each rewritten function’s schedule, with I/O events allowed to reorder only where a commutativity specification of the service says they commute.

Idea by idea

IdeaClosest prior workWhat it already didWhat psync does differentlyMore
Overlap independent awaitsReSynchronizer (ECOOP 2021), AutoFutures (2012)automatic await overlap in JavaScript, with a file/network effect table; futures for independent Java callsRust, at compile time, in a macro; effects by target; exits and loops; gates and certificatesBasic concepts
Chains joined in levelsApplicativeDo (Haskell 2016), Haxl (ICFP 2014)independent segments joined concurrently, decided from bindingsreorders past statements whose effects are independent, not only their bindingsBasic concepts
Early exitsHaxl §8.1, Safe Futures (OOPSLA 2005)the leftmost exception wins, so the result is deterministicRust ?, return, break, continue, across chains on one taskEarly exits
LoopsMowry et al. (OSDI 1996), futures::StreamExt::bufferedcompiler-inserted I/O prefetch hints; hand-written bounded concurrencyruns the real calls early, so it checks items for conflictsLoops
Effects of the outside worldReSynchronizer Table 1, DPJ (OOPSLA 2009), QSS (PPoPP 2008)effect tables per environment; nested regions; barriers from alias analysistargets that overlap by prefix, value gates with file identitySide effects
Summaries across cratesZhou, Sun and Criswell (2023), MirChecker (CCS 2021)per-function MIR summaries for whole programsthe summaries hold effects and their targets, fed back to the macroFrom MIR to summaries
CertificatesTranslation validation (1998, 2000), CompCert, Safe Futuresper-run validation; a verified compiler with identical I/O traces; serial equivalence for memoryI/O may reorder under a commutativity spec; a Lean checker proved soundCertified schedules
World modelScalable Commutativity Rule (SOSP 2013), linearizability (1990)interface-level commutativity of POSIX calls; atomic operationsthe same idea, used to justify reordering a client’s callsThe outside world
Predicting the speedupCoz (SOSP 2015), Yang and Miller (1988), WebProphet (NSDI 2010)virtual speedups; critical paths; what-if latency from a fetch graphthe graph comes from the code, per control path, for one automatic rewritePredicting the speedup
Synchronous code (Phase II)thread-level speculation (ISCA 2000), SpecHint (OSDI 1999), Rayonspeculative threads that stall on system calls; speculative read hints; data parallelism by handreal calls, reads and ordered writes, on worker threads, chosen automaticallyPhase II design

Where the speedup comes from

psync does no work faster. It waits less. When a function makes several calls that wait on the network or the disk, the sequential code waits for each in turn: the sum of the waits. After the rewrite, each level waits as long as its slowest chain. So the speedup is roughly

speedup ≈ (sum of all waits) / (sum over levels of the longest chain in each level)

and psync can compute that number before it rewrites anything (Predicting the speedup). This is why the speedups below are 1.6× to 6.9×. Three waits of equal length give 3×; a loop of six items with a bound of 16 gives 6×; a function whose second half must wait for its first gains little.

Speedup over the sequential code, 16 real-world shapes P1 independent awaits anza-xyz/agave: 3.41×anza-xyz/agave3.41× matrix-org/matrix-rust-sdk: 4.50×matrix-org/matrix-rust-sdk4.50× loco-rs/loco: 1.57×loco-rs/loco1.57× LemmyNet/lemmy: 5.99×LemmyNet/lemmy5.99× P2 await in a loop delta-io/delta-rs: 5.99×delta-io/delta-rs5.99× apache/datafusion: 5.98×apache/datafusion5.98× LemmyNet/lemmy: 4.99×LemmyNet/lemmy4.99× jdx/mise (fission): 3.49×jdx/mise (fission)3.49× launchbadge/sqlx (fission, hinted): 5.99×launchbadge/sqlx (fission, hinted)5.99× apache/iggy (fission): 3.99×apache/iggy (fission)3.99× P3 per-item chains kube-rs/kube: 5.88×kube-rs/kube5.88× neondatabase/neon: 3.15×neondatabase/neon3.15× quickwit-oss/quickwit: 6.89×quickwit-oss/quickwit6.89× P4 late start GreptimeTeam/greptimedb: 1.57×GreptimeTeam/greptimedb1.57× sigp/lighthouse: 1.66×sigp/lighthouse1.66× zed-industries/zed: 2.50×zed-industries/zed2.50× 0× 1× 2× 3× 4× 5× 6× 7× 1×: as fast as the sequential code
Geometric mean 3.8×. The dashed line is 1×. Data: Sixteen real-world shapes.

Measured: the same functions under ReSynchronizer’s rules

The closest earlier tool, ReSynchronizer, works on JavaScript, so it cannot run on these Rust functions. Instead, psync can schedule a function by ReSynchronizer’s rules, with #[psync::graph(baseline = resync)]:

  • an await may start earlier and be awaited later, but only within one block: nothing moves across a loop or a branch (its §4.6);
  • effects are one per environment (its §4.4): every file effect touches “the file system”, every network or service call “the network”, so a write orders every other access to its environment;
  • no run-time checks, and none of psync’s hints (speculate, independent).

cargo run --example graph_suite --no-default-features -- --brief --resync times each of the 16 functions three ways: as written, by these rules, and by psync. Two runs agreed to 0.02×.

The same 16 functions: ReSynchronizer's rules vs psync ReSynchronizer's rules psync P1 independent awaits anza-xyz/agave: ReSynchronizer's rules: 3.41×; psync: 3.41×anza-xyz/agave3.41×3.41× matrix-org/matrix-rust-sdk: ReSynchronizer's rules: 1.00×; psync: 4.49×matrix-org/matrix-rust-sdk1.00×4.49× loco-rs/loco: ReSynchronizer's rules: 1.57×; psync: 1.57×loco-rs/loco1.57×1.57× LemmyNet/lemmy: ReSynchronizer's rules: 5.98×; psync: 5.97×LemmyNet/lemmy5.98×5.97× P2 await in a loop delta-io/delta-rs: ReSynchronizer's rules: 1.00×; psync: 5.99×delta-io/delta-rs1.00×5.99× apache/datafusion: ReSynchronizer's rules: 1.00×; psync: 5.99×apache/datafusion1.00×5.99× LemmyNet/lemmy: ReSynchronizer's rules: 1.00×; psync: 4.99×LemmyNet/lemmy1.00×4.99× jdx/mise (fission): ReSynchronizer's rules: 1.00×; psync: 3.49×jdx/mise (fission)1.00×3.49× launchbadge/sqlx (fission, hinted): ReSynchronizer's rules: 1.00×; psync: 5.99×launchbadge/sqlx (fission, hinted)1.00×5.99× apache/iggy (fission): ReSynchronizer's rules: 1.00×; psync: 3.99×apache/iggy (fission)1.00×3.99× P3 per-item chains kube-rs/kube: ReSynchronizer's rules: 1.00×; psync: 5.88×kube-rs/kube1.00×5.88× neondatabase/neon: ReSynchronizer's rules: 1.00×; psync: 3.15×neondatabase/neon1.00×3.15× quickwit-oss/quickwit: ReSynchronizer's rules: 1.00×; psync: 6.83×quickwit-oss/quickwit1.00×6.83× P4 late start GreptimeTeam/greptimedb: ReSynchronizer's rules: 1.00×; psync: 1.57×GreptimeTeam/greptimedb1.00×1.57× sigp/lighthouse: ReSynchronizer's rules: 1.67×; psync: 1.66×sigp/lighthouse1.67×1.66× zed-industries/zed: ReSynchronizer's rules: 1.00×; psync: 2.50×zed-industries/zed1.00×2.50× 0× 1× 2× 3× 4× 5× 6× 7× 1×: as fast as the sequential code
Geometric means: ReSynchronizer's rules 1.28×, psync 3.79× (without a scheduling hint: 1.44× vs 3.89×). Measured with graph_suite --resync; the baseline is ReSynchronizer's rules applied to Rust by psync, not the JavaScript tool.
FunctionsReSynchronizer’s rulespsync
all161.28×3.79×
without a scheduling hint (speculate, independent)111.44×3.89×
loops (P2, P3)91.00×4.99×
straight-line code (P1, P4)71.77×2.67×

Read the figure row by row and the differences have three causes:

  1. Loops: 1.00× against 3.1× to 6.9×. All nine loops stay sequential under the rules, because nothing moves across a loop test. psync prefetches the items, with a check per item.
  2. Moving past a loop or a branch. zed reads the database after a loop over S3 manifests, and the read needs nothing from the loop. psync starts it before the loop: 2.50×. By the rules it must wait: 1.00×.
  3. Targets, and hints. matrix-rust-sdk makes eight PUTs to distinct account-data event types; greptimedb uploads a Parquet file that does not need the index it builds first. By the rules every write is “the network”, so each waits for the one before: 1.00×. psync compares the targets, and with the function’s speculate hint (the PUTs and the upload are idempotent, so they may run before an earlier call has succeeded) reaches 4.49× and 1.57×.

Where the rules apply, they match psync exactly: agave, loco, lemmy’s six reads and lighthouse run at the same speed both ways (3.41×, 1.57×, 5.98×, 1.67×). For straight-line awaits without writes, ReSynchronizer already had the idea, and psync adds nothing to the speed.

Two cautions. The baseline is psync’s reading of ReSynchronizer’s published rules, applied to Rust; it is not the JavaScript tool, whose analysis of particular libraries may differ. And these 16 shapes come from psync’s own audit, which looked for what psync rewrites; a sample chosen by ReSynchronizer’s authors might show a smaller gap.

What psync does not claim

  • It is not sound on its own. It assumes that names mean what they say (get reads, put writes), that unknown calls only read, and that different receivers are different resources (What is checked, what is assumed). A certificate proves a schedule safe given the effects psync found, not that it found the right ones; the proof that summaries over-approximate real accesses covers a fragment of MIR (Certified schedules).
  • It does not batch or cache. Haxl, Stitch and DataLoader merge many small reads into one request. psync sends the same requests, at the same time.
  • It can hurt under load. Offering every request at once can overload a server: 32 clients refreshing DataFusion’s catalog together ran at 0.76× (kube-rs and DataFusion).
kube-rs and DataFusion under load one client at a time (low end): 3.40×one client at a time (low end)3.40× busy server: 5.20×busy server5.20× CPU-saturated client: 4.40×CPU-saturated client4.40× 8 clients, kube-rs throughput: 2.00×8 clients, kube-rs throughput2.00× 8 clients, DataFusion throughput: 1.70×8 clients, DataFusion throughput1.70× 32 clients, kube-rs throughput: 1.03×32 clients, kube-rs throughput1.03× 32 clients, DataFusion refresh: 0.76×32 clients, DataFusion refresh0.76× 0× 1× 2× 3× 4× 5× 6× 1×: as fast as the sequential code
Below the dashed line psync is slower: 32 clients refreshing a catalog at once overload the server. Data: kube-rs and DataFusion.

Sources

  • Arteca, Tip, Schäfer. Enabling Additional Parallelism in Asynchronous JavaScript Applications (ReSynchronizer). ECOOP 2021.
  • Marlow, Brandy, Coens, Purdy. There is No Fork (Haxl). ICFP 2014.
  • Marlow, Peyton Jones, Kmett, Mokhov. Desugaring Haskell’s do-notation into Applicative Operations (ApplicativeDo). Haskell Symposium 2016.
  • Molitorisz, Schimmel, Otto. Automatic Parallelization using AutoFutures. KIT, 2012 (extended abstract).
  • Stork et al. Æminium: A Permission-Based Concurrent-by-Default Programming Language Approach. TOPLAS 2014.
  • Bocchino et al. A Type and Effect System for Deterministic Parallel Java. OOPSLA 2009.
  • Welc, Jagannathan, Hosking. Safe Futures for Java. OOPSLA 2005.
  • Navabi, Zhang, Jagannathan. Quasi-Static Scheduling for Safe Futures. PPoPP 2008.
  • Rinard, Diniz. Commutativity Analysis. TOPLAS 1997.
  • Clements et al. The Scalable Commutativity Rule. SOSP 2013.
  • Herlihy, Wing. Linearizability. TOPLAS 1990.
  • Li et al. MirChecker. CCS 2021. Zhou, Sun, Criswell. Fast Summary-based Whole-program Analysis to Identify Unsafe Memory Accesses in Rust. arXiv 2310.10298, 2023.
  • Ho et al. Charon. arXiv 2410.18042. Ho, Protzenko. Aeneas. ICFP 2022.
  • Pnueli, Siegel, Singerman. Translation Validation. TACAS 1998. Necula. Translation Validation for an Optimizing Compiler. PLDI 2000.
  • Leroy. Formal Verification of a Realistic Compiler. CACM 2009. Bell. A Proof Theory for Loop-Parallelizing Transformations. Princeton, 2014.
  • Mowry, Demke, Krieger. Automatic Compiler-Inserted I/O Prefetching for Out-of-Core Applications. OSDI 1996.
  • Curtsinger, Berger. Coz. SOSP 2015. Yang, Miller. Critical Path Analysis for the Execution of Parallel and Distributed Programs. ICDCS 1988. Zhou et al. wPerf. OSDI 2018. Li et al. WebProphet. NSDI 2010.
  • Steffan et al. A Scalable Approach to Thread-Level Speculation. ISCA 2000. Chang, Gibson. Automatic I/O Hint Generation through Speculative Execution (SpecHint). OSDI 1999.

Not read, so not relied on: Kremlin (PLDI 2011), which reportedly predicts parallel speedup and must be checked before psync claims its predictor is new; Hollingsworth and Miller’s slack; the Asynchronizer tools for Android and C#; Rudra’s paper.