Repository navigation
Cache parsed Pitch names; Speedup MusicMXL Style parsing - #2077
Merged
Merged
Conversation
The .name setter remembers each string it parses as (step, accidental
string, octave) in a module-level dict, cleared at 1000 entries. A
remembered name sets the attributes directly, makes a new Accidental
(they are mutable, so never shared), and informs the Note once. Names
that fail to parse are not remembered and raise as before.
Best of 7, master -> branch:
Pitch('D5') 543 -> 230 ns
Pitch('F#4') 691 -> 319 ns
p.name = 'F#4' 556 -> 172 ns
Note('F#4') 1812 -> 1382 ns
AI-assisted (Claude)
_parseName is now _cacheParsedName: it only parses and remembers (step, accidental string, octave). The .name setter reads the cache after a miss too, so it sets the attributes and informs the Note once even the first time, and a name that fails to parse leaves the pitch unchanged (before, D$ changed the step to D and then raised). Names over 100 characters raise a ValueError, so the cache cannot hold huge strings. setStyleAttributes returns early for a tag with no attributes (most <accidental>, <stem>, <type> tags): <accidental>natural</accidental> 1378 -> 491 ns; about 1-3% on whole MusicXML files. AI-assisted (Claude)
mscuthbert
marked this pull request as ready for review
October 10, 2026 04:02
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.
Faster pitches from names, part of the #2010 speedup work.
Pitch.nameremembers each string it parses as (step, accidental string, octave), up to 1000 strings, so a repeated name is one dict lookup. Each Pitch still gets its own Accidental.Pitch('F#4'), name seen beforePitch('F#4'), name not seen beforeNote('F#4')<accidental>natural</accidental>importsetStyleAttributesreturns early for a MusicXML tag with no attributes. 5% faster musicxml parsing on scores without attributes! (5.7ms before for bwv66.6 - 5.4ms after) -- Note that MuseScore writes a position attribute on nearly every tag, so there's no speedup in parsing MuseScore exports.AI-assisted (Claude)