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
bpo-46528: Simplify the VM's stack manipulations #30902
Merged
Merged
+285
−320
Conversation
This file contains 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
sweeneyde
reviewed
Jan 26, 2022
sweeneyde
reviewed
Jan 26, 2022
|
I threw together a test for the basic def permutation(swaps, N=None):
if N is None:
N = max(swaps)
perm = list(range(N))
for s in swaps:
perm[0], perm[s-1] = perm[s-1], perm[0]
return perm
def swaptimize(*old_swaps):
assert old_swaps
if len(old_swaps) == 1:
return tuple(old_swaps)
perm = permutation(old_swaps)
swaps = []
for i, x in enumerate(perm):
# March forward, searching for an un-traversed cycle.
if x is None or x == i:
continue
# Traverse this cycle.
j = i
while True:
if j != 0:
swaps.append(j + 1)
if perm[j] is None:
# Back to the start of the cycle.
assert j == i
break
next_j = perm[j]
perm[j] = None
j = next_j
swaps = tuple(reversed(swaps))
assert permutation(old_swaps, len(perm)) == permutation(swaps, len(perm))
assert len(swaps) <= len(old_swaps)
return swaps
assert swaptimize(1) == (1,) # This should be ()?
assert swaptimize(2) == (2,)
assert swaptimize(3) == (3,)
assert swaptimize(10) == (10,)
assert swaptimize(2, 2, 5, 5) == ()
assert swaptimize(10, 20, 20, 10) == ()
assert swaptimize(2, 3, 4, 3) == (3, 4, 3, 2)
from random import randint
for i in range(100_000):
for n in range(1, 20):
swaptimize(*[randint(2, 20) for _ in range(n)]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

...as discussed in faster-cpython/ideas#228:
DUP_TOPwithCOPY(1).DUP_TOP_TWOwithCOPY(2), COPY(2).SWAPinstruction.ROT_TWOwithSWAP(2).ROT_THREEwithSWAP(3), SWAP(2).ROT_FOUR.ROT_N(n)withSWAP(n), SWAP(n - 1), ..., SWAP(2).SWAPinstructions.https://bugs.python.org/issue46528
The text was updated successfully, but these errors were encountered: