Skip to content

OpenQASM parser optimization#3392

Open
idavis wants to merge 5 commits into
iadavis/openqasm-parser-movefrom
iadavis/openqasm-parser-perf
Open

OpenQASM parser optimization#3392
idavis wants to merge 5 commits into
iadavis/openqasm-parser-movefrom
iadavis/openqasm-parser-perf

Conversation

@idavis

@idavis idavis commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator

OpenQASM parser optimization: before vs after

Input sizes used: 1 MiB (63,001 statements) and 5 MiB (302,908 statements).

  • Peak memory: −48% (≈ halved)
  • Retained memory: −59% (semantic-lowering's retained set alone is down −77%)
  • Time: −30%
  • Allocation count (using dhat): −50% at analyze — the root driver of both the time and peak wins

What changed

Change Targets
Remove the whole-source clone() in lower() (walk via mem::take) transient peak
List<T>: Box<[Box<T>]>Box<[T]> (drop per-element box) allocation count + time
Drop the retained syntax AST after lowering (program: Option<Program>) retained/net-live
Box inline Expr fields in big ExprKind variants (264 → 96 B) both
Box inline Expr fields in big StmtKind variants (280 → 96 B) both

Time — criterion median

Stage 1 MiB before 1 MiB after Δ 5 MiB before 5 MiB after Δ
tokenize 11.3 ms 11.2 ms ~0% 54.4 ms 55.0 ms ~0%
parse 79.5 ms 68.9 ms −13% 378.7 ms 335.1 ms −12%
semantic_lower 67.5 ms 44.4 ms −34% 336.4 ms 214.4 ms −36%
analyze (e2e) 161.9 ms 113.6 ms −30% 789.6 ms 552.2 ms −30%

Tokenize is unchanged (no lexer changes).

Memory — peak

Stage 1 MiB before 1 MiB after Δ 5 MiB before 5 MiB after Δ
parse 45.8 MB 43.2 MB −6% 227 MB 214 MB −6%
semantic_lower 170.0 MB 70.3 MB −59% 824 MB 345 MB −58%
analyze (e2e) 213.7 MB 111.3 MB −48% 1034 MB 542 MB −48%

Memory — retained (net-live, after the stage returns)

Stage 1 MiB before 1 MiB after Δ 5 MiB before 5 MiB after Δ
parse 43.7 MB 41.1 MB −6% 210 MB 198 MB −6%
semantic_lower 128.7 MB 30.2 MB −77% 619 MB 145 MB −77%
analyze (e2e) 172.5 MB 71.2 MB −59% 829 MB 343 MB −59%

@idavis idavis self-assigned this Jun 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant