Skip to content

Conversation

@ESultanik
Copy link
Collaborator

Summary

  • Cache descendants, mimetypes, and all_extensions as @functools.cached_property on MagicTest instances
  • These values are static once the test hierarchy is built during parsing, so caching eliminates redundant tree traversals
  • Changed return types from generators/LazyIterableSet to tuples for immutability and faster iteration

Performance Results

On a 990-byte PDF test file:

Metric Before After Improvement
Import time 4246ms 378ms 91% faster
First match 141ms 39ms 72% faster
Subsequent matches 141ms 12ms 92% faster

Test plan

  • Run pytest tests/test_magic.py tests/test_pdf.py - all relevant tests pass
  • Profile import and match times to verify improvement
  • Verify caching works across multiple matches

🤖 Generated with Claude Code

@ESultanik ESultanik force-pushed the perf/cache-test-types branch 2 times, most recently from 80e558c to b3ad9c6 Compare January 20, 2026 22:02
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>
@ESultanik ESultanik force-pushed the perf/cache-test-types branch from b3ad9c6 to 0b00346 Compare January 20, 2026 22:15
@ESultanik ESultanik merged commit 3311444 into master Jan 20, 2026
10 checks passed
@ESultanik ESultanik deleted the perf/cache-test-types branch January 20, 2026 22:21
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.

2 participants