-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
bpo-32856: Optimize the idiom for assignment in comprehensions. #5695
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
serhiy-storchaka
wants to merge
5
commits into
python:master
from
serhiy-storchaka:optimize-assignment-in-comprehensions
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
638cf8c
bpo-32856: Optimiz the idiom for assignment in comprehensions.
serhiy-storchaka 59fb617
Fix a bug and add tests.
serhiy-storchaka ae4af48
Fix typos.
serhiy-storchaka 1d54e7b
Merge branch 'master' into optimize-assignment-in-comprehensions
serhiy-storchaka 37ba353
Merge branch 'master' into optimize-assignment-in-comprehensions
serhiy-storchaka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Core and Builtins/2018-02-16-10-44-24.bpo-32856.UjR8SD.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Optimized the idiom for assignment a temporary variable in comprehensions. | ||
| Now ``for y in [expr]`` in comprehensions is so fast as a simple assignment | ||
| ``y = expr``. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -200,11 +200,13 @@ static int compiler_set_qualname(struct compiler *); | |
| static int compiler_sync_comprehension_generator( | ||
| struct compiler *c, | ||
| asdl_seq *generators, int gen_index, | ||
| int depth, | ||
| expr_ty elt, expr_ty val, int type); | ||
|
|
||
| static int compiler_async_comprehension_generator( | ||
| struct compiler *c, | ||
| asdl_seq *generators, int gen_index, | ||
| int depth, | ||
| expr_ty elt, expr_ty val, int type); | ||
|
|
||
| static PyCodeObject *assemble(struct compiler *, int addNone); | ||
|
|
@@ -3875,22 +3877,24 @@ compiler_call_helper(struct compiler *c, | |
| static int | ||
| compiler_comprehension_generator(struct compiler *c, | ||
| asdl_seq *generators, int gen_index, | ||
| int depth, | ||
| expr_ty elt, expr_ty val, int type) | ||
| { | ||
| comprehension_ty gen; | ||
| gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); | ||
| if (gen->is_async) { | ||
| return compiler_async_comprehension_generator( | ||
| c, generators, gen_index, elt, val, type); | ||
| c, generators, gen_index, depth, elt, val, type); | ||
| } else { | ||
| return compiler_sync_comprehension_generator( | ||
| c, generators, gen_index, elt, val, type); | ||
| c, generators, gen_index, depth, elt, val, type); | ||
| } | ||
| } | ||
|
|
||
| static int | ||
| compiler_sync_comprehension_generator(struct compiler *c, | ||
| asdl_seq *generators, int gen_index, | ||
| int depth, | ||
| expr_ty elt, expr_ty val, int type) | ||
| { | ||
| /* generate code for the iterator, then each of the ifs, | ||
|
|
@@ -3918,12 +3922,38 @@ compiler_sync_comprehension_generator(struct compiler *c, | |
| } | ||
| else { | ||
| /* Sub-iter - calculate on the fly */ | ||
| VISIT(c, expr, gen->iter); | ||
| ADDOP(c, GET_ITER); | ||
| /* Fast path for the temporary variable assignment idiom: | ||
| for y in [f(x)] | ||
| */ | ||
| asdl_seq *elts; | ||
| switch (gen->iter->kind) { | ||
| case List_kind: | ||
| elts = gen->iter->v.List.elts; | ||
| break; | ||
| case Tuple_kind: | ||
| elts = gen->iter->v.Tuple.elts; | ||
| break; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about we handle
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will change the behavior if |
||
| default: | ||
| elts = NULL; | ||
| } | ||
| if (asdl_seq_LEN(elts) == 1) { | ||
| expr_ty elt = asdl_seq_GET(elts, 0); | ||
| if (elt->kind != Starred_kind) { | ||
| VISIT(c, expr, elt); | ||
| start = NULL; | ||
| } | ||
| } | ||
| if (start) { | ||
| VISIT(c, expr, gen->iter); | ||
| ADDOP(c, GET_ITER); | ||
| } | ||
| } | ||
| if (start) { | ||
| depth++; | ||
| compiler_use_next_block(c, start); | ||
| ADDOP_JREL(c, FOR_ITER, anchor); | ||
| NEXT_BLOCK(c); | ||
| } | ||
| compiler_use_next_block(c, start); | ||
| ADDOP_JREL(c, FOR_ITER, anchor); | ||
| NEXT_BLOCK(c); | ||
| VISIT(c, expr, gen->target); | ||
|
|
||
| /* XXX this needs to be cleaned up...a lot! */ | ||
|
|
@@ -3937,7 +3967,7 @@ compiler_sync_comprehension_generator(struct compiler *c, | |
|
|
||
| if (++gen_index < asdl_seq_LEN(generators)) | ||
| if (!compiler_comprehension_generator(c, | ||
| generators, gen_index, | ||
| generators, gen_index, depth, | ||
| elt, val, type)) | ||
| return 0; | ||
|
|
||
|
|
@@ -3952,18 +3982,18 @@ compiler_sync_comprehension_generator(struct compiler *c, | |
| break; | ||
| case COMP_LISTCOMP: | ||
| VISIT(c, expr, elt); | ||
| ADDOP_I(c, LIST_APPEND, gen_index + 1); | ||
| ADDOP_I(c, LIST_APPEND, depth + 1); | ||
| break; | ||
| case COMP_SETCOMP: | ||
| VISIT(c, expr, elt); | ||
| ADDOP_I(c, SET_ADD, gen_index + 1); | ||
| ADDOP_I(c, SET_ADD, depth + 1); | ||
| break; | ||
| case COMP_DICTCOMP: | ||
| /* With 'd[k] = v', v is evaluated before k, so we do | ||
| the same. */ | ||
| VISIT(c, expr, val); | ||
| VISIT(c, expr, elt); | ||
| ADDOP_I(c, MAP_ADD, gen_index + 1); | ||
| ADDOP_I(c, MAP_ADD, depth + 1); | ||
| break; | ||
| default: | ||
| return 0; | ||
|
|
@@ -3972,15 +4002,18 @@ compiler_sync_comprehension_generator(struct compiler *c, | |
| compiler_use_next_block(c, skip); | ||
| } | ||
| compiler_use_next_block(c, if_cleanup); | ||
| ADDOP_JABS(c, JUMP_ABSOLUTE, start); | ||
| compiler_use_next_block(c, anchor); | ||
| if (start) { | ||
| ADDOP_JABS(c, JUMP_ABSOLUTE, start); | ||
| compiler_use_next_block(c, anchor); | ||
| } | ||
|
|
||
| return 1; | ||
| } | ||
|
|
||
| static int | ||
| compiler_async_comprehension_generator(struct compiler *c, | ||
| asdl_seq *generators, int gen_index, | ||
| int depth, | ||
| expr_ty elt, expr_ty val, int type) | ||
| { | ||
| comprehension_ty gen; | ||
|
|
@@ -4024,9 +4057,10 @@ compiler_async_comprehension_generator(struct compiler *c, | |
| NEXT_BLOCK(c); | ||
| } | ||
|
|
||
| depth++; | ||
| if (++gen_index < asdl_seq_LEN(generators)) | ||
| if (!compiler_comprehension_generator(c, | ||
| generators, gen_index, | ||
| generators, gen_index, depth, | ||
| elt, val, type)) | ||
| return 0; | ||
|
|
||
|
|
@@ -4041,18 +4075,18 @@ compiler_async_comprehension_generator(struct compiler *c, | |
| break; | ||
| case COMP_LISTCOMP: | ||
| VISIT(c, expr, elt); | ||
| ADDOP_I(c, LIST_APPEND, gen_index + 1); | ||
| ADDOP_I(c, LIST_APPEND, depth + 1); | ||
| break; | ||
| case COMP_SETCOMP: | ||
| VISIT(c, expr, elt); | ||
| ADDOP_I(c, SET_ADD, gen_index + 1); | ||
| ADDOP_I(c, SET_ADD, depth + 1); | ||
| break; | ||
| case COMP_DICTCOMP: | ||
| /* With 'd[k] = v', v is evaluated before k, so we do | ||
| the same. */ | ||
| VISIT(c, expr, val); | ||
| VISIT(c, expr, elt); | ||
| ADDOP_I(c, MAP_ADD, gen_index + 1); | ||
| ADDOP_I(c, MAP_ADD, depth + 1); | ||
| break; | ||
| default: | ||
| return 0; | ||
|
|
@@ -4119,7 +4153,7 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type, | |
| ADDOP_I(c, op, 0); | ||
| } | ||
|
|
||
| if (!compiler_comprehension_generator(c, generators, 0, elt, | ||
| if (!compiler_comprehension_generator(c, generators, 0, 0, elt, | ||
| val, type)) | ||
| goto error_in_scope; | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update the NEWS entry with some benchmark results.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what should I provide. The optimized code always is the part of complex comprehension expression.