Replace ROT_xxx bytecodes with a single SWAP bytecode. #228
Replies: 10 comments 5 replies
-
Anyone know if a |
Beta Was this translation helpful? Give feedback.
-
There was no good reason. It was just what I knew from other interpreters (lost in the mist of times). |
Beta Was this translation helpful? Give feedback.
-
Hm, wouldn't this become
Cool! Not sure what you mean by "last resort", though. Rotations are currently pretty fundamental to how name captures work, so it would be nice to see some actual numbers before finally ditching |
Beta Was this translation helpful? Give feedback.
-
Do you plan to replace That (in combination with replacing |
Beta Was this translation helpful? Give feedback.
-
Ok. Here's a plan.
Steps 1 and 2 are independent and can be done in either order. |
Beta Was this translation helpful? Give feedback.
-
I think it is OK. There are no guarantees about when or where As an aside, we should probably ban most introspection and VM state changes in |
Beta Was this translation helpful? Give feedback.
-
Keyboard interrupts can only occur at known points, so peephole optimizer knows about them. Likewise for line events. Take the multiple assignment, The language specifies an order of events, so that in (a,
b) = t there is a "line" event after the assignment to For a, b = t There is no point at which you can observe that |
Beta Was this translation helpful? Give feedback.
-
FYI, I'm working on a patch for all of this. Should be up later today. |
Beta Was this translation helpful? Give feedback.
-
More aggressive swap folding: python/cpython#30970 |
Beta Was this translation helpful? Give feedback.
-
In general a swap is quicker than a rotate. A swap needs no loop and only two reads and writes:
I propose adding a
SWAP
bytecode which swaps the TOS with then
th item on the stack, and removingROT_TWO
,ROT_THREE
, andROT_N
.Replacing the simple rotates instructions with
SWAP
s is simple:ROT_TWO
=SWAP 2
ROT_THREE
=SWAP 3; SWAP 2
. GivenROT_THREE
is mostly used in combination with another instruction, this shouldn't cause an increase in code size:DUP_TOP; ROT_THREE
->DUP_TOP; SWAP 3
ROT_THREE; ROT_TWO
->SWAP 3
Removing
ROT_N
is a bit more complex, but usingROT_N
is already a last resort in the pattern matching compiler.A sequence of
SWAP
s is likely to be faster, even if it needs a few more instructions.Combined with
COPY
andPOP_TOP
, this provides the ability to perform arbitrary stack transformations at a cost that is linear in the number of instructions.In theory we could replace
NOP
withSWAP 1
, but givenNOP
s are already unwanted overhead, we don't want them any slower.Beta Was this translation helpful? Give feedback.
All reactions