Cache Lattice property (lengths/angles/volume) for much faster Structure.as_dict#4421
Merged
shyuep merged 8 commits intomaterialsproject:masterfrom May 30, 2025
Merged
Conversation
Lattice property: lengths/angles/volume
3f11e41 to
108fee6
Compare
Lattice property: lengths/angles/volumeLattice property (lengths/angles/volume) for much faster Structure.as_dict
Member
|
There is no need to put in a setter. The Lattice object is meant to be immutable like. There is no setter today and you cannot modify a matrix (without some weird hacky code like modifying variables indicated as private by convention). |
Contributor
Author
|
Good point! having matrix immutable makes caching much easier :), I didn't realize that |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
lengths/angles/volumeofLatticewould now be cached, related toorjsonfor much faster JSON operations? #4385verbosityinas_dictofPeriodicSite/Latticenow explicitly requires literal 0 or 1 to be consistent with docstring, instead of checkingif verbosity > 0(currently in grace period, only warning issued)pymatgen/src/pymatgen/core/lattice.py
Lines 904 to 905 in 34608d0
Cache frequently used
LatticepropertiesCurrently
length/angles/volumeis not cached and is frequently used, for example accessing all lattice parameter related property would lead tolength/anglesbeing repeatedly calculated:pymatgen/src/pymatgen/core/lattice.py
Lines 475 to 524 in 34608d0
structure.as_dictnow around 8x fasterBefore (1000 structure, each has 10-100 atoms):
Now:
Also note
lattice(the performance bottleneck) is not used in the dict for site:pymatgen/src/pymatgen/core/structure.py
Lines 2856 to 2857 in 34608d0
So we could modify
as_dictto control whether lattice would be generated at allThis could reduce the runtime slightly so I guess it's not worth the effort: