Skip to content

Super-instructions #16

Closed
Closed
@gvanrossum

Description

@gvanrossum

Literature: look for work by Anton Ertl (Forth)

Basic idea: suppose in a runtime (!) execution profile we see that the sequence LOAD_FAST, LOAD_FAST occurs frequently. We can introduce a new opcode, LOAD_FAST_LOAD_FAST (LFLF), and blindly replace the first of every pair of LOAD_FAST opcodes with the super-instruction LFLF. The code then changes from e.g.

    LOAD_FAST 0 (x)
    LOAD_FAST 1 (y)
    BINARY_ADD

to

    LOAD_FAST_LOAD_FAST 0 (x)
    LOAD_FAST 1 (y)
    BINARY_ADD

The opcode case for LFLF would do the following:

  • load the first variable (x)
  • decode the argument for the second opcode
  • adjust the program counter to consume the second opcode
  • load the second variable(y)
  • DISPATCH()

This saves all the other stuff that's done for each opcode (tracing etc.), and it doesn't even read the second opcode. (If the second LOAD_FAST is preceded by an EXTENDED_ARG opcode we should not do the substitution, so that getting the argument for the second opcode is a single byte access and the p-counter update is a fixed add.)

The beauty is that if there's a jump to the second LOAD_FAST instruction, things just work, so this form of code rewriting can be very fast.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions