Skip to content

Reduce allocations when continuing a single-type GenerationCounts - #1253

Merged
sbfnk merged 15 commits into
mainfrom
issue-1251-continuing-a-single-type-generationcount
Nov 25, 2026
Merged

sbfnk merged 15 commits into
mainfrom
issue-1251-continuing-a-single-type-generationcount

Conversation

@sbfnk-bot

@sbfnk-bot sbfnk-bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Continuing a single-type GenerationCounts no longer allocates in a generation, including one that takes doses from the population, one that doses contacts, and the first continuation of a fresh state. The state holds doses_used as a tuple of integers, one per campaign, and protected as an integer. It stays immutable, and a continuation returns a new one without a heap copy. States with several types keep a vector of vectors and a vector. prepare_generations holds the campaigns of a single-type state as a tuple, so their number is known at compile time. A fresh single-type state built by simulate starts with one zero per campaign, so its first continuation returns a state of the same type. The draws are unchanged.

Measured with @allocated on a prepared continuation, a budgeted MassVaccination, a GroupVaccination, both together, and no campaign allocate 0 bytes, including the first continuation of a fresh state. I did not re-run the thread-scaling and particle-filter timings. test/test_generation_campaigns.jl passes with the new allocation checks, as do the other test files that read these fields.

This PR closes #1251.

sbfnk-bot and others added 3 commits November 25, 2026 08:17
Co-authored-by: sbfnk <sebastian.funk@lshtm.ac.uk>
Co-authored-by: sbfnk <sebastian.funk@lshtm.ac.uk>
Co-authored-by: sbfnk <sebastian.funk@lshtm.ac.uk>
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark comparison vs base

Minimum time per call. Buckets are PR time as a % of base, so lower is faster (🟢 faster, ⚪ within 5%, 🔴 slower). Counts of benchmarks per bucket:

Group 🟢 <50% 🟢 50–75% 🟢 75–95% ⚪ 95–105% 🔴 105–125% 🔴 125–150% 🔴 >150%
Evaluation · · · 19 · · ·
Evaluation — 19 benchmarks (by time change)
Benchmark base PR time memory
simulation: homogeneous, susceptibility trait 33.67 ms 34.68 ms ⚪ 1.03× ⚪ 1.0×
simulation: branching process, interventions 16.39 μs 16.7 μs ⚪ 1.02× ⚪ 1.0×
simulation: homogeneous SI, max_time 17.96 ms 18.26 ms ⚪ 1.02× ⚪ 1.0×
simulation: network, live kernel 3.97 ms 4.02 ms ⚪ 1.01× ⚪ 1.0×
simulation: branching process, HeldCapacity with DeferRefused 1.33 s 1.32 s ⚪ 0.99× ⚪ 1.0×
simulation: homogeneous 27.63 ms 27.88 ms ⚪ 1.01× ⚪ 1.0×
evaluation: compile_contact_pairs 26.08 μs 26.24 μs ⚪ 1.01× ⚪ 1.0×
simulation: network, shared kernel 165.42 μs 166.37 μs ⚪ 1.01× ⚪ 1.0×
simulation: network, live kernel with policy 4.39 ms 4.41 ms ⚪ 1.01× ⚪ 1.0×
evaluation: chain size loglikelihood 1.97 μs 1.96 μs ⚪ 0.99× —
simulation: branching process 1.46 ms 1.46 ms ⚪ 1.0× ⚪ 1.0×
evaluation: pairwise_surv_loglik 24.3 μs 24.24 μs ⚪ 1.0× ⚪ 1.0×
evaluation: PoissonLognormal pdf, large count 38.07 μs 37.98 μs ⚪ 1.0× ⚪ 1.0×
evaluation: PoissonWeibull pdf, large count 50.64 μs 50.76 μs ⚪ 1.0× ⚪ 1.0×
simulation: households 1.02 ms 1.02 ms ⚪ 1.0× ⚪ 1.0×
evaluation: PoissonWeibull pdf 51.71 μs 51.78 μs ⚪ 1.0× ⚪ 1.0×
evaluation: PoissonLognormal pdf 36.69 μs 36.72 μs ⚪ 1.0× ⚪ 1.0×
evaluation: ChainSizes loglikelihood 460.0 ns 460.0 ns ⚪ 1.0× —
evaluation: extinction_probability 891.0 ns 891.0 ns ⚪ 1.0× ⚪ 1.0×
AD gradients — 0 benchmarks (by time change)

