gh-151907: Avoid creating temporary objects in some comprehensions#151908
gh-151907: Avoid creating temporary objects in some comprehensions#151908ZeroIntensity wants to merge 7 commits into
Conversation
| VISIT(c, expr, elt); | ||
| ADDOP(c, elt_loc, POP_TOP); | ||
| break; | ||
| } |
There was a problem hiding this comment.
Can we instead just wrap the LIST_APPEND in if !avoid_creation?
There was a problem hiding this comment.
I think we'd also need to wrap the LIST_EXTEND with that too; I opted for this approach to avoid duplicating that logic.
But do we need the separate VISIT path for the Starred case at all? I might be able to refactor it to look like this:
ISIT(c, expr, elt);
if (elt->kind == Starred_kind) {
op = LIST_EXTEND;
}
else {
op = LIST_APPEND;
}
if (!avoid_creation) {
ADDOP_I(c, elt_loc, op, depth + 1);
}|
I think this might change semantics for the dict case - if a key is not hashable. |
|
Yeah, good catch. This breaks {x for x in [[]]} # No error!I'll just remove |
This was a breaking change because hash() was no longer called on the elements.
Maybe also add tests for these cases. |
| ] | ||
| self.codegen_test(snippet, expected) | ||
|
|
||
| def test_no_target_comp_optimization(self): |
There was a problem hiding this comment.
I believe you need to add a test where the loop body has a side effect. IIUC, the body with thread.join() should be turned into a method call, rather than just pass.
The following code:
is essentially turned into: