Skip to content

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

Draft
float3 wants to merge 1 commit into
cuthbertLab:meter-speed-2026from
float3:help/meter-1972-cache-bounds
Draft

float3 wants to merge 1 commit into
cuthbertLab:meter-speed-2026from
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
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
'''
__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
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
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