none

@codecov

codecov Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.83333% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.82%. Comparing base (7eed817) to head (eab3669).
⚠️ Report is 29 commits behind head on main.

Files with missing lines Patch % Lines
src/models/generation_counts.jl 92.30% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1253      +/-   ##
==========================================
+ Coverage   97.81%   97.82%   +0.01%     
==========================================
  Files         104      104              
  Lines       11791    12143     +352     
==========================================
+ Hits        11533    11879     +346     
- Misses        258      264       +6     
Flag Coverage Δ
epibranch-ad 29.36% <0.00%> (-0.14%) ⬇️
epibranch-analytical 40.44% <0.00%> (-0.23%) ⬇️
epibranch-engine 52.38% <8.33%> (-0.26%) ⬇️
epibranch-inference 44.21% <0.00%> (+0.53%) ⬆️
epibranch-interventions 54.09% <95.83%> (+0.02%) ⬆️
epibranch-population 53.78% <66.66%> (-0.24%) ⬇️
epihouseholds 96.94% <ø> (ø)
epinetwork 95.40% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

sbfnk-bot and others added 2 commits November 25, 2026 10:04
Co-authored-by: sbfnk <sebastian.funk@lshtm.ac.uk>
Co-authored-by: sbfnk <sebastian.funk@lshtm.ac.uk>
@sbfnk-bot

Copy link
Copy Markdown
Collaborator Author

The changelog check failed because the pull request had no fragment. I added changelog.d/changed-generation-counts-continuation-allocations.md, and scripts/changelog.jl --list reads it. I did not run the check's own diff against the merge ref. No code changed.

Co-authored-by: sbfnk <sebastian.funk@lshtm.ac.uk>
@sbfnk-bot

Copy link
Copy Markdown
Collaborator Author

Merged main. The conflict was in the single-type _run_generations, where main now tracks the people protected per type. The scalar population path keeps its integer arithmetic and passes the vaccinated count to protected_by_doses. The returned state reuses state.protected when nothing was vaccinated. One test that built a GenerationCounts directly now passes the new protected field. test/test_generation_campaigns.jl passes.

@sbfnk-bot

Copy link
Copy Markdown
Collaborator Author

Relayed for @sbfnk:

Yes, change the type of doses_used and go to zero allocations for a single type, including a generation that takes doses and a contact-dosing campaign. A particle filter continuing 1,000 single-type states per generation over 8 populations takes 42 ms with this PR against 74 ms before. A hand-written step that allocates nothing takes 14 ms. The dose counts of a single-type state with one budget per campaign can be plain integers, or a tuple of them, so that the state stays immutable and a continuation returns a new one without a heap copy.

sbfnk-bot and others added 2 commits November 25, 2026 11:55
…on allocates nothing

Co-authored-by: sbfnk <sebastian.funk@lshtm.ac.uk>
Co-authored-by: sbfnk <sebastian.funk@lshtm.ac.uk>
@sbfnk-bot

Copy link
Copy Markdown
Collaborator Author

A single-type GenerationCounts now holds doses_used as a tuple of integers, one per campaign, and protected as an integer. The state stays immutable, and a continuation returns a new one without a heap copy. States with several types keep the vector forms. prepare_generations holds the campaigns of a single-type state as a tuple, so their number is known at compile time.

Measured with @allocated on a prepared continuation: 0 bytes for a budgeted MassVaccination taking doses from the population, a GroupVaccination dosing contacts, and both together, once the state already holds the doses of its campaigns. The first continuation of a fresh state, whose doses_used is still (), allocates 96 bytes because the returned state has a different type from the one given. The draws are unchanged. The tests that read doses_used and protected of a single-type state were updated, and a new test checks zero allocations for both campaign kinds. The particle-filter timings were not re-run.

sbfnk-bot and others added 2 commits November 25, 2026 12:32
…ead of a missing method

Co-authored-by: sbfnk <sebastian.funk@lshtm.ac.uk>
Co-authored-by: sbfnk <sebastian.funk@lshtm.ac.uk>
@sbfnk-bot

Copy link
Copy Markdown
Collaborator Author

The JET failure came from _single_used having no method for a Vector{Vector{Int}} doses_used, which the single-type path can see through the field's union type. I added one in src/models/generation_counts.jl. It returns zero doses for each campaign, as a state with no doses for these campaigns already does. test/test_generation_campaigns.jl passes. I could not run the JET check here, so CI has to confirm it.

Comment thread test/test_generation_campaigns.jl Resolved
sbfnk-bot and others added 3 commits November 25, 2026 12:43
Co-authored-by: sbfnk <sebastian.funk@lshtm.ac.uk>
Co-authored-by: sbfnk <sebastian.funk@lshtm.ac.uk>
Co-authored-by: sbfnk <sebastian.funk@lshtm.ac.uk>
@sbfnk-bot

Copy link
Copy Markdown
Collaborator Author

The failing check was the allocation test on the full single-type state, which reported 288 bytes on Julia 1.10. It called simulate with the rng keyword directly inside @allocated. The other allocation checks measure through a helper function and passed, so this one now does too. test/test_generation_campaigns.jl passes on the default Julia. I could not run it on 1.10 here, so CI has to confirm the 288 bytes are gone.

@sbfnk-bot
sbfnk-bot marked this pull request as ready for review November 25, 2026 14:09
@sbfnk-bot
sbfnk-bot requested a review from sbfnk as a code owner November 25, 2026 14:09
@sbfnk-bot

Copy link
Copy Markdown
Collaborator Author

Relayed for @sbfnk:

Fix the fresh-state case in this PR too, so that it closes #1251. The issue's own example continues a fresh state with empty doses_used on every call, and that still allocates 96 bytes each time. Give a single-type state its doses as a tuple of zeros, one per campaign, before the first continuation (in prepare_generations or where the state is built), so the returned state has the same type as the one given. Add an allocation check for the first continuation of a fresh state with a MassVaccination, a GroupVaccination and no campaign, and re-run the issue's example.

sbfnk-bot and others added 2 commits November 25, 2026 18:01
Co-authored-by: sbfnk <sebastian.funk@lshtm.ac.uk>
Co-authored-by: sbfnk <sebastian.funk@lshtm.ac.uk>
@sbfnk-bot

Copy link
Copy Markdown
Collaborator Author

simulate now builds the fresh single-type state with one zero per campaign (_zero_doses), so its first continuation returns a state of the same type and allocates nothing. The test covers a budgeted MassVaccination, a GroupVaccination and no campaign, and checks both the type and 0 bytes. test/test_generation_campaigns.jl passes. I did not re-run the example script from the issue. A state built by hand with an empty doses_used still changes type on its first continuation, because prepare_generations cannot alter a state it is given.

@sbfnk
sbfnk enabled auto-merge November 25, 2026 18:22
@sbfnk-bot

Copy link
Copy Markdown
Collaborator Author

The documentation job timed out. The supplied log shows only cleanup, so the stall point and cause remain unconfirmed. The build did not complete.

@sbfnk
sbfnk merged commit 2f8f3a3 into main Nov 25, 2026
43 of 44 checks passed
@sbfnk
sbfnk deleted the issue-1251-continuing-a-single-type-generationcount branch November 25, 2026 19:43
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.

Continuing a single-type GenerationCounts allocates on every call

2 participants