Skip to content

Cache generated method compilation - #1602

Open
KRRT7 wants to merge 2 commits into
python-attrs:mainfrom
KRRT7:perf/cache-generated-method-compilation
Open

Cache generated method compilation#1602
KRRT7 wants to merge 2 commits into
python-attrs:mainfrom
KRRT7:perf/cache-generated-method-compilation

Conversation

@KRRT7

@KRRT7 KRRT7 commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Summary

Cache compiled generated method scripts by script shape to avoid recompiling identical methods for repeated attrs class definitions.

  • Keep the cache bounded at 128 entries.
  • Preserve class-specific traceback and debugger filenames by rewriting cached code objects recursively.
  • Compile cache misses directly with their real filename, avoiding a regression for unique class shapes.

@codspeed-hq

codspeed-hq Bot commented Jul 31, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by ×2.9

⚡ 4 improved benchmarks
✅ 13 untouched benchmarks
⏩ 2 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation test_create_cached_property_class 6 s 1.8 s ×3.4
Simulation test_create_frozen_class 4.5 s 1.5 s ×2.9
Simulation test_create_simple_class_make_class 3.5 s 1.2 s ×2.9
Simulation test_create_simple_class 3.7 s 1.4 s ×2.6

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing KRRT7:perf/cache-generated-method-compilation (5c16645) with main (6851ab5)

Open in CodSpeed

Footnotes

  1. 2 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@hynek

hynek commented Jul 31, 2026

Copy link
Copy Markdown
Member

I think this is an artificial optimization. Unless I'm missing something, it only works when someone keep creating (not instantiating) the same class over and over again. We do that in benchmarks, but that's not something regular codes does. On the flip side, this creates a cache that every class pays for.

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