Skip to content

Commit

Permalink
optimize piping
Browse files Browse the repository at this point in the history
  • Loading branch information
thatbirdguythatuknownot committed Sep 1, 2023
1 parent 0b76892 commit 762398e
Show file tree
Hide file tree
Showing 17 changed files with 128 additions and 53 deletions.
2 changes: 1 addition & 1 deletion Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ atom[expr_ty]:
| 'None' { _PyAST_Constant(Py_None, NULL, EXTRA) }
| a='$' {
p->subn > 0 ?
_PyAST_Template(EXTRA) :
_PyAST_Template(0, EXTRA) :
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "template not in righthand side of pipe operation") }
| &(STRING|FSTRING_START) strings
| NUMBER
Expand Down
8 changes: 6 additions & 2 deletions Include/internal/pycore_ast.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Include/internal/pycore_ast_state.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Include/internal/pycore_opcode.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Include/internal/pycore_opcode_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 16 additions & 15 deletions Include/opcode_ids.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ def def_op(name, op):
def_op('BUILD_STRING', 157)
def_op('CONVERT_VALUE', 158)

def_op('SWAP_N', 159)

def_op('LIST_EXTEND', 162)
def_op('SET_UPDATE', 163)
def_op('DICT_MERGE', 164)
Expand Down
2 changes: 1 addition & 1 deletion Parser/Python.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ module Python
| FormattedValue(expr value, int conversion, expr? format_spec)
| JoinedStr(expr* values)
| Constant(constant value, string? kind)
| Template()
| Template(int last)

-- the following expression can appear in assignment context
| Attribute(expr value, identifier attr, expr_context ctx)
Expand Down
2 changes: 1 addition & 1 deletion Parser/parser.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 37 additions & 8 deletions Python/Python-ast.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 18 additions & 14 deletions Python/ast_opt.c
Original file line number Diff line number Diff line change
Expand Up @@ -691,17 +691,17 @@ fold_compare(expr_ty node, PyArena *arena, _PyASTOptimizeState *state)
} \
}

static int fold_comp_compinternal(comprehension_ty, int *, expr_ty,
static int fold_comp_compinternal(comprehension_ty, expr_ty *, expr_ty,
_PyASTOptimizeState *);
static int fold_comp_keywordinternal(keyword_ty, int *, expr_ty,
static int fold_comp_keywordinternal(keyword_ty, expr_ty *, expr_ty,
_PyASTOptimizeState *);
static int fold_comp_argumentsinternal(arguments_ty, int *, expr_ty,
static int fold_comp_argumentsinternal(arguments_ty, expr_ty *, expr_ty,
_PyASTOptimizeState *);
static int fold_comp_arginternal(arg_ty, int *, expr_ty,
static int fold_comp_arginternal(arg_ty, expr_ty *, expr_ty,
_PyASTOptimizeState *);

static int
fold_comp_internal(expr_ty node_, int *ptr, expr_ty sub,
fold_comp_internal(expr_ty node_, expr_ty *ptr, expr_ty sub,
_PyASTOptimizeState *state)
{
switch (node_->kind) {
Expand Down Expand Up @@ -814,7 +814,7 @@ fold_comp_internal(expr_ty node_, int *ptr, expr_ty sub,
if (sub) {
COPY_NODE(node_, sub);
}
*ptr = 1;
*ptr = node_;
break;
case Composition_kind:
case Name_kind:
Expand All @@ -826,7 +826,7 @@ fold_comp_internal(expr_ty node_, int *ptr, expr_ty sub,
}

static int
fold_comp_compinternal(comprehension_ty node_, int *ptr, expr_ty sub,
fold_comp_compinternal(comprehension_ty node_, expr_ty *ptr, expr_ty sub,
_PyASTOptimizeState *state)
{
CALL(node_->target);
Expand All @@ -836,15 +836,15 @@ fold_comp_compinternal(comprehension_ty node_, int *ptr, expr_ty sub,
}

static int
fold_comp_keywordinternal(keyword_ty node_, int *ptr, expr_ty sub,
fold_comp_keywordinternal(keyword_ty node_, expr_ty *ptr, expr_ty sub,
_PyASTOptimizeState *state)
{
CALL(node_->value);
return 1;
}

static int
fold_comp_argumentsinternal(arguments_ty node_, int *ptr, expr_ty sub,
fold_comp_argumentsinternal(arguments_ty node_, expr_ty *ptr, expr_ty sub,
_PyASTOptimizeState *state)
{
CALL_SEQ(fold_comp_arginternal, arg, node_->posonlyargs);
Expand All @@ -862,7 +862,7 @@ fold_comp_argumentsinternal(arguments_ty node_, int *ptr, expr_ty sub,
}

static int
fold_comp_arginternal(arg_ty node_, int *ptr, expr_ty sub,
fold_comp_arginternal(arg_ty node_, expr_ty *ptr, expr_ty sub,
_PyASTOptimizeState *state)
{
if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) {
Expand All @@ -878,7 +878,8 @@ fold_comp(expr_ty node, PyArena *arena, _PyASTOptimizeState *state)
{
expr_ty arg;
expr_ty func;
int ptr = 0, constant = 1;
expr_ty ptr = NULL;
int constant = 1;

arg = node->v.Composition.arg;
if (arg->kind != Constant_kind) {
Expand All @@ -902,9 +903,12 @@ fold_comp(expr_ty node, PyArena *arena, _PyASTOptimizeState *state)
node->v.Call.args = seq;
node->v.Call.keywords = NULL;
}
else if (constant) {
COPY_NODE(node, func);
return astfold_expr(node, arena, state); /* second pass */
else {
if (constant) {
COPY_NODE(node, func);
return astfold_expr(node, arena, state); /* second pass */
}
ptr->v.Template.last = 1;
}

return 1;
Expand Down
Loading

0 comments on commit 762398e

Please sign in to comment.