Skip to content

Lazy-load typechecking environment for quicker startup, help, etc#1623

Draft
WardBrian wants to merge 2 commits into
masterfrom
lazy-load-environment
Draft

Lazy-load typechecking environment for quicker startup, help, etc#1623
WardBrian wants to merge 2 commits into
masterfrom
lazy-load-environment

Conversation

@WardBrian
Copy link
Copy Markdown
Member

This small change inserts a couple strategic lazy statements to improve the loading time of the compiler.

Many invocations of the compiler never reach typechecking: those for shell completion (#1572), --help, or parser errors. Currently, all of those still load the math library signatures, which is sometimes the majority of the runtime.

Benchmarks:

Details

No command line

$ hyperfine --warmup 20 -L version master,lazy "./stanc-{version} " -iN
Benchmark 1: ./stanc-master 
  Time (mean ± σ):      15.1 ms ±   1.2 ms    [User: 7.3 ms, System: 7.6 ms]
  Range (min … max):    12.6 ms …  19.4 ms    183 runs
 
Benchmark 2: ./stanc-lazy 
  Time (mean ± σ):       5.6 ms ±   0.8 ms    [User: 1.8 ms, System: 3.6 ms]
  Range (min … max):     4.5 ms …   8.9 ms    570 runs
 
Summary
  ./stanc-lazy  ran
    2.69 ± 0.42 times faster than ./stanc-master

Completion

$ hyperfine --warmup 20 -L version master,lazy "./stanc-{version} --__complete -I --__complete=" -N
Benchmark 1: ./stanc-master --__complete -I --__complete=
  Time (mean ± σ):      14.9 ms ±   1.1 ms    [User: 7.2 ms, System: 7.4 ms]
  Range (min … max):    12.8 ms …  18.5 ms    222 runs
 
Benchmark 2: ./stanc-lazy --__complete -I --__complete=
  Time (mean ± σ):       5.6 ms ±   0.8 ms    [User: 1.8 ms, System: 3.6 ms]
  Range (min … max):     4.5 ms …  10.5 ms    587 runs
 
Summary
  ./stanc-lazy --__complete -I --__complete= ran
    2.64 ± 0.43 times faster than ./stanc-master --__complete -I --__complete=

Syntax error:

$ hyperfine --warmup 20 -L version master,lazy "./stanc-{version} ./test/integration/bad/expect_statement_seq_close_brace_2.stan" -i
Benchmark 1: ./stanc-master ./test/integration/bad/expect_statement_seq_close_brace_2.stan
  Time (mean ± σ):      15.4 ms ±   1.4 ms    [User: 7.4 ms, System: 7.9 ms]
  Range (min … max):    13.0 ms …  20.1 ms    175 runs
 
Benchmark 2: ./stanc-lazy ./test/integration/bad/expect_statement_seq_close_brace_2.stan
  Time (mean ± σ):       6.9 ms ±   0.8 ms    [User: 2.6 ms, System: 4.2 ms]
  Range (min … max):     5.4 ms …  11.6 ms    433 runs
 
Summary
  ./stanc-lazy ./test/integration/bad/expect_statement_seq_close_brace_2.stan ran
    2.24 ± 0.34 times faster than ./stanc-master ./test/integration/bad/expect_statement_seq_close_brace_2.stan

Type error:

$ hyperfine --warmup 20 -L version master,lazy "./stanc-{version} ./test/integration/bad/complex-numbers/bad_bounds1.stan" -i
Benchmark 1: ./stanc-master ./test/integration/bad/complex-numbers/bad_bounds1.stan
  Time (mean ± σ):      15.5 ms ±   1.2 ms    [User: 7.8 ms, System: 7.5 ms]
  Range (min … max):    13.3 ms …  18.8 ms    172 runs
 
Benchmark 2: ./stanc-lazy ./test/integration/bad/complex-numbers/bad_bounds1.stan
  Time (mean ± σ):      15.6 ms ±   1.3 ms    [User: 7.8 ms, System: 7.7 ms]
  Range (min … max):    13.1 ms …  19.7 ms    204 runs
 
Summary
  ./stanc-master ./test/integration/bad/complex-numbers/bad_bounds1.stan ran
    1.01 ± 0.11 times faster than ./stanc-lazy ./test/integration/bad/complex-numbers/bad_bounds1.stan

Successful compile:

$ hyperfine --warmup 20 -L version master,lazy "./stanc-{version} ./test/integration/good/code-gen/laplace_bernoulli_logit.stan" -N
Benchmark 1: ./stanc-master ./test/integration/good/code-gen/laplace_bernoulli_logit.stan
  Time (mean ± σ):      22.0 ms ±   1.6 ms    [User: 13.5 ms, System: 8.3 ms]
  Range (min … max):    19.0 ms …  28.3 ms    130 runs
 
Benchmark 2: ./stanc-lazy ./test/integration/good/code-gen/laplace_bernoulli_logit.stan
  Time (mean ± σ):      21.8 ms ±   1.7 ms    [User: 13.9 ms, System: 7.6 ms]
  Range (min … max):    18.8 ms …  26.7 ms    141 runs
 
Summary
  ./stanc-lazy ./test/integration/good/code-gen/laplace_bernoulli_logit.stan ran
    1.01 ± 0.11 times faster than ./stanc-master ./test/integration/good/code-gen/laplace_bernoulli_logit.stan

So overall a tie when the typechecking is needed, but twice or more as fast when it isn't

Submission Checklist

  • Run unit tests
  • Documentation
    • If a user-facing facing change was made, the documentation PR is here:
    • OR, no user-facing changes were made

Release notes

Improved startup time of the compiler by deferring loading of the math library signatures

Copyright and Licensing

By submitting this pull request, the copyright holder is agreeing to
license the submitted work under the BSD 3-clause license (https://opensource.org/licenses/BSD-3-Clause)

@codecov
Copy link
Copy Markdown

codecov Bot commented May 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.49%. Comparing base (37675d4) to head (5f9c77f).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1623   +/-   ##
=======================================
  Coverage   90.49%   90.49%           
=======================================
  Files          65       65           
  Lines       10112    10115    +3     
=======================================
+ Hits         9151     9154    +3     
  Misses        961      961           
Files with missing lines Coverage Δ
src/frontend/Environment.ml 88.52% <100.00%> (+0.19%) ⬆️
src/frontend/SignatureMismatch.ml 87.77% <100.00%> (ø)
src/frontend/Typechecker.ml 94.21% <ø> (+<0.01%) ⬆️
src/stan_math_signatures/Stan_math_signatures.ml 86.27% <100.00%> (+0.13%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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