Skip to content

gh-125063: Move slice constant-folding to AST #126830

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Lib/test/test_peepholer.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ def test_folding_of_binops_on_constants(self):
('a = 2+3', 5), # binary add
('a = 13-4', 9), # binary subtract
('a = (12,13)[1]', 13), # binary subscr
('a = (12,13)[:1]', (12,)), # binary subscr (slice)
('a = 13 << 2', 52), # binary lshift
('a = 13 >> 2', 3), # binary rshift
('a = 13 & 7', 5), # binary and
Expand Down Expand Up @@ -282,11 +283,17 @@ def test_binary_subscr_on_unicode(self):
self.assertInBytecode(code, 'LOAD_CONST', 'f')
self.assertNotInBytecode(code, 'BINARY_SUBSCR')
self.check_lnotab(code)

code = compile('"\u0061\uffff"[1]', '', 'single')
self.assertInBytecode(code, 'LOAD_CONST', '\uffff')
self.assertNotInBytecode(code,'BINARY_SUBSCR')
self.check_lnotab(code)

code = compile('"\u0061\uffffxx"[1::2]', '', 'single')
self.assertInBytecode(code, 'LOAD_CONST', '\uffffx')
self.assertNotInBytecode(code,'BINARY_SUBSCR')
self.check_lnotab(code)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking this test doesn't belong in test_peepholer now that the optimization is not part of the peephole optimiser.

Where are the other const folding tests?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, Irit.

This test should be re-written and placed in the test_ast/test_ast.py::ASTOptimiziationTests

# With PEP 393, non-BMP char get optimized
code = compile('"\U00012345"[0]', '', 'single')
self.assertInBytecode(code, 'LOAD_CONST', '\U00012345')
Expand Down
21 changes: 21 additions & 0 deletions Python/ast_opt.c
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,26 @@ fold_subscr(expr_ty node, PyArena *arena, _PyASTOptimizeState *state)
return make_const(node, newval, arena);
}

static int
fold_slice(expr_ty node, PyArena *arena, _PyASTOptimizeState *state)
{
expr_ty lower = node->v.Slice.lower;
expr_ty upper = node->v.Slice.upper;
expr_ty step = node->v.Slice.step;
if ((lower && lower->kind != Constant_kind) ||
(upper && upper->kind != Constant_kind) ||
(step && step->kind != Constant_kind))
{
return 1;
}

PyObject *newval = PySlice_New(
lower ? lower->v.Constant.value : Py_None,
upper ? upper->v.Constant.value : Py_None,
step ? step->v.Constant.value : Py_None);
return make_const(node, newval, arena);
}

/* Change literal list or set of constants into constant
tuple or frozenset respectively. Change literal list of
non-constants into tuple.
Expand Down Expand Up @@ -831,6 +851,7 @@ astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.lower);
CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.upper);
CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.step);
CALL(fold_slice, expr_ty, node_);
break;
case List_kind:
CALL_SEQ(astfold_expr, expr, node_->v.List.elts);
Expand Down
36 changes: 1 addition & 35 deletions Python/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -5039,23 +5039,10 @@ codegen_visit_expr(compiler *c, expr_ty e)
return SUCCESS;
}

static bool
is_constant_slice(expr_ty s)
{
return s->kind == Slice_kind &&
(s->v.Slice.lower == NULL ||
s->v.Slice.lower->kind == Constant_kind) &&
(s->v.Slice.upper == NULL ||
s->v.Slice.upper->kind == Constant_kind) &&
(s->v.Slice.step == NULL ||
s->v.Slice.step->kind == Constant_kind);
}

static bool
should_apply_two_element_slice_optimization(expr_ty s)
{
return !is_constant_slice(s) &&
s->kind == Slice_kind &&
return s->kind == Slice_kind &&
s->v.Slice.step == NULL;
}

Expand Down Expand Up @@ -5312,27 +5299,6 @@ codegen_slice(compiler *c, expr_ty s)
int n = 2;
assert(s->kind == Slice_kind);

if (is_constant_slice(s)) {
PyObject *start = NULL;
if (s->v.Slice.lower) {
start = s->v.Slice.lower->v.Constant.value;
}
PyObject *stop = NULL;
if (s->v.Slice.upper) {
stop = s->v.Slice.upper->v.Constant.value;
}
PyObject *step = NULL;
if (s->v.Slice.step) {
step = s->v.Slice.step->v.Constant.value;
}
PyObject *slice = PySlice_New(start, stop, step);
if (slice == NULL) {
return ERROR;
}
ADDOP_LOAD_CONST_NEW(c, LOC(s), slice);
return SUCCESS;
}

RETURN_IF_ERROR(codegen_slice_two_parts(c, s));

if (s->v.Slice.step) {
Expand Down
Loading