[Python-ideas] More compact bytecode

Serhiy Storchaka storchaka at gmail.com
Thu Feb 4 17:11:07 EST 2016


On 04.02.16 23:43, Andrew Barnert via Python-ideas wrote:
> I just tried it out to see what's hard. And it seems like it's all easy. I was able to get it working (not passing all tests, of course--you'd have to fix dis, pdb, etc. for that) on my lunch hour.
>
>
> I didn't try to be smart anywhere: every opcode gets 8 bits for an arg, and uses up to three EXTENDED_ARG ops if it needs more. I didn't do any simplification, nor did I cast the bytecode to unsigned short *.
> The hardest changes are in ceval (plus frameobject--I didn't realize lasti has to start lasti at -2 instead of -1 until I tried to run it and immediately got errors about unknown opcodes at odd offsets).
>
> Compile is pretty easy by comparison.
>
> Nothing at all has to be done for bootstrap, freeze, marshal, etc. (unless you want to add backward-compat code to load non-word-based .pyc files).
>
> You'd also need to fix at least peephole (I just disabled it), dis, and pdb, but none of them look too hard.
>
> The bootstrap bytecode and stdlib .pyc files ends up about 5% smaller (and that's with the peephole optimizer disabled, so presumably it would be better in real life).
>
>
> If anyone wants to look at or play with the code, I'm in the process of regenerating my github fork, but it'll be branch wpy on https://github.com/abarnert/cpython once that's done (maybe 20 minutes? I forget how long cloning takes).

Looks good. Right now I'm implementing the first option (the hard part 
is peephole optimizer). We could compare both approaches later.




More information about the Python-ideas mailing list