Skip to content

gh-149180: avoid double checking tp_as_number, tp_as_sequence and tp_as_mapping#149476

Open
NekoAsakura wants to merge 1 commit intopython:mainfrom
NekoAsakura:gh-149180/avoid-double-checking
Open

gh-149180: avoid double checking tp_as_number, tp_as_sequence and tp_as_mapping#149476
NekoAsakura wants to merge 1 commit intopython:mainfrom
NekoAsakura:gh-149180/avoid-double-checking

Conversation

@NekoAsakura
Copy link
Copy Markdown
Contributor

@NekoAsakura NekoAsakura commented May 7, 2026

Benchmark results
main branch %
map_check_int 8.51 ns 7.95 ns -6.5%
map_check_obj 8.50 ns 8.00 ns -6.0%
seq_check_obj 8.32 ns 7.92 ns -4.8%
seq_check_int 8.32 ns 7.94 ns -4.5%
seq_check_tuple 8.21 ns 7.94 ns -3.3%
seq_check_list 8.22 ns 8.01 ns -2.5%
map_check_dict 8.12 ns 7.93 ns -2.4%
map_check_tuple 8.09 ns 7.93 ns -1.9%
getitem_dict 12.92 ns 12.68 ns -1.9%
mul_str_int 14.74 ns 14.53 ns -1.4%
mul_int_str 14.70 ns 14.51 ns -1.3%
map_check_list 8.06 ns 7.96 ns -1.2%
mul_int_list 16.11 ns 15.98 ns -0.8%
mul_list_int 15.87 ns 15.76 ns -0.7%
getitem_list 3.55 ns 3.53 ns -0.4%
getitem_tuple 3.45 ns 3.44 ns -0.3%
getitem_str 3.51 ns 3.50 ns -0.1%
bool_none 6.78 ns 6.77 ns -0.1%
bool_dict 7.49 ns 7.50 ns +0.2%
bool_list 7.51 ns 7.54 ns +0.4%
seq_check_dict 8.32 ns 8.39 ns +0.9%
bool_obj 7.03 ns 7.19 ns +2.3%
bool_int 7.66 ns 7.96 ns +3.8%
Geomean −1.0%
Benchmark scripts

Build flags: ./configure --enable-experimental-jit --enable-optimizations --with-lto

import pyperf

r = pyperf.Runner()
I = "from _testlimitedcapi import "

for n, s in [("list", "v=[1]"), ("dict", "v={1:2}"), ("int", "v=5"),
             ("none", "v=None"), ("obj", "v=object()")]:
    r.timeit(f"bool_{n}", "bool(v)", s)

for n, s in [("list", "v=[1]"), ("tuple", "v=(1,)"), ("int", "v=5"),
             ("dict", "v={1:2}"), ("obj", "v=object()")]:
    r.timeit(f"seq_check_{n}", "f(v)", I + "sequence_check as f;" + s)
    r.timeit(f"map_check_{n}", "f(v)", I + "mapping_check as f;" + s)

for n, s in [("list", "v=[1,2,3]"), ("tuple", "v=(1,2,3)"),
             ("dict", "v={0:'a',1:'b'}"), ("str", "v='abc'")]:
    r.timeit(f"getitem_{n}", "v[1]", s)

for n, s in [("int_list", "a=2;b=[1]"), ("list_int", "a=[1];b=2"),
             ("int_str", "a=2;b='ab'"), ("str_int", "a='ab';b=2")]:
    r.timeit(f"mul_{n}", "a*b", s)

P.S.
Different build flags might affect the benchmarks, but I haven't had the time to test them yet.
I did see a regression on PyMapping_Check matching @picnixz's concern(#149317 (comment)) w/ --enable-experimental-jit (no PGO/LTO) build only, but I suspect my testing at the time wasn't sufficiently rigorous. Will take a closer look later.

@read-the-docs-community
Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #32576493 | 📁 Comparing 8cefc49 against main (b2582a6)

  🔍 Preview build  

40 files changed · ± 39 modified · - 1 deleted

± Modified

- Deleted

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants