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) = tthere is a "line" event after the assignment to For a, b = tThere 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.
-
Beta Was this translation helpful? Give feedback.
-
|
More aggressive swap folding: python/cpython#30970 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
In general a swap is quicker than a rotate. A swap needs no loop and only two reads and writes:
I propose adding a
SWAPbytecode which swaps the TOS with thenth item on the stack, and removingROT_TWO,ROT_THREE, andROT_N.Replacing the simple rotates instructions with
SWAPs is simple:ROT_TWO=SWAP 2ROT_THREE=SWAP 3; SWAP 2. GivenROT_THREEis mostly used in combination with another instruction, this shouldn't cause an increase in code size:DUP_TOP; ROT_THREE->DUP_TOP; SWAP 3ROT_THREE; ROT_TWO->SWAP 3Removing
ROT_Nis a bit more complex, but usingROT_Nis already a last resort in the pattern matching compiler.A sequence of
SWAPs is likely to be faster, even if it needs a few more instructions.Combined with
COPYandPOP_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
NOPwithSWAP 1, but givenNOPs are already unwanted overhead, we don't want them any slower.Beta Was this translation helpful? Give feedback.
All reactions