Skip to content

Improve astuple performance for atomic dictionary keys and values - #1646

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

Johnny-Kao wants to merge 6 commits into
python-attrs:mainfrom
Johnny-Kao:perf/astuple-dict-atomic

Conversation

@Johnny-Kao

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

Copy link
Copy Markdown
Contributor

Summary

Hi again!

Following up on the merged #1636, I noticed that astuple() still performs unnecessary has() checks when processing dictionary keys and values.

I made the same small change for dictionary keys and values, so astuple() can skip unnecessary checks.

For dictionaries with 1,000 atomic entries, this makes it 2.62× faster, with no change in behavior.

Details (for reference only)

CPython 3.14, Linux, 1,000 dictionary entries:

Workload Before After Speedup
Atomic keys and values 164.8 µs 62.9 µs 2.62×
Mixed values 313.1 µs 234.9 µs 1.33×
OrderedDict (atomic) 175.4 µs 73.6 µs 2.38×

Validation:

Pull Request Checklist

  • I acknowledge this project's AI policy.
  • This pull request is not from my main branch.
  • There's tests for all new and changed code.
  • Changes or additions to public APIs are reflected in our type stubs (files ending in .pyi).
    • ...and used in the stub test file typing_tests/baseline.py or, if necessary, typing_tests/mypy.py.
    • If they've been added to attr/__init__.pyi, they've also been re-imported in attrs/__init__.pyi.
  • The documentation has been updated.
    • New functions/classes have to be added to docs/api.rst by hand.
    • Changes to the signatures of @attr.s() and @attrs.define() have to be added by hand too.
    • Changed/added classes/methods/functions have appropriate versionadded, versionchanged, or deprecated directives.
    • Documentation in .rst and .md files is written using semantic newlines.
  • Changes have news fragments in changelog.d.

@Johnny-Kao
Johnny-Kao marked this pull request as ready for review October 20, 2026 02:42
@Johnny-Kao

Copy link
Copy Markdown
Contributor Author

Just wanted to follow up on this PR since it's been open for about a month.
Let me know if you need any test required.

Happy to work on this more.

Thanks!

@codspeed

codspeed Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by ×3.2

2 warnings

⚠️ 2 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚠️ 1 benchmark with a significant change ran on different hardware

Its base and head results were measured on machines with a different CPU, core count or memory, so the change might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚡ 1 improved benchmark
✅ 18 untouched benchmarks
⏩ 2 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
⚡ CPU Simulation test_astuple_nested_atomic_dict 598.3 ms 184.6 ms ×3.2

Tip

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


Comparing Johnny-Kao:perf/astuple-dict-atomic (7eb4023) with main (f830e60)

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 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added a baseline benchmark to main and it seems to help but the tests and test docstrings violate basically all our rules. Please check out the contributing guide, I don't have time to do the fixups myself right now.

@Johnny-Kao

Copy link
Copy Markdown
Contributor Author

Thanks, Hynek. I've updated the tests and docstrings to follow the contributing guide, and fixed the changelog filename. CI is green now.

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.

2 participants