β‘ Profiler: Optimize Ising Model Evolution #365
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
π The Bottleneck:
SpinLattice::evolvewas performing tworng.gen_rangecalls per step (one forx, one fory), creating unnecessary overhead. Additionally, the hot loop performed multiple bounds checks for every spin update (center spin + 4 neighbors).π The Boost:
π» Technical Detail:
idxfrom0..total_spinsand derivexandyusing integer division/modulo. This reduces the number of calls to the random number generator by 50%.unsafe { *ptr.get_unchecked(idx) }for accessing the spin lattice and lookup tables. Sinceidxis generated from a valid range and neighbor coordinates are wrapped using modulo/logic, the bounds are guaranteed to be valid, allowing us to skip the checks safely.π§ͺ Verification:
bench_ising_customto verify the speedup.test_ising_evolve_orderedtomath_explorer/src/physics/stat_mech/ising.rswhich confirms that the optimizedevolvemethod still correctly simulates the ferromagnetic phase transition at low temperatures (magnetization > 0.8).PR created automatically by Jules for task 4510165448549268913 started by @fderuiter