Author: Laurence Tratt <[email protected]>
Branch: 
Changeset: r74945:22ee57670e0d
Date: 2014-12-15 16:37 +0000
http://bitbucket.org/pypy/pypy/changeset/22ee57670e0d/

Log:    Compact dictionaries that have very few live items left in them.

        For some reason, we copied CPython's hard-to-defend behaviour here.
        Replace it with a (very) conservative check that will only shrink
        the memory allocated for a dictionary if it has very few items left.

diff --git a/rpython/rtyper/lltypesystem/rordereddict.py 
b/rpython/rtyper/lltypesystem/rordereddict.py
--- a/rpython/rtyper/lltypesystem/rordereddict.py
+++ b/rpython/rtyper/lltypesystem/rordereddict.py
@@ -661,19 +661,11 @@
         entry.key = lltype.nullptr(ENTRY.key.TO)
     if ENTRIES.must_clear_value:
         entry.value = lltype.nullptr(ENTRY.value.TO)
-    #
-    # The rest is commented out: like CPython we no longer shrink the
-    # dictionary here.  It may shrink later if we try to append a number
-    # of new items to it.  Unsure if this behavior was designed in
-    # CPython or is accidental.  A design reason would be that if you
-    # delete all items in a dictionary (e.g. with a series of
-    # popitem()), then CPython avoids shrinking the table several times.
-    #num_entries = len(d.entries)
-    #if num_entries > DICT_INITSIZE and d.num_items <= num_entries / 4:
-    #    ll_dict_resize(d)
-    # A previous xxx: move the size checking and resize into a single
-    # call which is opaque to the JIT when the dict isn't virtual, to
-    # avoid extra branches.
+
+    # If the dictionary is at least 87.5% dead items, then consider shrinking
+    # it.
+    if d.num_live_items + DICT_INITSIZE <= len(d.entries) / 8:
+        ll_dict_resize(d)
 
 def ll_dict_resize(d):
     # make a 'new_size' estimate and shrink it if there are many
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to