Compact object headers #125
Replies: 12 comments 3 replies
|
Saturating refcounts will slow down INCREF/DECREF everywhere (this is why Facebook's immortal objects aren't accepted). |
|
Doing something with The My current thinking on the "removal of PyGC_Head" idea is that it's probably not a win for memory usage. You gain by packing info compactly, rather than using whole machine words for things. However, there are other overheads that bring the memory usage back to similar levels. I think it's possible the alternative scheme could be faster. Manipulating and traversing double-linked lists is quite slow on modern CPUs. Memory bitmaps that define object sets could be faster. If you benchmark gcmodule.c, you should see that a lot of time is spent on the linked-list manipulation. |
I didn't say it would be fast 🙂 Just that's what would use the least amount of memory I don't know what the sweet spot that uses as little memory as possible without negatively impacting performance will be. |
|
The low 4 bits of the refcount can be freed for flags very easily: Three of these can be used for:
Which should speed up cycle GC interactions in allocation and deallocation. |
|
Wouldn't it be simpler to just use the high bits for these flags? |
|
How would you test efficiently for |
|
mask the high bits away. |
|
Either of these changes will make it confusing for people when they look at refcnts in the debugger. |
|
|
Given that immortal objects look likely, it makes sense to use saturating reference counts, as they use less memory. Here's a possible object header for 3.12: Flags would include:
GC objects would have a two word pre-header It might be cleaner to put the "dict or values" flag in flags, but that would need an extra memory read to access the values. |
|
The first step is to combine the dict and values pointer and move the weakref to that slot. PyDictValues *values;
PyObject *dict;
GC_HEAD;
intptr_t refcnt;
PyTypeObject *type;
...
PyWeakReference *weakref;becomes PyWeakReference *weakref;
PyDictOrValues *dict_or_values;
GC_HEAD;
intptr_t refcnt;
PyTypeObject *type;
...which reduces the size of the header from 7 (often taking 8) words to 6. |
|
Doesn't this break the ABI? |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
How much space do the various fields of an object header need?
1/2**nis the fraction of memory that can be allocated to objectsA 1 word header is going to be tricky and may well be so convoluted that it makes performance worse.
However I think the two word header is feasible.
The bits required for GC and finalization are:
Of course, aiming for a two word header doesn't mean we can't improve things incrementally.
A three word header would still be a significant improvement and would be significantly simpler to implement.
All reactions