Skip to content

Emit slices as constants in the bytecode compiler #125063

Description

@mdboom

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

Metadata

Metadata

Assignees

Labels

performancePerformance or resource usagetype-featureA feature request or enhancement

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions