[Python-checkins] cpython (3.6): Issue #28509: dict.update() no longer allocate unnecessary large memory

inada.naoki python-checkins at python.org
Thu Oct 27 06:30:18 EDT 2016


https://hg.python.org/cpython/rev/8c2615decd2e
changeset:   104741:8c2615decd2e
branch:      3.6
parent:      104738:4ddb89661a7f
user:        INADA Naoki <songofacandy at gmail.com>
date:        Thu Oct 27 19:26:50 2016 +0900
summary:
  Issue #28509: dict.update() no longer allocate unnecessary large memory

files:
  Misc/NEWS            |  2 ++
  Objects/dictobject.c |  6 ++++--
  2 files changed, 6 insertions(+), 2 deletions(-)


diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@
 Core and Builtins
 -----------------
 
+- Issue #28509: dict.update() no longer allocate unnecessary large memory.
+
 - Issue #28426: Fixed potential crash in PyUnicode_AsDecodedObject() in debug
   build.
 
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -2406,9 +2406,11 @@
          * incrementally resizing as we insert new items.  Expect
          * that there will be no (or few) overlapping keys.
          */
-        if (mp->ma_keys->dk_usable * 3 < other->ma_used * 2)
-            if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
+        if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
+            if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) {
                return -1;
+            }
+        }
         ep0 = DK_ENTRIES(other->ma_keys);
         for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
             PyObject *key, *value;

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list