Skip to content

gh-125063: Emit slices as constants in the bytecode compiler - #125064

Merged
mdboom merged 7 commits into
python:mainfrom
mdboom:marshal-slice
Oct 8, 2024
Merged

mdboom merged 7 commits into
python:mainfrom
mdboom:marshal-slice

Conversation

@mdboom

@mdboom mdboom commented Oct 7, 2024 •

Copy link
Copy Markdown
Contributor

This emits slices as constants in the compiler (when the slices only contain constant members).

I measured a 10% speedup on the cavity_flow benchmark in npbench, which not surprisingly uses a lot of slices, but this kind of code is fairly idiomatic for Numpy. (EDIT: The benchmark as a standalone script)

This does measure as are 1% slower on pyperformance, but that seems largely due to unpack_sequence. I'm going to rerun on more platforms, and also collect stats, since I would expect a measurable reduction in instructions executed.

Comment thread Python/marshal.c Resolved
Comment thread Python/codegen.c Resolved
Comment thread Python/codegen.c Resolved
mdboom and others added 3 commits October 7, 2024 18:28
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
always has the same net effect
@mdboom
mdboom requested a review from iritkatriel October 8, 2024 12:51
Comment thread Python/codegen.c Resolved
@mdboom

mdboom commented Oct 8, 2024

Copy link
Copy Markdown
Contributor Author

Here are the largest stat changes:

Name Before After Change
BINARY_SLICE 1,144,280,918 237,406,036 -79.3%
BINARY_SUBSCR 2,095,449,827 3,002,218,240 43.3%
STORE_SLICE 162,427,163 149,989,760 -7.7%
LOAD_CONST 18,743,983,320 17,819,791,524 -4.9%
EXTENDED_ARG 481,860,317 466,468,380 -3.2%
STORE_SUBSCR 876,092,109 888,534,141 1.4%
BUILD_SLICE 211,754,494 209,634,774 -1.0%
@mdboom
mdboom merged commit c6127af into python:main Oct 8, 2024
efimov-mikhail pushed a commit to efimov-mikhail/cpython that referenced this pull request Oct 9, 2024
…ython#125064)

* Make slices marshallable

* Emit slices as constants

* Update Python/marshal.c

Co-authored-by: Peter Bierma <zintensitydev@gmail.com>

* Refactor codegen_slice into two functions so it
always has the same net effect

* Fix for free-threaded builds

* Simplify marshal loading of slices

* Only return SUCCESS/ERROR from codegen_slice

---------

Co-authored-by: Mark Shannon <mark@hotpy.org>
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

4 participants