Skip to content

Defer imports on the cold import path - #1600

Merged
hynek merged 5 commits into
python-attrs:mainfrom
KRRT7:perf/defer-inspect-import-v2
Jul 31, 2026
Merged

Defer imports on the cold import path#1600
hynek merged 5 commits into
python-attrs:mainfrom
KRRT7:perf/defer-inspect-import-v2

Conversation

@KRRT7

@KRRT7 KRRT7 commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Summary

Further reduce import attr and import attrs startup time by:

  • Replacing the platform import with sys.implementation.name.
  • Deferring copy imports until assoc(), evolve(), or Attribute.evolve() is used.

@codspeed-hq

codspeed-hq Bot commented Jul 31, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 44.45%

⚡ 2 improved benchmarks
✅ 15 untouched benchmarks
⏩ 2 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
WallTime import attrs 127.3 ms 88 ms +44.65%
WallTime import attr 124.9 ms 86.5 ms +44.26%

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/defer-inspect-import-v2 (e685f6c) with main (ca0f045)

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.

@KRRT7

KRRT7 commented Jul 31, 2026

Copy link
Copy Markdown
Contributor Author

Merging this PR will improve performance by 46.76%

nice.

@KRRT7 KRRT7 changed the title Defer additional imports on the cold import path Defer imports on the cold import path Jul 31, 2026
@hynek

hynek commented Jul 31, 2026

Copy link
Copy Markdown
Member

hmmm I think we'll have to try this again:

CleanShot 2026-07-31 at 10 02 45@2x

I'll switch main to wall time and merge it here.

@KRRT7

KRRT7 commented Jul 31, 2026

Copy link
Copy Markdown
Contributor Author

it'll be interesting to see the result, but I don't think codspeed is a good tool for this, typically I use -X importtime and analyze the results.

@KRRT7

KRRT7 commented Jul 31, 2026

Copy link
Copy Markdown
Contributor Author

Merging this PR will improve performance by 66.22%

never mind, I agree, I agree /hj

@hynek

hynek commented Jul 31, 2026

Copy link
Copy Markdown
Member

they have to be fucking kidding me

CleanShot 2026-07-31 at 10 25 05@2x

but NOW we're nice :P

+69.33%

@hynek

hynek commented Jul 31, 2026

Copy link
Copy Markdown
Member

Well, that was a morning well spent! +44.65% lfg

@hynek
hynek added this pull request to the merge queue Jul 31, 2026
Merged via the queue into python-attrs:main with commit 6851ab5 Jul 31, 2026
21 checks passed
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