-
-
Notifications
You must be signed in to change notification settings - Fork 31.5k
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
Play around with variable-length instructions (DRAFT!) #101160
Conversation
(This version is a one-word instruction that only extends oparg3, so we can replace EXTENDED_ARG with it.)
This is not yet fully successful
Some things aren't supported yet, and others are just super inelegant. But the basics seem to work. Phew!
BTW I didn't bother to fix test_dis.py, it's a chore. |
Some next steps:
A big open question: What will become of the code that "searches backwards" for the full oparg (in get_arg() in frameobject.c)? I've always thought that was dicey, but it may become untenable (even for unquickened code). |
It's now done in the compiler.
Don't delete it, since this is only an experiment. But assure it's not used anywhere.
Oh, that should just be done by properly decoding the instructions as we go in mark_stacks(). Basically 3-arg instructions invalidate all bytecode scanning code that assumes every word is an instruction. (UPDATE) There's another bytecode scanner in |
As an alternative: can we keep fixed-length instructions but compress the whole pyc file with zlib instead? The procedure would be: serialize the whole pyc-file into a memory buffer and deflate in onto a disc; to load it back inflate it into the memory in full, close the file and do the rest as usual. It should work because benefits of memory mapping for execution is just an idea for now. Moreover, it would be irrelevant with faster-cpython/ideas#538 allowing to drop the whole inflated buffer immediately after quickening all its content. |
But what length should instructions have? 4 bytes still isn't enough for operators like BINARY_OP_R that need four arguments (and yes, we debated endlessly if we could do it with three -- the answer is, not easily). There's also cache sizes. Compressing with zlib is a sepatate topic, we could do that at any time but we've never felt that disk space for PYC files is much of a problem, so why would we spend the extra CPU and memory on (de)compression. Also, you probably meant to follow up in one of the "ideas" issues, not in this PR. |
According to the test output, test_dis is the only failing test. I'm still not doing anything about it. |
My bad; I posted an answer on instruction length in faster-cpython/ideas#540 (comment). |
As this PR is in limbo while I figure out how to complete this work, I might just save some less complicated pieces of it in a new PR. |
This picks a silly example intentionally: replace the sequence
LOAD_CONST
,MAKE_FUNCTION
with a variable-length instructionMAKE_FUNCTION_FROM_CODE
. The replacement is done as part of quickening (so effectively at the end of compilation). I plan to implement faster-cpython/ideas#540 and faster-cpython/ideas#539 (though the current code forEXTENDED_ARG_3
is different -- it only extends oparg3, so that I can patch it on top ofEXTENDED_ARG
when quickening).Things I've learned so far:
locale.py
has lots of constants so there was an interesting bug (now fixed) when we hadEXTENDED_ARG 5
,LOAD_CONST n
,MAKE_FUNCTION 0
.make regen-opcode regen-opcode-targets
.