Dict versioning tag in {“python” : 3.6}
2 min readMay 2, 2020
Hello readers, this blog post would explain the new feature dict versioning in python.
Added a new private version to the builtin dict type, incremented at each dictionary creation and at each dictionary change, to implement fast guards check on namespaces.
Properties/Benefits
- Dict lookups and execution could be skipped if version of dict is not changed.
- Version is globally unique, if dict is modified(updated, deleted or deleted) version will be changed.
- dictionary versions will benefit to implement optimizations.
- Below code is internal implementation of version tag in Cpython.
/* Data structure of dictionary in C *//* newly added ma_version_tag to PyDictObject for holding version number. (64-bit unsigned integer) */typedef struct {PyObject_HEADPy_ssize_t ma_used;/* Dictionary version: globally unique, value change each timethe dictionary is modified */uint64_t ma_version_tag;PyDictKeysObject *ma_keys;PyObject **ma_values;} PyDictObject;
- Example guard check using hypothetical functions dict_get_version()
UNSET = object()
class GuardDictKey:
def __init__(self, dict, key):
self.dict = dict
self.key = key
self.value = dict.get(key, UNSET)
self.version = dict_get_version(dict)
def check(self):
"""Return True if the dictionary entry did not change
and the dictionary was not replaced."""
# read the version of the dictionary
version = dict_get_version(self.dict)
if version == self.version:
# Fast-path: dictionary lookup avoided
return True
# lookup in the dictionary
value = self.dict.get(self.key, UNSET)
if value is self.value:
# another key was modified:
# cache the new dictionary version
self.version = version
return True
# the key was modified
return False
- dict version tag used internally not exposed to public.
[*] References