faster-cpython / ideas

1.67k stars 49 forks source link

Per-object flags #616

Open markshannon opened 10 months ago

markshannon commented 10 months ago

This presumes a one-time ABI breakage, moving to abi4. Such a breakage seems likely in order to make no-gil feasible. If it doesn't happen, then the following is moot.

We change the object header from:

struct _object {
    intptr_t ob_refcount;
    PyTypeObject *ob_type;
};

to


struct _object {
    uint32_t ob_refcount;
    uint32_t ob_flags;
    PyTypeObject *ob_type;
};

which is the same size on 64 bit platforms, and one word larger on 32 bit platforms.

We can do a lot of performance optimizations with those bits. First of all, setting them during object creation should be free, or close to it, as we need to set the ref count at the same time.

Then we can make many improvements, such as:

Possible flags:

Related issue on object layout: https://github.com/faster-cpython/ideas/issues/553

gvanrossum commented 10 months ago

I expect this will have to wait until we've stopped supporting abi3. During the time when abi3 and abi4 coexist, compatibility with abi3 will presumably require us to keep the layout of the ob_refcnt field unchanged. Let's discuss in Brno when we can drop abi3 -- we could conceivably do this overnight (i.e., in 3.13) as long as we preserve source compatibility. Or perhaps there are pressing reasons (like promises to the users of abi3? I have no idea what we've promised) to keep it around longer.