Skip to content

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

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Feb 16, 2018

Now for y in [expr] in comprehensions is so fast as a simple assignment y = expr.

https://bugs.python.org/issue32856

Now `for y in [expr]` in comprehensions is so fast as a simple
assignment `y = expr`.
@serhiy-storchaka serhiy-storchaka added the performance Performance or resource usage label Feb 16, 2018
@serhiy-storchaka serhiy-storchaka changed the title bpo-32856: Optimiz the idiom for assignment in comprehensions. bpo-32856: Optimize the idiom for assignment in comprehensions. Feb 16, 2018
break;
case Tuple_kind:
elts = gen->iter->v.Tuple.elts;
break;
Copy link
Member

Choose a reason for hiding this comment

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

How about we handle for x in {a} too?

Copy link
Member Author

Choose a reason for hiding this comment

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

This will change the behavior if a is not hashable.

@@ -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``.
Copy link
Member

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.

Copy link
Member Author

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.

@@ -81,6 +81,17 @@ def test_illegal_assignment(self):
compile("{x: y for y, x in ((1, 2), (3, 4))} += 5", "<test>",
"exec")

def test_assignment_idiom_in_comprehesions(self):

Choose a reason for hiding this comment

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

Typo: comprehesions

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

@serhiy-storchaka serhiy-storchaka deleted the optimize-assignment-in-comprehensions branch March 7, 2019 13:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting merge performance Performance or resource usage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants