Open source · MontePy · 1 min read
Making MontePy's collection renumbering scale
A quadratic slowdown in numbered-object collections, fixed upstream and refined through maintainer review.
Reported synthetic benchmark: 4,000 colliding appends · 695.001 s → 0.606 s (~1,150×)Benchmark comment
Read storyClose story
The motivating profile
Performance issue #786 identified quadratic scaling in repeated NumberedObjectCollection.append_renumber calls. The profile reported in that issue — not my measurement — showed append_renumber at 235.57 s cumulative in a 365.843 s model-building run with 10,000 cells.
The first fix
In PR #841 I improved available-number selection by tracking the last assigned number, while still honouring the requested start number when it was free.
What review caught
Review pointed out that optimizing check_number alone left a repeated scan in request_number, and flagged stale-cache risks because object numbers can mutate. Follow-up PR #867 used weak references to link objects back to their collection, keeping the cache consistent when numbers change.
The reported synthetic benchmark
PR #841's final benchmark is separate from the issue's profile: it used synthetic objects that repeatedly requested the same number, so every append hit a numbering collision. 4,000 appends took 695.001 s before and 0.606 s after — roughly 1,150× faster in that benchmark. The maintainer agreed the observed scaling looked O(N log N). It is not a simulation-dataset timing or a general guarantee.
Follow-up
Performance issue #895 and PR #915 removed the remaining O(N) fallback scans and relied on the authoritative cache. No separate benchmark is claimed for that change.