Repository navigation
Conversation
getMeterTerminal and makeSequence keep the 1024 and 4096 most recently used objects with lru_cache, and TimeSignature.load keeps the defaults for the 512 most recently used meters. The music21/meter tests make about 440 terminals, 2950 sequences, and 94 meters; 161 corpus scores make 116, 376, and 27. FrozenObject.__hash__ hashed every slot, recursively, on each call. A subclass with a _frozenHash slot (MeterTerminal, MeterSequence, FrozenDuration) now keeps its hash there. _getSlotsRecursive leaves _frozenHash out, so equality, pickling, and the hash ignore it.
mscuthbert
requested changes
Nov 2, 2026
mscuthbert
left a comment
Member
There was a problem hiding this comment.
Great! Requesting making _frozenHash part of all FrozenObjects + also why lru_cache placement/helpers? otherwise fine THANKS!
Comment on lines
+463
to
+465
| @lru_cache(1024) | ||
| def _cachedMeterTerminal(numerator: int, denominator: int, weight: float) -> MeterTerminal: | ||
| return MeterTerminal(numerator=numerator, denominator=denominator, weight=weight) |
Member
There was a problem hiding this comment.
Why can't the lru_cache go directly on getMeterTerminal?
| ''' | ||
| __slots__: tuple[str, ...] = () | ||
|
|
||
| _frozenHash: int |
Member
There was a problem hiding this comment.
I like the _frozenHash idea. So much so, that I think it should be part of FrozenObject -- so every frozen object has a _frozenHash -- the __hash__ doesn't have to worry about AttributeErrors. But it does mean we need _frozenHash to be int|None
Comment on lines
+2061
to
+2062
| @lru_cache(4096) | ||
| def _cachedSequence( |
Member
There was a problem hiding this comment.
again -- why not on makeSequence itself?
…meter-1972-cache-bounds
FrozenObject.__new__ and __setstate__ set _frozenHash to None, and
__hash__ fills it on the first call, so __hash__ no longer catches
AttributeError. The slot itself stays in each subclass's __slots__:
FrozenDuration also inherits Duration's slots, and two bases with
non-empty __slots__ cannot be combined ("multiple bases have instance
lay-out conflict").
getMeterTerminal and makeSequence carry lru_cache themselves. lru_cache
keys on the call as written, so getMeterTerminal(1, 4) and
getMeterTerminal(1, 4, weight=1.0) are cached as two equal objects.
# Conflicts: # music21/common/objects.py # music21/meter/base.py # music21/meter/core.py # music21/meter/tests.py
Contributor
Author
|
accidentally closed; reopening |
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.
For #1972.
getMeterTerminalandmakeSequenceuselru_cachewith 1024 and 4096 entries, and the TimeSignature default cache keeps the 512 most recently used meters. The music21/meter tests make about 440 terminals, 2950 sequences, and 94 meters; 161 corpus scores make 116, 376, and 27._frozenHashslot, which_getSlotsRecursiveleaves out, so equality and pickling ignore it.hash(TimeSignature('4/4').beatSequence)goes from 25 µs to 0.14 µs.AI-assisted (Claude)