-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-126298: Don't deduplicate slice constants based on equality #126398
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
Changes from 5 commits
af304c8
1d55e6e
8d70094
1fc93c7
3cd03e0
b2b6149
3a08929
6eb1ffa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1384,53 +1384,90 @@ def check_op_count(func, op, expected): | |||||||||
| actual += 1 | ||||||||||
| self.assertEqual(actual, expected) | ||||||||||
|
|
||||||||||
| def check_num_consts(func, typ, expected): | ||||||||||
| num_consts = 0 | ||||||||||
| consts = func.__code__.co_consts | ||||||||||
| for instr in dis.Bytecode(func): | ||||||||||
| if instr.opname == "LOAD_CONST" and isinstance(consts[instr.oparg], typ): | ||||||||||
| num_consts += 1 | ||||||||||
| self.assertEqual(num_consts, expected) | ||||||||||
|
|
||||||||||
| def check_consts(func, typ, expected): | ||||||||||
| slice_consts = 0 | ||||||||||
| all_consts = set() | ||||||||||
|
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. I don't think this is working correctly - slices just defer to their contents for hash/equality, so this will be deduplicating the slices in the same way this is trying to fix. 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. Ooh, nice catch! |
||||||||||
| consts = func.__code__.co_consts | ||||||||||
| for instr in dis.Bytecode(func): | ||||||||||
| if instr.opname == "LOAD_CONST" and isinstance(consts[instr.oparg], typ): | ||||||||||
| slice_consts += 1 | ||||||||||
| self.assertEqual(slice_consts, expected) | ||||||||||
| all_consts.add(consts[instr.oparg]) | ||||||||||
| self.assertEqual(all_consts, expected) | ||||||||||
|
|
||||||||||
| def load(): | ||||||||||
| return x[a:b] + x [a:] + x[:b] + x[:] | ||||||||||
|
|
||||||||||
| check_op_count(load, "BINARY_SLICE", 3) | ||||||||||
| check_op_count(load, "BUILD_SLICE", 0) | ||||||||||
|
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.
Suggested change
|
||||||||||
| check_consts(load, slice, {slice(None, None, None)}) | ||||||||||
|
|
||||||||||
| def store(): | ||||||||||
| x[a:b] = y | ||||||||||
| x [a:] = y | ||||||||||
| x[a:] = y | ||||||||||
|
||||||||||
| x[:b] = y | ||||||||||
| x[:] = y | ||||||||||
|
|
||||||||||
| check_op_count(store, "STORE_SLICE", 3) | ||||||||||
| check_op_count(store, "BUILD_SLICE", 0) | ||||||||||
|
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.
Suggested change
|
||||||||||
| check_consts(store, slice, {slice(None, None, None)}) | ||||||||||
|
|
||||||||||
| def long_slice(): | ||||||||||
| return x[a:b:c] | ||||||||||
|
|
||||||||||
| check_op_count(long_slice, "BUILD_SLICE", 1) | ||||||||||
| check_op_count(long_slice, "BINARY_SLICE", 0) | ||||||||||
|
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.
Suggested change
|
||||||||||
| check_num_consts(long_slice, slice, 0) | ||||||||||
|
|
||||||||||
| def aug(): | ||||||||||
| x[a:b] += y | ||||||||||
|
|
||||||||||
| check_op_count(aug, "BINARY_SLICE", 1) | ||||||||||
| check_op_count(aug, "STORE_SLICE", 1) | ||||||||||
| check_op_count(aug, "BUILD_SLICE", 0) | ||||||||||
|
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.
Suggested change
|
||||||||||
| check_num_consts(long_slice, slice, 0) | ||||||||||
|
||||||||||
| check_num_consts(long_slice, slice, 0) | |
| check_num_consts(aug, slice, 0) |
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.
| check_op_count(aug_const, "STORE_SLICE", 0) | |
| check_op_count(aug_const, "STORE_SLICE", 0) | |
| check_op_count(aug_const, "BINARY_SUBSCR", 1) | |
| check_op_count(aug_const, "STORE_SUBSCR", 1) |
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.
| check_op_count(compound_const_slice, "BUILD_SLICE", 0) | |
| check_op_count(compound_const_slice, "BUILD_SLICE", 0) | |
| check_op_count(compound_const_slice, "STORE_SLICE", 0) | |
| check_op_count(compound_const_slice, "BINARY_SUBSCR", 1) |
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 think everywhere this is used it's called with
0expected. Maybe just replace those calls withcheck_consts(func, typ, set())since both functions are so similar?