Skip to content

Add instructions for creating small objects. #609

@markshannon

Description

@markshannon

When we use a small constant in Python code, we compile it as LOAD_CONST.
This looks efficient, but ignores the fact that the constant object has to be created by marshal, which is quite slow compared to the main interpreter.

This is fine for code in functions as they are expected to be executed many times, but for run once code, like module and class code, this is not so good.

Rather than have marshal create the object, to be put in a constants array to be used only once, we should create the objects in bytecode.

To do that, we want to add the following instructions:

  • LOAD_INT: Loads a small int (0-255)
  • MAKE_FLOAT: Makes a float.
  • MAKE_ASCII: Makes a string from ASCII data
  • MAKE_UNICODE: Makes a string from UTF-8 data
  • MAKE_LONG: Make a long object from an array of bytes.

These bytecodes are needed for #566, so this would be a useful halfway-house.

Where to put the data?
In #566 I suggest a separate array.
That won't work here because there is no separate array, but there is no reason why it cannot follow the instruction.
It would make disassembly harder, but shouldn't matter for performance, as we don't expect to be branching much in the run-once code.

For this to be efficient, we need to avoid copying and traversing the code of the code object too many times.
See #608 #462 #566 [Guido: I think that it's now #566, since #462 is closed] for how to deal with this.

This complements #583 which reduces code object size, and thus code object loading time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions