It would be nice to hear Barry Warsow who was opposed to the PEP in january. He wanted to wait until FAT Python was proven to really be faster, which is still not case right now. (I mean that I didnt't run seriously benchmarks, but early macro benchmarks are not really promising, only micro benchmarks. I expect better results when the implemenation will be more complete.)<div><br></div><div>The main change since january is that Yury wrote a patch making method calls using the PEP.<div><a href="https://mail.python.org/pipermail/python-dev/2016-January/142772.html">https://mail.python.org/pipermail/python-dev/2016-January/142772.html</a></div><div><br></div><div>Victor<br><br>Le jeudi 14 avril 2016, Guido van Rossum <<a href="mailto:guido@python.org">guido@python.org</a>> a écrit :<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I'll wait a day before formally pronouncing to see if any objections<br>
are made, but it looks good to me.<br>
<br>
On Thu, Apr 14, 2016 at 8:19 AM, Victor Stinner<br>
<<a href="javascript:;" onclick="_e(event, 'cvml', 'victor.stinner@gmail.com')">victor.stinner@gmail.com</a>> wrote:<br>
> Hi,<br>
><br>
> I updated my PEP 509 to make the dictionary version globally unique.<br>
> With *two* use cases of this PEP (Yury's method call patch and my FAT<br>
> Python project), I think that the PEP is now ready to be accepted.<br>
><br>
> Globally unique identifier is a requirement for Yury's patch<br>
> optimizing method calls ( <a href="https://bugs.python.org/issue26110" target="_blank">https://bugs.python.org/issue26110</a> ). It<br>
> allows to check for free if the dictionary was replaced.<br>
><br>
> I also renamed the ma_version field to ma_version_tag.<br>
><br>
> HTML version:<br>
> <a href="https://www.python.org/dev/peps/pep-0509/" target="_blank">https://www.python.org/dev/peps/pep-0509/</a><br>
><br>
> Victor<br>
><br>
><br>
> PEP: 509<br>
> Title: Add a private version to dict<br>
> Version: $Revision$<br>
> Last-Modified: $Date$<br>
> Author: Victor Stinner <<a href="javascript:;" onclick="_e(event, 'cvml', 'victor.stinner@gmail.com')">victor.stinner@gmail.com</a>><br>
> Status: Draft<br>
> Type: Standards Track<br>
> Content-Type: text/x-rst<br>
> Created: 4-January-2016<br>
> Python-Version: 3.6<br>
><br>
><br>
> Abstract<br>
> ========<br>
><br>
> Add a new private version to the builtin ``dict`` type, incremented at<br>
> each dictionary creation and at each dictionary change, to implement<br>
> fast guards on namespaces.<br>
><br>
><br>
> Rationale<br>
> =========<br>
><br>
> In Python, the builtin ``dict`` type is used by many instructions. For<br>
> example, the ``LOAD_GLOBAL`` instruction searchs for a variable in the<br>
> global namespace, or in the builtins namespace (two dict lookups).<br>
> Python uses ``dict`` for the builtins namespace, globals namespace, type<br>
> namespaces, instance namespaces, etc. The local namespace (namespace of<br>
> a function) is usually optimized to an array, but it can be a dict too.<br>
><br>
> Python is hard to optimize because almost everything is mutable: builtin<br>
> functions, function code, global variables, local variables, ... can be<br>
> modified at runtime. Implementing optimizations respecting the Python<br>
> semantics requires to detect when "something changes": we will call<br>
> these checks "guards".<br>
><br>
> The speedup of optimizations depends on the speed of guard checks. This<br>
> PEP proposes to add a version to dictionaries to implement fast guards<br>
> on namespaces.<br>
><br>
> Dictionary lookups can be skipped if the version does not change which<br>
> is the common case for most namespaces. Since the version is globally<br>
> unique, the version is also enough to check if the namespace dictionary<br>
> was not replaced with a new dictionary. The performance of a guard does<br>
> not depend on the number of watched dictionary entries, complexity of<br>
> O(1), if the dictionary version does not change.<br>
><br>
> Example of optimization: copy the value of a global variable to function<br>
> constants.  This optimization requires a guard on the global variable to<br>
> check if it was modified. If the variable is modified, the variable must<br>
> be loaded at runtime when the function is called, instead of using the<br>
> constant.<br>
><br>
> See the `PEP 510 -- Specialized functions with guards<br>
> <<a href="https://www.python.org/dev/peps/pep-0510/" target="_blank">https://www.python.org/dev/peps/pep-0510/</a>>`_ for the concrete usage of<br>
> guards to specialize functions and for the rationale on Python static<br>
> optimizers.<br>
><br>
><br>
> Guard example<br>
> =============<br>
><br>
> Pseudo-code of an fast guard to check if a dictionary entry was modified<br>
> (created, updated or deleted) using an hypothetical<br>
> ``dict_get_version(dict)`` function::<br>
><br>
>     UNSET = object()<br>
><br>
>     class GuardDictKey:<br>
>         def __init__(self, dict, key):<br>
>             self.dict = dict<br>
>             self.key = key<br>
>             self.value = dict.get(key, UNSET)<br>
>             self.version = dict_get_version(dict)<br>
><br>
>         def check(self):<br>
>             """Return True if the dictionary entry did not changed<br>
>             and the dictionary was not replaced."""<br>
><br>
>             # read the version of the dict structure<br>
>             version = dict_get_version(self.dict)<br>
>             if version == self.version:<br>
>                 # Fast-path: dictionary lookup avoided<br>
>                 return True<br>
><br>
>             # lookup in the dictionary<br>
>             value = self.dict.get(self.key, UNSET)<br>
>             if value is self.value:<br>
>                 # another key was modified:<br>
>                 # cache the new dictionary version<br>
>                 self.version = version<br>
>                 return True<br>
><br>
>             # the key was modified<br>
>             return False<br>
><br>
><br>
> Usage of the dict version<br>
> =========================<br>
><br>
> Speedup method calls 1.2x<br>
> -------------------------<br>
><br>
> Yury Selivanov wrote a `patch to optimize method calls<br>
> <<a href="https://bugs.python.org/issue26110" target="_blank">https://bugs.python.org/issue26110</a>>`_. The patch depends on the<br>
> `implement per-opcode cache in ceval<br>
> <<a href="https://bugs.python.org/issue26219" target="_blank">https://bugs.python.org/issue26219</a>>`_ patch which requires dictionary<br>
> versions to invalidate the cache if the globals dictionary or the<br>
> builtins dictionary has been modified.<br>
><br>
> The cache also requires that the dictionary version is globally unique.<br>
> It is possible to define a function in a namespace and call it<br>
> in a different namespace: using ``exec()`` with the *globals* parameter<br>
> for example. In this case, the globals dictionary was changed and the<br>
> cache must be invalidated.<br>
><br>
><br>
> Specialized functions using guards<br>
> ----------------------------------<br>
><br>
> The `PEP 510 -- Specialized functions with guards<br>
> <<a href="https://www.python.org/dev/peps/pep-0510/" target="_blank">https://www.python.org/dev/peps/pep-0510/</a>>`_ proposes an API to support<br>
> specialized functions with guards. It allows to implement static<br>
> optimizers for Python without breaking the Python semantics.<br>
><br>
> Example of a static Python optimizer: the `fatoptimizer<br>
> <<a href="http://fatoptimizer.readthedocs.org/" target="_blank">http://fatoptimizer.readthedocs.org/</a>>`_ of the `FAT Python<br>
> <<a href="http://faster-cpython.readthedocs.org/fat_python.html" target="_blank">http://faster-cpython.readthedocs.org/fat_python.html</a>>`_ project<br>
> implements many optimizations which require guards on namespaces.<br>
> Examples:<br>
><br>
> * Call pure builtins: to replace ``len("abc")`` with ``3``, guards on<br>
>   ``builtins.__dict__['len']`` and ``globals()['len']`` are required<br>
> * Loop unrolling: to unroll the loop ``for i in range(...): ...``,<br>
>   guards on ``builtins.__dict__['range']`` and ``globals()['range']``<br>
>   are required<br>
><br>
><br>
> Pyjion<br>
> ------<br>
><br>
> According of Brett Cannon, one of the two main developers of Pyjion,<br>
> Pyjion can also benefit from dictionary version to implement<br>
> optimizations.<br>
><br>
> Pyjion is a JIT compiler for Python based upon CoreCLR (Microsoft .NET<br>
> Core runtime).<br>
><br>
><br>
> Unladen Swallow<br>
> ---------------<br>
><br>
> Even if dictionary version was not explicitly mentioned, optimizing<br>
> globals and builtins lookup was part of the Unladen Swallow plan:<br>
> "Implement one of the several proposed schemes for speeding lookups of<br>
> globals and builtins." Source: `Unladen Swallow ProjectPlan<br>
> <<a href="https://code.google.com/p/unladen-swallow/wiki/ProjectPlan" target="_blank">https://code.google.com/p/unladen-swallow/wiki/ProjectPlan</a>>`_.<br>
><br>
> Unladen Swallow is a fork of CPython 2.6.1 adding a JIT compiler<br>
> implemented with LLVM. The project stopped in 2011: `Unladen Swallow<br>
> Retrospective<br>
> <<a href="http://qinsb.blogspot.com.au/2011/03/unladen-swallow-retrospective.html" target="_blank">http://qinsb.blogspot.com.au/2011/03/unladen-swallow-retrospective.html</a>>`_.<br>
><br>
><br>
> Changes<br>
> =======<br>
><br>
> Add a ``ma_version_tag`` field to the ``PyDictObject`` structure with<br>
> the C type ``PY_INT64_T``, 64-bit unsigned integer. Add also a global<br>
> dictionary version. Each time a dictionary is created, the global<br>
> version is incremented and the dictionary version is initialized to the<br>
> global version. The global version is also incremented and copied to the<br>
> dictionary version at each dictionary change:<br>
><br>
> * ``clear()`` if the dict was non-empty<br>
> * ``pop(key)`` if the key exists<br>
> * ``popitem()`` if the dict is non-empty<br>
> * ``setdefault(key, value)`` if the `key` does not exist<br>
> * ``__detitem__(key)`` if the key exists<br>
> * ``__setitem__(key, value)`` if the `key` doesn't exist or if the value<br>
>   is not ``dict[key]``<br>
> * ``update(...)`` if new values are different than existing values:<br>
>   values are compared by identity, not by their content; the version can<br>
>   be incremented multiple times<br>
><br>
> The ``PyDictObject`` structure is not part of the stable ABI.<br>
><br>
> The field is called ``ma_version_tag`` rather than ``ma_version`` to<br>
> suggest to compare it using ``version_tag == old_version_tag`` rather<br>
> than ``version <= old_version`` which makes the integer overflow much<br>
> likely.<br>
><br>
> Example using an hypothetical ``dict_get_version(dict)`` function::<br>
><br>
>     >>> d = {}<br>
>     >>> dict_get_version(d)<br>
>     100<br>
>     >>> d['key'] = 'value'<br>
>     >>> dict_get_version(d)<br>
>     101<br>
>     >>> d['key'] = 'new value'<br>
>     >>> dict_get_version(d)<br>
>     102<br>
>     >>> del d['key']<br>
>     >>> dict_get_version(d)<br>
>     103<br>
><br>
> The version is not incremented if an existing key is set to the same<br>
> value. For efficiency, values are compared by their identity:<br>
> ``new_value is old_value``, not by their content:<br>
> ``new_value == old_value``. Example::<br>
><br>
>     >>> d = {}<br>
>     >>> value = object()<br>
>     >>> d['key'] = value<br>
>     >>> dict_get_version(d)<br>
>     40<br>
>     >>> d['key'] = value<br>
>     >>> dict_get_version(d)<br>
>     40<br>
><br>
> .. note::<br>
>    CPython uses some singleton like integers in the range [-5; 257],<br>
>    empty tuple, empty strings, Unicode strings of a single character in<br>
>    the range [U+0000; U+00FF], etc. When a key is set twice to the same<br>
>    singleton, the version is not modified.<br>
><br>
><br>
> Implementation and Performance<br>
> ==============================<br>
><br>
> The `issue #26058: PEP 509: Add ma_version_tag to PyDictObject<br>
> <<a href="https://bugs.python.org/issue26058" target="_blank">https://bugs.python.org/issue26058</a>>`_ contains a patch implementing<br>
> this PEP.<br>
><br>
> On pybench and timeit microbenchmarks, the patch does not seem to add<br>
> any overhead on dictionary operations.<br>
><br>
> When the version does not change, ``PyDict_GetItem()`` takes 14.8 ns for<br>
> a dictionary lookup, whereas a guard check only takes 3.8 ns. Moreover,<br>
> a guard can watch for multiple keys. For example, for an optimization<br>
> using 10 global variables in a function, 10 dictionary lookups costs 148<br>
> ns, whereas the guard still only costs 3.8 ns when the version does not<br>
> change (39x as fast).<br>
><br>
> The `fat module<br>
> <<a href="http://fatoptimizer.readthedocs.org/en/latest/fat.html" target="_blank">http://fatoptimizer.readthedocs.org/en/latest/fat.html</a>>`_ implements<br>
> such guards: ``fat.GuardDict`` is based on the dictionary version.<br>
><br>
><br>
> Integer overflow<br>
> ================<br>
><br>
> The implementation uses the C type ``PY_UINT64_T`` to store the version:<br>
> a 64 bits unsigned integer. The C code uses ``version++``. On integer<br>
> overflow, the version is wrapped to ``0`` (and then continue to be<br>
> incremented) according to the C standard.<br>
><br>
> After an integer overflow, a guard can succeed whereas the watched<br>
> dictionary key was modified. The bug only occurs at a guard check if<br>
> there are exaclty ``2 ** 64`` dictionary creations or modifications<br>
> since the previous guard check.<br>
><br>
> If a dictionary is modified every nanosecond, ``2 ** 64`` modifications<br>
> takes longer than 584 years. Using a 32-bit version, it only takes 4<br>
> seconds. That's why a 64-bit unsigned type is also used on 32-bit<br>
> systems. A dictionary lookup at the C level takes 14.8 ns.<br>
><br>
> A risk of a bug every 584 years is acceptable.<br>
><br>
><br>
> Alternatives<br>
> ============<br>
><br>
> Expose the version at Python level as a read-only __version__ property<br>
> ----------------------------------------------------------------------<br>
><br>
> The first version of the PEP proposed to expose the dictionary version<br>
> as a read-only ``__version__`` property at Python level, and also to add<br>
> the property to ``collections.UserDict`` (since this type must mimick<br>
> the ``dict`` API).<br>
><br>
> There are multiple issues:<br>
><br>
> * To be consistent and avoid bad surprises, the version must be added to<br>
>   all mapping types. Implementing a new mapping type would require extra<br>
>   work for no benefit, since the version is only required on the<br>
>   ``dict`` type in practice.<br>
> * All Python implementations must implement this new property, it gives<br>
>   more work to other implementations, whereas they may not use the<br>
>   dictionary version at all.<br>
> * Exposing the dictionary version at Python level can lead the<br>
>   false assumption on performances. Checking ``dict.__version__`` at<br>
>   the Python level is not faster than a dictionary lookup. A dictionary<br>
>   lookup has a cost of 48.7 ns and checking a guard has a cost of 47.5<br>
>   ns, the difference is only 1.2 ns (3%)::<br>
><br>
><br>
>     $ ./python -m timeit -s 'd = {str(i):i for i in range(100)}' 'd["33"] == 33'<br>
>     10000000 loops, best of 3: 0.0487 usec per loop<br>
>     $ ./python -m timeit -s 'd = {str(i):i for i in range(100)}'<br>
> 'd.__version__ == 100'<br>
>     10000000 loops, best of 3: 0.0475 usec per loop<br>
><br>
> * The ``__version__`` can be wrapped on integer overflow. It is error<br>
>   prone: using ``dict.__version__ <= guard_version`` is wrong,<br>
>   ``dict.__version__ == guard_version`` must be used instead to reduce<br>
>   the risk of bug on integer overflow (even if the integer overflow is<br>
>   unlikely in practice).<br>
><br>
> Mandatory bikeshedding on the property name:<br>
><br>
> * ``__cache_token__``: name proposed by Nick Coghlan, name coming from<br>
>   `abc.get_cache_token()<br>
>   <<a href="https://docs.python.org/3/library/abc.html#abc.get_cache_token" target="_blank">https://docs.python.org/3/library/abc.html#abc.get_cache_token</a>>`_.<br>
> * ``__version__``<br>
> * ``__timestamp__``<br>
><br>
><br>
> Add a version to each dict entry<br>
> --------------------------------<br>
><br>
> A single version per dictionary requires to keep a strong reference to<br>
> the value which can keep the value alive longer than expected. If we add<br>
> also a version per dictionary entry, the guard can only store the entry<br>
> version to avoid the strong reference to the value (only strong<br>
> references to the dictionary and to the key are needed).<br>
><br>
> Changes: add a ``me_version`` field to the ``PyDictKeyEntry`` structure,<br>
> the field has the C type ``PY_INT64_T``. When a key is created or<br>
> modified, the entry version is set to the dictionary version which is<br>
> incremented at any change (create, modify, delete).<br>
><br>
> Pseudo-code of an fast guard to check if a dictionary key was modified<br>
> using hypothetical ``dict_get_version(dict)``<br>
> ``dict_get_entry_version(dict)`` functions::<br>
><br>
>     UNSET = object()<br>
><br>
>     class GuardDictKey:<br>
>         def __init__(self, dict, key):<br>
>             self.dict = dict<br>
>             self.key = key<br>
>             self.dict_version = dict_get_version(dict)<br>
>             self.entry_version = dict_get_entry_version(dict, key)<br>
><br>
>         def check(self):<br>
>             """Return True if the dictionary entry did not changed<br>
>             and the dictionary was not replaced."""<br>
><br>
>             # read the version of the dict structure<br>
>             dict_version = dict_get_version(self.dict)<br>
>             if dict_version == self.version:<br>
>                 # Fast-path: dictionary lookup avoided<br>
>                 return True<br>
><br>
>             # lookup in the dictionary<br>
>             entry_version = get_dict_key_version(dict, key)<br>
>             if entry_version == self.entry_version:<br>
>                 # another key was modified:<br>
>                 # cache the new dictionary version<br>
>                 self.dict_version = dict_version<br>
>                 return True<br>
><br>
>             # the key was modified<br>
>             return False<br>
><br>
> The main drawback of this option is the impact on the memory footprint.<br>
> It increases the size of each dictionary entry, so the overhead depends<br>
> on the number of buckets (dictionary entries, used or unused yet). For<br>
> example, it increases the size of each dictionary entry by 8 bytes on<br>
> 64-bit system.<br>
><br>
> In Python, the memory footprint matters and the trend is to reduce it.<br>
> Examples:<br>
><br>
> * `PEP 393 -- Flexible String Representation<br>
>   <<a href="https://www.python.org/dev/peps/pep-0393/" target="_blank">https://www.python.org/dev/peps/pep-0393/</a>>`_<br>
> * `PEP 412 -- Key-Sharing Dictionary<br>
>   <<a href="https://www.python.org/dev/peps/pep-0412/" target="_blank">https://www.python.org/dev/peps/pep-0412/</a>>`_<br>
><br>
><br>
> Add a new dict subtype<br>
> ----------------------<br>
><br>
> Add a new ``verdict`` type, subtype of ``dict``. When guards are needed,<br>
> use the ``verdict`` for namespaces (module namespace, type namespace,<br>
> instance namespace, etc.) instead of ``dict``.<br>
><br>
> Leave the ``dict`` type unchanged to not add any overhead (memory<br>
> footprint) when guards are not needed.<br>
><br>
> Technical issue: a lot of C code in the wild, including CPython core,<br>
> expecting the exact ``dict`` type. Issues:<br>
><br>
> * ``exec()`` requires a ``dict`` for globals and locals. A lot of code<br>
>   use ``globals={}``. It is not possible to cast the ``dict`` to a<br>
>   ``dict`` subtype because the caller expects the ``globals`` parameter<br>
>   to be modified (``dict`` is mutable).<br>
> * Functions call directly ``PyDict_xxx()`` functions, instead of calling<br>
>   ``PyObject_xxx()`` if the object is a ``dict`` subtype<br>
> * ``PyDict_CheckExact()`` check fails on ``dict`` subtype, whereas some<br>
>   functions require the exact ``dict`` type.<br>
> * ``Python/ceval.c`` does not completely supports dict subtypes for<br>
>   namespaces<br>
><br>
><br>
> The ``exec()`` issue is a blocker issue.<br>
><br>
> Other issues:<br>
><br>
> * The garbage collector has a special code to "untrack" ``dict``<br>
>   instances. If a ``dict`` subtype is used for namespaces, the garbage<br>
>   collector can be unable to break some reference cycles.<br>
> * Some functions have a fast-path for ``dict`` which would not be taken<br>
>   for ``dict`` subtypes, and so it would make Python a little bit<br>
>   slower.<br>
><br>
><br>
> Prior Art<br>
> =========<br>
><br>
> Method cache and type version tag<br>
> ---------------------------------<br>
><br>
> In 2007, Armin Rigo wrote a patch to to implement a cache of methods. It<br>
> was merged into Python 2.6.  The patch adds a "type attribute cache<br>
> version tag" (``tp_version_tag``) and a "valid version tag" flag to<br>
> types (the ``PyTypeObject`` structure).<br>
><br>
> The type version tag is not available at the Python level.<br>
><br>
> The version tag has the C type ``unsigned int``. The cache is a global<br>
> hash table of 4096 entries, shared by all types. The cache is global to<br>
> "make it fast, have a deterministic and low memory footprint, and be<br>
> easy to invalidate". Each cache entry has a version tag. A global<br>
> version tag is used to create the next version tag, it also has the C<br>
> type ``unsigned int``.<br>
><br>
> By default, a type has its "valid version tag" flag cleared to indicate<br>
> that the version tag is invalid. When the first method of the type is<br>
> cached, the version tag and the "valid version tag" flag are set. When a<br>
> type is modified, the "valid version tag" flag of the type and its<br>
> subclasses is cleared. Later, when a cache entry of these types is used,<br>
> the entry is removed because its version tag is outdated.<br>
><br>
> On integer overflow, the whole cache is cleared and the global version<br>
> tag is reset to ``0``.<br>
><br>
> See `Method cache (issue #1685986)<br>
> <<a href="https://bugs.python.org/issue1685986" target="_blank">https://bugs.python.org/issue1685986</a>>`_ and `Armin's method cache<br>
> optimization updated for Python 2.6 (issue #1700288)<br>
> <<a href="https://bugs.python.org/issue1700288" target="_blank">https://bugs.python.org/issue1700288</a>>`_.<br>
><br>
><br>
> Globals / builtins cache<br>
> ------------------------<br>
><br>
> In 2010, Antoine Pitrou proposed a `Globals / builtins cache (issue<br>
> #10401) <<a href="http://bugs.python.org/issue10401" target="_blank">http://bugs.python.org/issue10401</a>>`_ which adds a private<br>
> ``ma_version`` field to the ``PyDictObject`` structure (``dict`` type),<br>
> the field has the C type ``Py_ssize_t``.<br>
><br>
> The patch adds a "global and builtin cache" to functions and frames, and<br>
> changes ``LOAD_GLOBAL`` and ``STORE_GLOBAL`` instructions to use the<br>
> cache.<br>
><br>
> The change on the ``PyDictObject`` structure is very similar to this<br>
> PEP.<br>
><br>
><br>
> Cached globals+builtins lookup<br>
> ------------------------------<br>
><br>
> In 2006, Andrea Griffini proposed a patch implementing a `Cached<br>
> globals+builtins lookup optimization<br>
> <<a href="https://bugs.python.org/issue1616125" target="_blank">https://bugs.python.org/issue1616125</a>>`_.  The patch adds a private<br>
> ``timestamp`` field to the ``PyDictObject`` structure (``dict`` type),<br>
> the field has the C type ``size_t``.<br>
><br>
> Thread on python-dev: `About dictionary lookup caching<br>
> <<a href="https://mail.python.org/pipermail/python-dev/2006-December/070348.html" target="_blank">https://mail.python.org/pipermail/python-dev/2006-December/070348.html</a>>`_.<br>
><br>
><br>
> Guard against changing dict during iteration<br>
> --------------------------------------------<br>
><br>
> In 2013, Serhiy Storchaka proposed `Guard against changing dict during<br>
> iteration (issue #19332) <<a href="https://bugs.python.org/issue19332" target="_blank">https://bugs.python.org/issue19332</a>>`_ which<br>
> adds a ``ma_count`` field to the ``PyDictObject`` structure (``dict``<br>
> type), the field has the C type ``size_t``.  This field is incremented<br>
> when the dictionary is modified, and so is very similar to the proposed<br>
> dictionary version.<br>
><br>
> Sadly, the dictionary version proposed in this PEP doesn't help to<br>
> detect dictionary mutation. The dictionary version changes when values<br>
> are replaced, whereas modifying dictionary values while iterating on<br>
> dictionary keys is legit in Python.<br>
><br>
><br>
> PySizer<br>
> -------<br>
><br>
> `PySizer <<a href="http://pysizer.8325.org/" target="_blank">http://pysizer.8325.org/</a>>`_: a memory profiler for Python,<br>
> Google Summer of Code 2005 project by Nick Smallbone.<br>
><br>
> This project has a patch for CPython 2.4 which adds ``key_time`` and<br>
> ``value_time`` fields to dictionary entries. It uses a global<br>
> process-wide counter for dictionaries, incremented each time that a<br>
> dictionary is modified. The times are used to decide when child objects<br>
> first appeared in their parent objects.<br>
><br>
><br>
> Discussion<br>
> ==========<br>
><br>
> Thread on the mailing lists:<br>
><br>
> * python-dev: `PEP 509: Add a private version to dict<br>
>   <<a href="https://mail.python.org/pipermail/python-dev/2016-January/142685.html" target="_blank">https://mail.python.org/pipermail/python-dev/2016-January/142685.html</a>>`_<br>
>   (january 2016)<br>
> * python-ideas: `RFC: PEP: Add dict.__version__<br>
>   <<a href="https://mail.python.org/pipermail/python-ideas/2016-January/037702.html" target="_blank">https://mail.python.org/pipermail/python-ideas/2016-January/037702.html</a>>`_<br>
>   (january 2016)<br>
><br>
><br>
> Copyright<br>
> =========<br>
><br>
> This document has been placed in the public domain.<br>
> _______________________________________________<br>
> Python-Dev mailing list<br>
> <a href="javascript:;" onclick="_e(event, 'cvml', 'Python-Dev@python.org')">Python-Dev@python.org</a><br>
> <a href="https://mail.python.org/mailman/listinfo/python-dev" target="_blank">https://mail.python.org/mailman/listinfo/python-dev</a><br>
> Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/guido%40python.org" target="_blank">https://mail.python.org/mailman/options/python-dev/guido%40python.org</a><br>
<br>
<br>
<br>
--<br>
--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)<br>
</blockquote></div></div>