Skip to content

Declare istr with __slots__ = () in _multidict_py.py#1361

Open
jonathandung wants to merge 7 commits into
aio-libs:masterfrom
jonathandung:master
Open

Declare istr with __slots__ = () in _multidict_py.py#1361
jonathandung wants to merge 7 commits into
aio-libs:masterfrom
jonathandung:master

Conversation

@jonathandung

Copy link
Copy Markdown

What do these changes do?

Optimizes memory usage by preventing a dictionary to store attributes to be created on every istr instance.

Are there changes in behavior for the user?

Users can no longer set attributes on the keys of the Python version of case-insensitive multidicts, mutable or otherwise.

Related issue number

Fixes #1360.

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes

@jonathandung
jonathandung requested a review from asvetlov as a code owner June 9, 2026 11:48
@jonathandung
jonathandung requested a review from webknjaz as a code owner June 9, 2026 11:53
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Jun 9, 2026

@Vizonex Vizonex 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.

If your going to use slots I would recommend trying to add __istr_identitiy__ to it. While I like your idea for speeding up pure python the tests reflect the issue.

AttributeError: 'istr' object attribute '__istr_identity__' is read-only

A Way you could slove this one is to try

__slots__ = ("__istr_identity__",)

Comment thread multidict/_multidict_py.py Outdated
jonathandung and others added 2 commits June 10, 2026 13:16
Co-authored-by: Vizonex <114684698+Vizonex@users.noreply.github.com>
@jonathandung

Copy link
Copy Markdown
Author

Wait, but it's impossible to define a default value for a slot by setting it on the class, since that overwrites the slot. I changed the relevant logic a bit.

@codspeed-hq

codspeed-hq Bot commented Jun 10, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 21.09%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

❌ 11 regressed benchmarks
✅ 231 untouched benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
test_iterate_multidict_keys[ci-py] 245.1 µs 346.6 µs -29.29%
test_iterate_multidict[ci-py] 246.2 µs 346.1 µs -28.86%
test_iterate_multidict_items[ci-py] 264.4 µs 364.3 µs -27.42%
test_keys_view_less_or_equal[ci-py] 285.2 µs 383.3 µs -25.59%
test_keys_view_less[ci-py] 290.8 µs 389.8 µs -25.4%
test_items_view_less[ci-py] 335 µs 437.1 µs -23.35%
test_items_view_less_or_equal[ci-py] 332.6 µs 433.6 µs -23.28%
test_multidict_popitem_str[ci-py] 675.8 µs 781.6 µs -13.54%
test_keys_view_sub[ci-py] 1 ms 1.2 ms -11.92%
test_keys_view_or[ci-py] 1.1 ms 1.3 ms -9.83%
test_items_view_or[ci-py] 1.2 ms 1.4 ms -9.75%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing jonathandung:master (2de45be) with master (c63dfaf)1

Open in CodSpeed

Footnotes

  1. No successful run was found on master (a4271e8) during the generation of this report, so c63dfaf was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

Comment thread multidict/_multidict_py.py Outdated
Comment thread multidict/_multidict_py.py
@Vizonex

Vizonex commented Jun 13, 2026

Copy link
Copy Markdown
Member

@codspeedbot fix this regression

@jonathandung

Copy link
Copy Markdown
Author

I'm not familiar with this benchmarking framework, and I don't understand what it means by "different runtime environments detected".

@jonathandung

Copy link
Copy Markdown
Author

I have done all I can, and I think the regression was caused by CodSpeed running on a different virtual CPU. I'm not sure though.

@Vizonex

Vizonex commented Jun 13, 2026

Copy link
Copy Markdown
Member

@webknjaz Any thoughts on this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add an empty tuple as __slots__ to istr

2 participants