-
Notifications
You must be signed in to change notification settings - Fork 740
Description
Summary
scarb build is 40x+ slower in Cairo 2.15.0 when compiling a file containing multiple circuit functions with many operations. The same code compiles in seconds with Cairo 2.14.0. In larger codebases, the slowdown is so severe the build appears to hang indefinitely.
Reproducer
A minimal standalone reproducer is available at:
https://github.com/feltroidprime/cairo-compiler-bug-2-15
The reproducer is adapted from garaga's tower_circuits.cairo — 23 circuit functions totaling ~6000 lines of BLS12-381 and BN254 tower field arithmetic.
git clone https://github.com/feltroidprime/cairo-compiler-bug-2-15
cd cairo-compiler-bug-2-15
# Fast with Scarb 2.14.0 (~3 seconds)
cd cairo_2_14 && time scarb build
# Dramatically slow with Scarb 2.15.0 (~2 minutes for this standalone file)
cd ../cairo_2_15 && time scarb buildTimings
| Version | Time |
|---|---|
| Scarb 2.14.0 (Cairo 2.14.0) | ~3 seconds |
| Scarb 2.15.0 (Cairo 2.15.0) | ~116 seconds |
For the full garaga codebase (dozens of circuit files), the build exceeds 10 minutes and appears to hang indefinitely.
What the Code Does
Each circuit function:
- Builds a DAG of 50–700+ circuit operations using
circuit_add,circuit_mul,circuit_sub,circuit_inverse - Creates deeply-nested generic types — e.g.
MulModGate<MulModGate<CI<28>, CI<28>>, CI<28>>— where intermediate results are reused (DAG, not a tree) - Returns up to 12
u384outputs assembled from circuit results
The output tuple type for a function with 12 outputs and 300+ operations becomes a 12-element tuple where each element is a CircuitElement wrapping hundreds of levels of nested gate types.
Root Cause Hypothesis
The regression was introduced in Cairo 2.15.0. The most likely candidates are:
- PR added specialization for tuples and fixed size arrays #9087 — tuple/fixed-size array specialization
- PR added specialization for recursive functions #8881 — recursive function specialization
We suspect these specialization passes now process the deeply-nested circuit gate types without memoization (or with incomplete memoization), causing exponential blowup proportional to:
- circuit depth (number of operations → type nesting depth)
- DAG reuse (shared subtypes that get re-expanded rather than shared)
- number of such functions in the same compilation unit
Isolation
Binary search on garaga confirmed the regression is entirely caused by circuits/tower_circuits.cairo. Removing it from the module tree causes the full garaga build to complete normally under Scarb 2.15.0.
Environment
- Platform: x86_64-unknown-linux-gnu
- Scarb 2.14.0 / cairo-lang 2.14.0 ✅ fast
- Scarb 2.15.0 / cairo-lang 2.15.0 ❌ 40x slower