Skip to content

Performance: lazy-load material data on first access instead of at import#61

Merged
aidancrilly merged 6 commits intomasterfrom
copilot/improve-materials-loading-performance
Mar 1, 2026
Merged

Performance: lazy-load material data on first access instead of at import#61
aidancrilly merged 6 commits intomasterfrom
copilot/improve-materials-loading-performance

Conversation

Copy link
Contributor

Copilot AI commented Mar 1, 2026

All 5 default materials (H, D, T, C12, Be9) were loaded eagerly at import NeSST, adding ~500ms of I/O overhead even when only primary spectra were needed.

Changes

  • constants.py: Replace mat_dict = {} with mat_dict = LazyMaterialDict() — a dict subclass that loads material data on first key access via __missing__. Uses a deferred import of initialise_material_data from core to avoid circular imports at module-load time.
  • core.py: Remove the eager loading loop (for mat in default_mat_list: initialise_material_data(mat)).
  • tests/test_core.py: Add tests asserting mat_dict is a LazyMaterialDict, loads and caches on first access, and raises KeyError for unknown labels.

Behaviour

import NeSST as nst

# mat_dict is empty at import — no material I/O performed
assert len(nst.mat_dict) == 0

# Primary spectra work immediately with no material data loaded
mean, _, _ = nst.DTprimspecmoments(5e3)

# Material data loaded on first access, cached thereafter
mat_d = nst.mat_dict["D"]       # triggers load
mat_d2 = nst.mat_dict["D"]      # returns cached object instantly
assert mat_d is mat_d2

All modules sharing mat_dict via from NeSST.constants import * (including time_of_flight) reference the same LazyMaterialDict instance, so lazy loading is consistent across the package.

Original prompt

This section details on the original issue you should resolve

<issue_title>Performance: default materials loaded at import</issue_title>
<issue_description>Currently, all the default material data are loaded when NeSST is imported. This is slow if you just want primary spectra. A performance improvement would be to only load the data once it is required, i.e. once a call is made to the material data class for that material</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: aidancrilly <20111148+aidancrilly@users.noreply.github.com>
Copilot AI changed the title [WIP] Optimize default materials loading during import Performance: lazy-load material data on first access instead of at import Mar 1, 2026
@aidancrilly aidancrilly marked this pull request as ready for review March 1, 2026 09:28
@aidancrilly aidancrilly merged commit 9cc6ff5 into master Mar 1, 2026
2 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.

Performance: default materials loaded at import

2 participants