Skip to content

Performance Optimization: Memory Pool & Early Termination#10

Merged
avdeevag merged 4 commits into
masterfrom
optimzation_memory_and_logic
Oct 16, 2025
Merged

Performance Optimization: Memory Pool & Early Termination#10
avdeevag merged 4 commits into
masterfrom
optimzation_memory_and_logic

Conversation

@mgyoo86

@mgyoo86 mgyoo86 commented Oct 15, 2025

Copy link
Copy Markdown
Member

Summary

Optimize PAM's pellet ablation routine (ablate!) with significant memory reduction and improved computational efficiency through allocation pooling and early termination logic.

Performance Improvements

Memory Allocation Reduction

ablate! function optimization:

Metric Before After Improvement
Allocations 48.27k 6.34k 86.9% ↓
Memory 11.065 MiB 326.953 KiB 97.1% ↓
Execution time 15.9 ms 3.0 ms 81.2% ↓

Key Changes

1. Allocation Pool Pattern

Pre-allocate and reuse buffers for pellet_density calculations across iterations.

Implementation:

mutable struct AllocationPool{T<:Real}
    nsource_buffer::Vector{T}
    nsource_max_len::Int
end

Impact:

  • Eliminates repeated allocations in hot loop
  • Reuses single buffer across all flux surfaces
  • Reduces memory fragmentation

2. Early Termination Logic

Detect pellet full ablation and terminate simulation immediately.

Implementation:

if pelt.radius[k] <= 0.0
    @info "Pellet fully ablated at t = $(pelt.time[k]) s"
    return
end

Impact:

  • Avoids unnecessary iterations after pellet disappears
  • Faster completion for high ablation rate scenarios
  • Prevents NaN propagation

3. Test Infrastructure

  • Add OMFIT PAM comparison tests for validation
  • Switch to HDF5 format for reduced memory footprint
  • Verify JSON/HDF5 format compatibility

Performance Profiling Note

While ablate! showed significant improvement (97% memory reduction), overall runtime is bottlenecked by IMAS.trace_surfaces, which consumes the majority of execution time in run_PAM.

Profiling breakdown:

  • IMAS.trace_surfaces: ~85-90% of total runtime
  • ablate! (optimized): ~10-15% of total runtime

Future optimization opportunities lie in the IMAS.trace_surfaces implementation.

- Initialize radius array to 0.0 with only t=0 set to initial radius
- Add early exit when pellet fully ablated with info logging
- Simplify pellet_density calls by removing NaN checks
Switch from JSON to HDF5 format for template_D3D_1layer_2species test data.
HDF5 provides better memory efficiency during loading, which will be
beneficial for precompilation and reducing test overhead.

Changes:
- Add template_D3D_1layer_2species.h5
- Update runtests.jl to use hdf2imas() instead of json2imas()
- Add equality test between JSON and HDF5 formats
- Apply HDF5 format to OMFIT PAM comparison tests

Benefits:
- Lower memory footprint during test initialization
- Faster loading times with HDF5 binary format
- Better compatibility with precompilation strategies
@mgyoo86 mgyoo86 requested review from avdeevag and orso82 October 15, 2025 22:10
@codecov

codecov Bot commented Oct 15, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 70.95%. Comparing base (b33c3b0) to head (110b41e).
⚠️ Report is 5 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #10      +/-   ##
==========================================
+ Coverage   70.82%   70.95%   +0.13%     
==========================================
  Files           1        1              
  Lines         329      334       +5     
==========================================
+ Hits          233      237       +4     
- Misses         96       97       +1     

☔ View full report in Codecov by Sentry.
📢 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.

@avdeevag avdeevag left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

works well, thanks!

@orso82

orso82 commented Oct 16, 2025

Copy link
Copy Markdown
Contributor

you should have merge permissions @avdeevag ;)

@avdeevag avdeevag merged commit 9ccd822 into master Oct 16, 2025
4 checks passed
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.

3 participants