Skip to content

Improve astuple performance for nested atomic sequences - #1636

Merged
hynek merged 3 commits into
python-attrs:mainfrom
Johnny-Kao:perf/astuple-nested-atomic
Oct 13, 2026
Merged

hynek merged 3 commits into
python-attrs:mainfrom
Johnny-Kao:perf/astuple-nested-atomic

Conversation

@Johnny-Kao

@Johnny-Kao Johnny-Kao commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Avoid calling has() for values whose exact type is already known to be atomic when attrs.astuple() traverses a nested sequence.

The top-level path already skips attrs detection for _ATOMIC_TYPES; this applies the same shortcut to sequence elements while preserving the existing fallback for all non-atomic values.

Benchmark

Local CPython 3.14 benchmark on list[int]:

Elements main this PR Speedup
10 0.914 µs 0.519 µs 1.76×
100 5.704 µs 1.931 µs 2.95×
1000 53.373 µs 16.037 µs 3.33×

A CodSpeed benchmark for nested atomic sequences is included.

Testing

  • uv run pytest -q: 1407 passed, 4 skipped, 2 xfailed
  • uv run pytest bench/test_benchmarks.py::test_astuple_nested_atomic -q
  • uv run ruff check src/attr/_funcs.py bench/test_benchmarks.py
  • uv run ruff format --check src/attr/_funcs.py bench/test_benchmarks.py
  • git diff --check

Pull Request Checklist

  • I acknowledge this project's AI policy.
  • This pull request is not from my main branch.
  • Tests / benchmarks cover the changed path.
  • No public API or typing changes.
  • Changelog fragment added.

@Johnny-Kao
Johnny-Kao marked this pull request as ready for review October 1, 2026 18:17
@hynek

hynek commented Oct 13, 2026

Copy link
Copy Markdown
Member

I'll need to start running codspeed on main (again?) so we have a baseline. here's my manual work:

CleanShot 2026-10-13 at 15 51 46@2x

@hynek
hynek added this pull request to the merge queue Oct 13, 2026
Merged via the queue into python-attrs:main with commit 8f76a52 Oct 13, 2026
20 checks passed
@Johnny-Kao

Copy link
Copy Markdown
Contributor Author

@hynek Thank you for the review. Will continue to contribute to this repo! Have a good day!

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