Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit slices as constants in the bytecode compiler #125063

Closed
mdboom opened this issue Oct 7, 2024 · 2 comments
Closed

Emit slices as constants in the bytecode compiler #125063

mdboom opened this issue Oct 7, 2024 · 2 comments
Assignees
Labels
performance Performance or resource usage type-feature A feature request or enhancement

Comments

@mdboom
Copy link
Contributor

mdboom commented Oct 7, 2024

Feature or enhancement

Proposal:

Constant slices are currently emitted as multiple bytecodes by the Python bytecode compiler.

For example, x[:-2, 1:-1] generates:

              LOAD_CONST               4 (0)
              LOAD_CONST               5 (-2)
              BUILD_SLICE              2
              LOAD_CONST               1 (1)
              LOAD_CONST               2 (-1)
              BUILD_SLICE              2
              BUILD_TUPLE              2

Then tuples of slices are constant like this, this could instead be compiled down to a single LOAD_CONST instruction:

              LOAD_CONST                1 ((slice(0, -2, None), slice(1, -1, None))

According to @fberlakovich and Stefan Brunthaler's paper on Cross module quickening, this can have a significant impact on Numpy benchmarks.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs

@mdboom mdboom added type-feature A feature request or enhancement performance Performance or resource usage labels Oct 7, 2024
@mdboom mdboom self-assigned this Oct 7, 2024
mdboom added a commit that referenced this issue Oct 8, 2024
* Make slices marshallable

* Emit slices as constants

* Update Python/marshal.c

Co-authored-by: Peter Bierma <[email protected]>

* 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 <[email protected]>
Co-authored-by: Peter Bierma <[email protected]>
@nineteendo
Copy link
Contributor

Shouldn't this be closed now?

@mdboom mdboom closed this as completed Oct 8, 2024
@mdboom
Copy link
Contributor Author

mdboom commented Oct 8, 2024

Shouldn't this be closed now?

Yep -- sorry, I forgot that step.

efimov-mikhail pushed a commit to efimov-mikhail/cpython that referenced this issue Oct 9, 2024
…ython#125064)

* Make slices marshallable

* Emit slices as constants

* Update Python/marshal.c

Co-authored-by: Peter Bierma <[email protected]>

* 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 <[email protected]>
Co-authored-by: Peter Bierma <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance or resource usage type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants