bpo-32856: Optimize the idiom for assignment in comprehensions. by serhiy-storchaka · Pull Request #5695 · python/cpython
serhiy-storchaka
changed the title
bpo-32856: Optimiz the idiom for assignment in comprehensions.
bpo-32856: Optimize the idiom for assignment in comprehensions.
| break; | ||
| case Tuple_kind: | ||
| elts = gen->iter->v.Tuple.elts; | ||
| break; |
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?
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``. | |||
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.
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
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
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