KEMBAR78
bpo-32856: Optimize the idiom for assignment in comprehensions. by serhiy-storchaka · Pull Request #5695 · python/cpython · GitHub
Skip to content

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.

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