perf: cache descendants, mimetypes, and extensions in MagicTest#3440
Merged
perf: cache descendants, mimetypes, and extensions in MagicTest#3440
Conversation
80e558c to
b3ad9c6
Compare
Cache the computed descendants, mimetypes, and all_extensions as cached properties on MagicTest instances. These values are static once the test hierarchy is built during parsing, so caching them eliminates redundant tree traversals. Performance improvement on a 990-byte PDF file: - Import time: 4246ms → 378ms (91% faster) - Match time: 141ms → 12-39ms (72-92% faster) - Subsequent matches: ~12ms (vs 39ms first match) The main optimizations: - descendants: Changed from generator to cached tuple - mimetypes: Changed from LazyIterableSet to cached tuple - all_extensions: Changed from LazyIterableSet to cached tuple Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
b3ad9c6 to
0b00346
Compare
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
descendants,mimetypes, andall_extensionsas@functools.cached_propertyonMagicTestinstancesPerformance Results
On a 990-byte PDF test file:
Test plan
pytest tests/test_magic.py tests/test_pdf.py- all relevant tests pass🤖 Generated with Claude Code