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). |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
Wouldn't it be simpler to just use the high bits for these flags? |
Beta Was this translation helpful? Give feedback.
-
How would you test efficiently for |
Beta Was this translation helpful? Give feedback.
-
mask the high bits away. |
Beta Was this translation helpful? Give feedback.
-
Either of these changes will make it confusing for people when they look at refcnts in the debugger. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
Doesn't this break the ABI? |
Beta Was this translation helpful? Give feedback.
-
How much space do the various fields of an object header need?
1/2**n
is 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.
Beta Was this translation helpful? Give feedback.
All reactions