Skip to content

Faster startup -- Experiment E -- "deep-freeze" code objects as static C data structures #84

@gvanrossum

Description

@gvanrossum

This idea was @markshannon's, This a variant of something first proposed by Jeethu Rao of Facebook in https://bugs.python.org/issue34690. I'm just writing it up (and I may try to execute). The name "deep-freeze" is Mark's.

We could write a Python script that generates C code for a code object (including nested code objects). This would completely avoid the need for unmarshalling frozen modules (but only those). It would replace the current approach to freezing the marshalled code objects (which also generates C code, but it's just an array of bytes).

Suppose a simple code object has 4 bytes of data, "ABCD". We can then generate something like this:

static PyBytesObject co_code_1 = {
    .ob_refcnt = 999999999,
    .ob_type = &PyBytes_Type,
    .ob_size = 4,
    .ob_shash = -1,  // To be filled in dynamically
    .ob_sval = "ABCD"
};

static PyCodeObject code_1 = {
    ...
    co_code = &co_code_1,
    ...
};

(Lots of details left out, including the restructuring of object headers.)

An immediate concern here is the intended separation of static objects for multiple interpreters (@ericsnowcurrently). We're introducing something here that would be tricky to clone per interpreter. So maybe that kills the idea immediately?

Another concern is the initializer for ob_sval -- this field is actually declared as an array of 1 char, and the compiler will presumably balk if we put more in there. I think there's a solution though by declaring such bytes objects as a struct containing a modified header (omitting ob_sval) and a separate array of characters:

static struct {
    PyBytesObjectWithoutObSval head;  // PyBytesObject without ob_sval field
    char ob_sval[4+1];
} co_code_1 = {
    .head = {
        .ob_refcnt = 999999999,
        .ob_type = &PyBytes_Type,
        .ob_size = 4,
        .ob_shash = -1,
    },
    .ob_sval = "ABCD"
};

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