Skip to content

Improve astuple performance for nested atomic sequences - #1636

Open
Johnny-Kao wants to merge 2 commits into
python-attrs:mainfrom
Johnny-Kao:perf/astuple-nested-atomic
Open

Johnny-Kao wants to merge 2 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

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

This branch has not been deployed

No deployments
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.

1 participant