Repository navigation
Conversation
|
This is an interesting approach and definitely speeds things up. I reproduced the speedup locally. The shape of the fix worries me though. It re-implements a lot of I'm exploring making it so calling a slot wrapper itself is cheaper which I think will get these same results while pushing less complexity into all the slot implementations. |
|
@cmaloney I agree the inline implementation is too much. As I wrote in the description, I thought the speedup was worth the extra complexity, but it was a close call. Your general approach is better. I was not trying to speed up special methods in general, so I did not try a general approach. I will keep this PR open for now. If your approach solves this, feel free to close it. |
__setitem__and__delitem__share themp_ass_subscriptslot. If a Python subclass of a C type overrides only one of them, the inherited operation is still dispatched throughslot_mp_ass_subscript()and called through awrapper_descriptor.When lookup returns the expected unbound wrapper descriptor,
slot_mp_ass_subscript()now calls the wrapped C slot directly. Python overrides and other descriptors continue through the generic call path.Benchmark
I found this while profiling Counter-heavy code in a private project.
Counteroverrides__delitem__but inheritsdict.__setitem__, so several Counter operations go through this path.I compared the feature commit
8ca6cd042b1e1fe91e2751c5abf3204631c749c0with its direct parentb062727097e997bcb900e11503d3248daac903da. Both were release builds on macOS 15.7.1 arm64 with the same PGO/LTO configuration.c[key] = valuec[key] += 1c.update(mapping)c.subtract(mapping)a + ba | bCounter(iterable)andCounter.update(iterable)are not affected. They already use the_count_elements()C fast path.I also checked larger workloads with the same two builds. NetworkX
nx.triangles()was 5.1% to 9.0% faster across four graph shapes. The BPE benchmark in pyperformance was 6% to 8% faster in repeated runs.The same one sided
__setitem__/__delitem__pattern also exists outside Counter, but most of the other workloads I checked showed little or no end to end difference.Given these results, I think this optimization is worth the extra complexity in
slot_mp_ass_subscript(). The affected pattern is fairly narrow, but the improvement shows up in normal Counter operations and in larger workloads.(Updated on August 26, 2026 with additional Counter and real world benchmark results.)
Tests
Tests cover asymmetric
dictsubclasses, inherited slice deletion on alistsubclass, and fallback for custom or incompatible descriptors. The targeted tests pass on regular and free-threaded debug builds.Fixes #156250