This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Mark.Shannon
Recipients Mark.Shannon, methane
Date 2021-09-17.12:47:38
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1631882858.41.0.966804501884.issue45233@roundup.psfhosted.org>
In-reply-to
Content
Currently, if a dictionary is split, then the dictionary owns the memory for the values. Unless the values is the unique empty-values array.

In order to support lazily created dictionaries for objects (see https://github.com/faster-cpython/ideas/issues/72#issuecomment-886796360), we need to allow shared keys dicts that do not own the memory of the values.

I propose the following changes to the internals of dict objects.
Add 4 flag bits (these can go in the low bits of the version tag)
2 bit for ownership of values, the other 2 bits for the stride of the values (1 or 3). All dictionaries would then have a non-NULL values pointer.

The value of index `ix` would be always located at `dict->ma_values[ix*stride]`

The two ownership bits indicate whether the dictionary owns the references and whether it owns the memory.
When a dictionary is freed, the items in the values array would be decref'd if the references are owned.
The values array would be freed if the memory is owned.

I don't think it is possible to own the memory, but not the references.

Examples:

A combined dict. Stride = 3, owns_refs = 1, owns_mem = 0.
A split keys dict. Stride = 1, owns_refs = 1, owns_mem = 1.
Empty dict (split). Stride = 1, owns_refs = 0, owns_mem = 0.

Dictionary with values embedded in object (https://github.com/faster-cpython/ideas/issues/72#issuecomment-886796360, second diagram). Stride = 1, owns_refs = 0, owns_mem = 0.
History
Date User Action Args
2021-09-17 12:47:38Mark.Shannonsetrecipients: + Mark.Shannon, methane
2021-09-17 12:47:38Mark.Shannonsetmessageid: <1631882858.41.0.966804501884.issue45233@roundup.psfhosted.org>
2021-09-17 12:47:38Mark.Shannonlinkissue45233 messages
2021-09-17 12:47:38Mark.Shannoncreate