Skip to content

Speed up set operations on common built-in keys #162782

Not planned

Description

@josh-hurst

Set membership and insertion can dispatch through type slots even when an exact compact integer has a directly computable hash or a string or tuple already has a cached hash. Equal but distinct compact integer keys also reach generic rich comparison. Can we shorten these paths while preserving subclass behavior and the existing rules for comparisons that mutate a set?

Found and written by an AI optimization pipeline I run; I reviewed every line, ran the tests and sanitizers, and can answer for it.

Linked PRs

Activity

  1. added
    type-feature
    interpreter-core
    performance
    on Nov 28, 2026
  2. picnixz commented on Nov 28, 2026

    @picnixz
    Member

    Please provide benchmarks, pyperformance results, and use PGO/LTO (same story as for your other issues).

  3. added
    pending
    on Nov 28, 2026
  4. josh-hurst commented on Nov 28, 2026

    @josh-hurst
    Author

    Quick follow-up! I opened this based on a plain -O2 build, where the int set micros looked great and meteor_contest was about 3% faster. I then rebuilt base and patched with --enable-optimizations --with-lto (PROFILE_TASK="-m test --pgo --randseed 4109", Apple M4 Pro, Clang 21.0.0). The int paths are still faster there, but str and tuple lookups slip a bit (bm_set_ops_micro.py, -p 20 -w 1 -n 3, PYTHONHASHSEED=1):

    +-----------------------------+---------+-----------------------+
    | Benchmark                   | base    | patched               |
    +=============================+=========+=======================+
    | set_contains_int_hit        | 3.14 us | 2.35 us: 1.33x faster |
    +-----------------------------+---------+-----------------------+
    | set_contains_int_miss       | 3.94 us | 3.74 us: 1.05x faster |
    +-----------------------------+---------+-----------------------+
    | set_contains_str_hit        | 3.24 us | 3.38 us: 1.04x slower |
    +-----------------------------+---------+-----------------------+
    | set_contains_tuple_hit      | 6.00 us | 6.47 us: 1.08x slower |
    +-----------------------------+---------+-----------------------+
    | set_intersection_int        | 3.56 us | 2.59 us: 1.37x faster |
    +-----------------------------+---------+-----------------------+
    | set_difference_int          | 3.66 us | 2.64 us: 1.39x faster |
    +-----------------------------+---------+-----------------------+
    | set_issubset_int            | 1.75 us | 809 ns: 2.16x faster  |
    +-----------------------------+---------+-----------------------+
    | set_add_int_256             | 2.59 us | 2.43 us: 1.07x faster |
    +-----------------------------+---------+-----------------------+
    | set_contains_custom_control | 16.0 us | 15.9 us: 1.00x faster |
    +-----------------------------+---------+-----------------------+
    | Geometric mean              | (ref)   | 1.21x faster          |
    +-----------------------------+---------+-----------------------+
    

    The full pyperformance run is flat (geometric mean 1.00x slower, 0.9975x), and meteor_contest went from 48.8 ms -> 49.4 ms, so the -O2 win is gone. The str/tuple cost comes from the patch itself: set_keys_eq_fast checks PyLong_CheckExact before PyUnicode_CheckExact, and set_hash_key adds type branches ahead of PyObject_Hash. My guess is that PGO+LTO already gets most of what the manual inlining was buying, so all that's left is the extra branches. I haven't checked the generated code to confirm that, though.

    pyperformance, 57 zero-dep benchmarks (PGO+LTO)
    +-------------------------+------------+-----------------------+
    | Benchmark               | suite_base | suite_gh162782        |
    +=========================+============+=======================+
    | nbody                   | 42.7 ms    | 41.8 ms: 1.02x faster |
    +-------------------------+------------+-----------------------+
    | fannkuch                | 172 ms     | 174 ms: 1.01x slower  |
    +-------------------------+------------+-----------------------+
    | spectral_norm           | 42.1 ms    | 41.7 ms: 1.01x faster |
    +-------------------------+------------+-----------------------+
    | richards                | 20.5 ms    | 21.0 ms: 1.02x slower |
    +-------------------------+------------+-----------------------+
    | richards_super          | 23.5 ms    | 24.0 ms: 1.02x slower |
    +-------------------------+------------+-----------------------+
    | chaos                   | 26.0 ms    | 25.3 ms: 1.03x faster |
    +-------------------------+------------+-----------------------+
    | hexiom                  | 2.68 ms    | 2.66 ms: 1.01x faster |
    +-------------------------+------------+-----------------------+
    | float                   | 28.0 ms    | 27.4 ms: 1.02x faster |
    +-------------------------+------------+-----------------------+
    | go                      | 53.1 ms    | 52.4 ms: 1.01x faster |
    +-------------------------+------------+-----------------------+
    | regex_compile           | 53.0 ms    | 53.7 ms: 1.01x slower |
    +-------------------------+------------+-----------------------+
    | regex_effbot            | 1.27 ms    | 1.29 ms: 1.02x slower |
    +-------------------------+------------+-----------------------+
    | regex_v8                | 8.87 ms    | 8.90 ms: 1.00x slower |
    +-------------------------+------------+-----------------------+
    | meteor_contest          | 48.8 ms    | 49.4 ms: 1.01x slower |
    +-------------------------+------------+-----------------------+
    | mdp                     | 519 ms     | 526 ms: 1.01x slower  |
    +-------------------------+------------+-----------------------+
    | generators              | 17.7 ms    | 17.6 ms: 1.01x faster |
    +-------------------------+------------+-----------------------+
    | nqueens                 | 35.0 ms    | 34.4 ms: 1.02x faster |
    +-------------------------+------------+-----------------------+
    | comprehensions          | 6.51 us    | 6.55 us: 1.01x slower |
    +-------------------------+------------+-----------------------+
    | deepcopy                | 95.9 us    | 98.6 us: 1.03x slower |
    +-------------------------+------------+-----------------------+
    | deepcopy_reduce         | 1.03 us    | 1.07 us: 1.03x slower |
    +-------------------------+------------+-----------------------+
    | deepcopy_memo           | 11.8 us    | 12.6 us: 1.07x slower |
    +-------------------------+------------+-----------------------+
    | gc_traversal            | 1.78 ms    | 1.78 ms: 1.00x slower |
    +-------------------------+------------+-----------------------+
    | scimark_fft             | 117 ms     | 114 ms: 1.02x faster  |
    +-------------------------+------------+-----------------------+
    | scimark_lu              | 43.2 ms    | 39.8 ms: 1.09x faster |
    +-------------------------+------------+-----------------------+
    | scimark_sparse_mat_mult | 1.86 ms    | 1.76 ms: 1.05x faster |
    +-------------------------+------------+-----------------------+
    | pickle_list             | 2.26 us    | 2.29 us: 1.01x slower |
    +-------------------------+------------+-----------------------+
    | unpickle                | 6.16 us    | 6.10 us: 1.01x faster |
    +-------------------------+------------+-----------------------+
    | unpickle_list           | 2.01 us    | 2.03 us: 1.01x slower |
    +-------------------------+------------+-----------------------+
    | pickle_pure_python      | 140 us     | 141 us: 1.01x slower  |
    +-------------------------+------------+-----------------------+
    | logging_silent          | 42.2 ns    | 41.7 ns: 1.01x faster |
    +-------------------------+------------+-----------------------+
    | logging_simple          | 2.19 us    | 2.21 us: 1.01x slower |
    +-------------------------+------------+-----------------------+
    | pprint_safe_repr        | 313 ms     | 322 ms: 1.03x slower  |
    +-------------------------+------------+-----------------------+
    | pprint_pformat          | 643 ms     | 658 ms: 1.02x slower  |
    +-------------------------+------------+-----------------------+
    | xml_etree_parse         | 64.2 ms    | 66.7 ms: 1.04x slower |
    +-------------------------+------------+-----------------------+
    | xml_etree_iterparse     | 41.7 ms    | 42.4 ms: 1.02x slower |
    +-------------------------+------------+-----------------------+
    | xml_etree_process       | 24.6 ms    | 25.0 ms: 1.02x slower |
    +-------------------------+------------+-----------------------+
    | Geometric mean          | (ref)      | 1.00x slower          |
    +-------------------------+------------+-----------------------+
    
    Benchmark hidden because not significant (22): raytrace, deltablue, json_dumps, json_loads, pidigits, regex_dna, telco, pathlib, coroutines, async_generators, create_gc_cycles, pyflate, sqlite_synth, typing_runtime_protocols, btree, scimark_monte_carlo, scimark_sor, pickle, pickle_dict, unpickle_pure_python, logging_format, xml_etree_generate
    

    Honestly, flat overall and a bit slower for str and tuple keys isn't worth keeping open, so I'll close this. Thanks for taking a look!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-corependingperformancetype-feature

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions