Skip to content

Fix slowdown when using periodic_save_freq in MCMC - #436

Draft
sblunt wants to merge 2 commits into
v4from
speedup-periodicsave
Draft

Fix slowdown when using periodic_save_freq in MCMC#436
sblunt wants to merge 2 commits into
v4from
speedup-periodicsave

Conversation

@sblunt

@sblunt sblunt commented Sep 1, 2026

Copy link
Copy Markdown
Owner

periodic_save_freq previously re-derived the entire chain history (including an unvectorized per-sample prior recomputation) and rewrote the whole HDF5 output file on every periodic save, making cost grow with total run length instead of just the new chunk. _update_chains_from_sampler now only processes steps since the last save, and Results.save_results appends to resizable HDF5 datasets instead of rewriting the file each time.

(experimenting with claude)

sblunt and others added 2 commits September 1, 2026 12:09
periodic_save_freq previously re-derived the entire chain history (including
an unvectorized per-sample prior recomputation) and rewrote the whole HDF5
output file on every periodic save, making cost grow with total run length
instead of just the new chunk. _update_chains_from_sampler now only
processes steps since the last save, and Results.save_results appends to
resizable HDF5 datasets instead of rewriting the file each time.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
… saves

Results.add_samples() used np.vstack/np.append, which reallocate and copy
the entire accumulated post/lnlike arrays on every call. This meant that
even after the earlier fix to periodic_save_freq (only recomputing/writing
new chunks), the in-memory accumulation itself was still O(total rows so
far) per periodic save, making the MCMC fit progressively slower as it ran.

add_samples now writes into a capacity-doubling internal buffer, so appends
are amortized O(1) per row instead of O(n) per call.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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