-
Notifications
You must be signed in to change notification settings - Fork 52
Maximumizing the advantages of (deep) freezing. #466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
There should be quite a few things we can make |
That makes a lot of sense. |
But what to do about string hashes? We can't precompute those because of hash randomization. |
They currently get implicitly computed when they are added to the global interned string table. This happens very early in interpreter startup so is safe to share between interpreters. We effectively make the main interpreter responsible for managing it and since main interpreter is destroyed at last (or at least it should be when per interp GIL is implemented) it is safe. |
Not all strings get interned currently -- only strings that generally qualify for interning, i.e. strings whose value looks like an identifier. We can fix that of course. But even if we fix this, string objects cannot be made part of the 'const' data segment in the linker. And until the |
@markshannon in the past, we've explored internally to move a couple of objects to the Re: code objects, I've explored some of these ideas in the past for the Language Summit presentation. One of the optimization techniques was freezing some of the internal objects in code object. Another option, is to freeze the code object itself to be able to remove the frame code object decref in However, we're not using any of these in the current immortal proposal since it probably requires its own discussion around how to properly deal with code objects under the assumption that we can make it (or parts of it) immortal. |
See #566 for how to handle code objects and still deep freeze much of the data. |
Is anyone currently working on this? If not, we might be interested in getting it in for 3.12. |
I don't know if anyone is working on this, but I anticipate a small glitch: currently, nested code objects are included in the co_consts tuple. If the idea is to share the constant portion of a deep-frozen code object between subinterpreters, we'd have to make an exception if co_consts contains other frozen code oobjects -- which would be the case for code objects associated with modules, classes (if they have methods), and anything containing nested functions, lambdas, generator expressions, or (unless/until PEP 709 is accepted) comprehensions. To get around this, we could just duplicate the co_consts tuple if it contains a code object (as indicated by co_flags) We'd also need to recurse down to apply the same treatment to the nested code objects. |
So I think there are two separable (but sequential) action items here:
|
I created python/cpython#102802 to track specifically (1) as a python/cpython issue. |
We've abandoned deep freezing. |
If we can declare data as
const
in the C code, it can be shared across multiple interpreters at zero cost, and across processes at the cost of a mmap (near zero).Unfortunately we can't do that for Python objects, at least not unless we have immortal objects.
There is also the degree of mutability to consider. Some objects (mainly strings) are immutable except for the hash code (and maybe refcount). Once the hashcode has been initialized, then they are immutable (except for the refcount). I refer to these objects as idempotent immutable; they are mutable, but become immutable once their hash has been taken.
There is little point in freezing mutable objects, as they cannot be shared and we need extra code to clean them up, e.g.
_PyStaticCode_Dealloc
Definitions:
const
: Fully immutable to both Python and C code. Can be stored asconst
data.Deep-freezing of objects
const
const
objects can be marked asconst
in the C source, which is the ideal for startup and sharing.Changing object layout to better use deep-freezing
Some objects are mutable but have large immutable parts. For example, code objects.
These objects should be laid out so that as much as possible of the object can be deep-frozen. Keeping the mutable parts in the object, with pointer(s) to the immutable part(s) should work well.
The text was updated successfully, but these errors were encountered: