Skip to content

Conversation

@fderuiter
Copy link
Owner

πŸ“‰ The Bottleneck:
SpinLattice::evolve was performing two rng.gen_range calls per step (one for x, one for y), creating unnecessary overhead. Additionally, the hot loop performed multiple bounds checks for every spin update (center spin + 4 neighbors).

πŸš€ The Boost:

  • Before: ~25.74 M/s (Million updates per second)
  • After: ~26.83 M/s
  • Improvement: ~4.2% speedup

πŸ’» Technical Detail:

  1. RNG Optimization: Refactored the coordinate generation to sample a single random index idx from 0..total_spins and derive x and y using integer division/modulo. This reduces the number of calls to the random number generator by 50%.
  2. Bounds Check Elimination: Utilized unsafe { *ptr.get_unchecked(idx) } for accessing the spin lattice and lookup tables. Since idx is 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:

  • Ran bench_ising_custom to verify the speedup.
  • Added test_ising_evolve_ordered to math_explorer/src/physics/stat_mech/ising.rs which confirms that the optimized evolve method still correctly simulates the ferromagnetic phase transition at low temperatures (magnetization > 0.8).
  • Existing tests passed.

PR created automatically by Jules for task 4510165448549268913 started by @fderuiter

- Combined x/y coordinate generation into a single flat index generation to reduce RNG calls.
- Replaced safe indexing with `unsafe { get_unchecked }` in the hot loop for `spins` and `lut` access.
- Added regression test `test_ising_evolve_ordered`.
- Updated `.jules/profiler.md` with performance log.

Verification:
- Benchmarks show ~4.2% speedup (25.7 M/s -> 26.8 M/s).
- Tests passed.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
@google-labs-jules
Copy link
Contributor

πŸ‘‹ Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a πŸ‘€ emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

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.

2 participants