Skip to content

meter: bound the meter caches and keep frozen hashes - #2202

Draft
float3 wants to merge 4 commits into
cuthbertLab:masterfrom
float3:help/meter-1972-cache-bounds
Draft

float3 wants to merge 4 commits into
cuthbertLab:masterfrom
float3:help/meter-1972-cache-bounds

Conversation

@float3

@float3 float3 commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

For #1972.

  • getMeterTerminal and makeSequence use lru_cache with 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.
  • MeterTerminal, MeterSequence, and FrozenDuration keep their hash in a _frozenHash slot, which _getSlotsRecursive leaves out, so equality and pickling ignore it. hash(TimeSignature('4/4').beatSequence) goes from 25 µs to 0.14 µs.

AI-assisted (Claude)

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.
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage is 93.301% — float3:help/meter-1972-cache-bounds into cuthbertLab:meter-speed-2026. No base build found for cuthbertLab:meter-speed-2026.

@mscuthbert mscuthbert left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great! Requesting making _frozenHash part of all FrozenObjects + also why lru_cache placement/helpers? otherwise fine THANKS!

Comment thread music21/meter/core.py Outdated
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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why can't the lru_cache go directly on getMeterTerminal?

Comment thread music21/common/objects.py Outdated
'''
__slots__: tuple[str, ...] = ()

_frozenHash: int

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 thread music21/meter/core.py Outdated
Comment on lines +2061 to +2062
@lru_cache(4096)
def _cachedSequence(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

again -- why not on makeSequence itself?

@mscuthbert
mscuthbert marked this pull request as draft November 2, 2026 23:54
@mscuthbert
mscuthbert deleted the branch cuthbertLab:meter-speed-2026 November 3, 2026 03:53
@mscuthbert mscuthbert closed this Nov 3, 2026
@mscuthbert mscuthbert reopened this Nov 3, 2026
@mscuthbert
mscuthbert changed the base branch from meter-speed-2026 to master November 3, 2026 04:01
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
@float3 float3 closed this Nov 4, 2026
@float3
float3 deleted the help/meter-1972-cache-bounds branch November 4, 2026 15:29
@float3
float3 restored the help/meter-1972-cache-bounds branch November 6, 2026 12:24
@float3

float3 commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor Author

accidentally closed; reopening

@float3 float3 reopened this Nov 6, 2026
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