- Notifications
You must be signed in to change notification settings - Fork 53
Closed
Description
When code is quickened we have two copies of the bytecode instructions.
code->co_code(a bytearray object)code->co_firstinstr(an array of instructions).
Apart from its length (which would be trivial to add to the code object), code->co_code can be reconstructed from code->co_firstinstr.
I propose changing the code object as follows:
Current:
... _Py_CODEUNIT*co_firstinstr; ... PyObject*co_code; /* Points to bytes object */ ... union_cache_or_instruction*co_quickened}Proposed:
intcode_length; ... /* Remove pointer to first instruction */ ... PyObject*co_code; /* Initially NULL, lazily initialized */ ... _Py_CODEUNITco_instructions[1]}Apart from the obvious memory saving, this also allows us to streamline the interpreter a bit.
Pseudo code for generating co_code:
co_code=bytearray(len=co.code_length*2) index=0forinstinco.co_instructions: opcode=OPCODE_TO_BASE_MAP[inst.opcode] ifhas_specialized_cache(inst.opcode): oparg=get_specialized_cache(inst).original_opargelifhas_oparg(inst.opcode): oparg=inst.opargelse: oparg=0co_code[index] =opcodeco_code[index+1] =opargindex+=1returnbytes(co_code)gvanrossum
Metadata
Metadata
Assignees
Labels
No labels
Type
Projects
Status
Done