Skip to content

Add a list fast path for STORE_SLICE #162784

Not planned
Not planned

Description

@josh-hurst

List slice assignment builds a temporary slice even when the target is an exact list and the replacement has the same length. This shows up in fannkuch, which repeatedly reverses short prefixes. On an M4 Pro, a prototype reduced median fannkuch(9) time from 191.765 ms to 162.340 ms across five paired runs.

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
    performance
    interpreter-core
    on Nov 28, 2026
  2. picnixz commented on Nov 28, 2026

    @picnixz
    Member

    Please share benchmarks. How does it impact pyperformance suite for instance? is it in a PGO/LTO build? what's the actual geometric mean of various test cases? we need more evidence before making performance changes. I don't know how often we replace a list's slice with the same length though. It may be too specific for a task.

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

    @josh-hurst
    Author

    I reran this on PGO+LTO builds (--enable-optimizations --with-lto, base d88a6b9c3e, M4 Pro, Clang 21.0.0). Across 57 zero-dep pyperformance benchmarks, the geomean is 1.01x faster (1.0066x), almost all from fannkuch (172 ms -> 145 ms, 1.18x faster). Without it, the other 56 are flat (1.0037x).

    Microbenchmarks (-p 20 -w 1 -n 3):

    +-------------------------------+---------+-----------------------+
    | Benchmark                     | base    | patched               |
    +===============================+=========+=======================+
    | list_store_slice_same_4       | 79.6 ns | 71.4 ns: 1.12x faster |
    +-------------------------------+---------+-----------------------+
    | list_store_slice_same_16      | 136 ns  | 132 ns: 1.03x faster  |
    +-------------------------------+---------+-----------------------+
    | list_store_slice_tuple_4      | 81.4 ns | 73.4 ns: 1.11x faster |
    +-------------------------------+---------+-----------------------+
    | list_store_slice_resize       | 42.7 ns | 40.2 ns: 1.06x faster |
    +-------------------------------+---------+-----------------------+
    | bytearray_store_slice_control | 180 ns  | 174 ns: 1.04x faster  |
    +-------------------------------+---------+-----------------------+
    | Geometric mean                | (ref)   | 1.06x faster          |
    +-------------------------------+---------+-----------------------+
    
    Benchmark hidden because not significant (1): list_store_slice_large_100
    
    Full pyperformance results (57 zero-dep benchmarks, PGO+LTO)
    +--------------------------+------------+-----------------------+
    | Benchmark                | suite_base | suite_gh162784        |
    +==========================+============+=======================+
    | nbody                    | 42.7 ms    | 42.0 ms: 1.02x faster |
    +--------------------------+------------+-----------------------+
    | fannkuch                 | 172 ms     | 145 ms: 1.18x faster  |
    +--------------------------+------------+-----------------------+
    | spectral_norm            | 42.1 ms    | 41.8 ms: 1.01x faster |
    +--------------------------+------------+-----------------------+
    | richards_super           | 23.5 ms    | 23.7 ms: 1.01x slower |
    +--------------------------+------------+-----------------------+
    | deltablue                | 1.47 ms    | 1.42 ms: 1.04x faster |
    +--------------------------+------------+-----------------------+
    | chaos                    | 26.0 ms    | 25.1 ms: 1.03x faster |
    +--------------------------+------------+-----------------------+
    | hexiom                   | 2.68 ms    | 2.67 ms: 1.00x faster |
    +--------------------------+------------+-----------------------+
    | float                    | 28.0 ms    | 27.5 ms: 1.02x faster |
    +--------------------------+------------+-----------------------+
    | go                       | 53.1 ms    | 51.7 ms: 1.03x faster |
    +--------------------------+------------+-----------------------+
    | regex_effbot             | 1.27 ms    | 1.29 ms: 1.02x slower |
    +--------------------------+------------+-----------------------+
    | regex_v8                 | 8.87 ms    | 8.92 ms: 1.01x slower |
    +--------------------------+------------+-----------------------+
    | telco                    | 2.85 ms    | 2.82 ms: 1.01x faster |
    +--------------------------+------------+-----------------------+
    | meteor_contest           | 48.8 ms    | 48.6 ms: 1.01x faster |
    +--------------------------+------------+-----------------------+
    | coroutines               | 10.5 ms    | 10.3 ms: 1.02x faster |
    +--------------------------+------------+-----------------------+
    | nqueens                  | 35.0 ms    | 33.9 ms: 1.03x faster |
    +--------------------------+------------+-----------------------+
    | comprehensions           | 6.51 us    | 6.55 us: 1.01x slower |
    +--------------------------+------------+-----------------------+
    | deepcopy                 | 95.9 us    | 95.4 us: 1.01x faster |
    +--------------------------+------------+-----------------------+
    | deepcopy_memo            | 11.8 us    | 11.8 us: 1.01x faster |
    +--------------------------+------------+-----------------------+
    | pyflate                  | 184 ms     | 186 ms: 1.01x slower  |
    +--------------------------+------------+-----------------------+
    | typing_runtime_protocols | 43.9 us    | 43.1 us: 1.02x faster |
    +--------------------------+------------+-----------------------+
    | scimark_fft              | 117 ms     | 116 ms: 1.01x faster  |
    +--------------------------+------------+-----------------------+
    | scimark_lu               | 43.2 ms    | 42.7 ms: 1.01x faster |
    +--------------------------+------------+-----------------------+
    | scimark_sor              | 50.0 ms    | 49.2 ms: 1.02x faster |
    +--------------------------+------------+-----------------------+
    | scimark_sparse_mat_mult  | 1.86 ms    | 1.88 ms: 1.01x slower |
    +--------------------------+------------+-----------------------+
    | pickle                   | 5.70 us    | 5.80 us: 1.02x slower |
    +--------------------------+------------+-----------------------+
    | pickle_list              | 2.26 us    | 2.27 us: 1.01x slower |
    +--------------------------+------------+-----------------------+
    | unpickle                 | 6.16 us    | 6.12 us: 1.01x faster |
    +--------------------------+------------+-----------------------+
    | unpickle_pure_python     | 90.8 us    | 90.0 us: 1.01x faster |
    +--------------------------+------------+-----------------------+
    | logging_silent           | 42.2 ns    | 40.7 ns: 1.04x faster |
    +--------------------------+------------+-----------------------+
    | pprint_safe_repr         | 313 ms     | 312 ms: 1.00x faster  |
    +--------------------------+------------+-----------------------+
    | pprint_pformat           | 643 ms     | 638 ms: 1.01x faster  |
    +--------------------------+------------+-----------------------+
    | xml_etree_parse          | 64.2 ms    | 66.7 ms: 1.04x slower |
    +--------------------------+------------+-----------------------+
    | xml_etree_iterparse      | 41.7 ms    | 42.8 ms: 1.03x slower |
    +--------------------------+------------+-----------------------+
    | xml_etree_generate       | 34.9 ms    | 34.4 ms: 1.02x faster |
    +--------------------------+------------+-----------------------+
    | Geometric mean           | (ref)      | 1.01x faster          |
    +--------------------------+------------+-----------------------+
    
    Benchmark hidden because not significant (23): raytrace, richards, json_dumps, json_loads, pidigits, regex_dna, regex_compile, mdp, pathlib, generators, async_generators, deepcopy_reduce, create_gc_cycles, gc_traversal, sqlite_synth, btree, scimark_monte_carlo, pickle_dict, unpickle_list, pickle_pure_python, logging_simple, logging_format, xml_etree_process
    
    Microbenchmark script (bm_list_store_slice_micro.py)
    import pyperf
    
    
    def bench_replace_same_len_4(loops):
        a = list(range(64))
        rep = [10, 20, 30, 40]
        t0 = pyperf.perf_counter()
        for _ in range(loops):
            a[4:8] = rep
            a[8:12] = rep
            a[12:16] = rep
            a[16:20] = rep
        return pyperf.perf_counter() - t0
    
    
    def bench_replace_same_len_16(loops):
        a = list(range(64))
        rep = list(range(16))
        t0 = pyperf.perf_counter()
        for _ in range(loops):
            a[0:16] = rep
            a[16:32] = rep
            a[32:48] = rep
            a[48:64] = rep
        return pyperf.perf_counter() - t0
    
    
    def bench_replace_tuple_4(loops):
        a = list(range(64))
        rep = (10, 20, 30, 40)
        t0 = pyperf.perf_counter()
        for _ in range(loops):
            a[4:8] = rep
            a[8:12] = rep
            a[12:16] = rep
            a[16:20] = rep
        return pyperf.perf_counter() - t0
    
    
    def bench_replace_resize_oscillate(loops):
        a = list(range(32))
        grow = [1, 2, 3, 4, 5, 6]
        shrink = [1, 2]
        t0 = pyperf.perf_counter()
        for _ in range(loops):
            a[4:6] = grow
            a[4:10] = shrink
        return pyperf.perf_counter() - t0
    
    
    def bench_replace_large_100(loops):
        a = list(range(256))
        rep = list(range(100))
        t0 = pyperf.perf_counter()
        for _ in range(loops):
            a[10:110] = rep
        return pyperf.perf_counter() - t0
    
    
    def bench_bytearray_control(loops):
        b = bytearray(64)
        rep = b"abcd"
        t0 = pyperf.perf_counter()
        for _ in range(loops):
            b[4:8] = rep
            b[8:12] = rep
            b[12:16] = rep
            b[16:20] = rep
        return pyperf.perf_counter() - t0
    
    
    if __name__ == "__main__":
        runner = pyperf.Runner()
        runner.bench_time_func("list_store_slice_same_4", bench_replace_same_len_4)
        runner.bench_time_func("list_store_slice_same_16", bench_replace_same_len_16)
        runner.bench_time_func("list_store_slice_tuple_4", bench_replace_tuple_4)
        runner.bench_time_func("list_store_slice_resize", bench_replace_resize_oscillate)
        runner.bench_time_func("list_store_slice_large_100", bench_replace_large_100)
        runner.bench_time_func("bytearray_store_slice_control", bench_bytearray_control)

    Honestly, I agree the same-length <= 32 special case is too narrow to carry just for fannkuch, and good catch on the FT refcounts in #162785 (_STORE_SLICE shouldn't be stealing start/stop). _BINARY_SLICE right above it just borrows the indices and hands exact lists off to a ~20-line _PyList_BinarySlice helper. Would a minimal _PyList_StoreSlice mirroring that (just calling list_ass_slice, no same-length special case or sliceobject.c changes) be worth measuring? If STORE_SLICE on lists still feels too narrow either way, no worries, happy to close this out.

  5. picnixz commented on Nov 29, 2026

    @picnixz
    Member

    Would a minimal _PyList_StoreSlice mirroring that (just calling list_ass_slice, no same-length special case or sliceobject.c changes) be worth measuring

    That's not me who should answer. As the issue submitter, you should decide what you want to submit. I would say yes in this case, but this kind of question (that looks like an LLM one) is something you (as a hman) should be able to answer on your side.

    As a matter of fact, I don't think we need a special path for storing slices. It's a rare operation anyway, so I will close this issue.

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