Commit f2a18671 authored by Inada Naoki's avatar Inada Naoki Committed by GitHub

bpo-30040: new empty dict uses key-sharing dict (GH-1080)

Sizeof new empty dict becomes 72 bytes from 240 bytes (amd64).
It is same size to empty dict created by dict.clear().
parent fc06a192
...@@ -5346,16 +5346,16 @@ class SharedKeyTests(unittest.TestCase): ...@@ -5346,16 +5346,16 @@ class SharedKeyTests(unittest.TestCase):
a, b = A(), B() a, b = A(), B()
self.assertEqual(sys.getsizeof(vars(a)), sys.getsizeof(vars(b))) self.assertEqual(sys.getsizeof(vars(a)), sys.getsizeof(vars(b)))
self.assertLess(sys.getsizeof(vars(a)), sys.getsizeof({})) self.assertLess(sys.getsizeof(vars(a)), sys.getsizeof({"a":1}))
# Initial hash table can contain at most 5 elements. # Initial hash table can contain at most 5 elements.
# Set 6 attributes to cause internal resizing. # Set 6 attributes to cause internal resizing.
a.x, a.y, a.z, a.w, a.v, a.u = range(6) a.x, a.y, a.z, a.w, a.v, a.u = range(6)
self.assertNotEqual(sys.getsizeof(vars(a)), sys.getsizeof(vars(b))) self.assertNotEqual(sys.getsizeof(vars(a)), sys.getsizeof(vars(b)))
a2 = A() a2 = A()
self.assertEqual(sys.getsizeof(vars(a)), sys.getsizeof(vars(a2))) self.assertEqual(sys.getsizeof(vars(a)), sys.getsizeof(vars(a2)))
self.assertLess(sys.getsizeof(vars(a)), sys.getsizeof({})) self.assertLess(sys.getsizeof(vars(a)), sys.getsizeof({"a":1}))
b.u, b.v, b.w, b.t, b.s, b.r = range(6) b.u, b.v, b.w, b.t, b.s, b.r = range(6)
self.assertLess(sys.getsizeof(vars(b)), sys.getsizeof({})) self.assertLess(sys.getsizeof(vars(b)), sys.getsizeof({"a":1}))
class DebugHelperMeta(type): class DebugHelperMeta(type):
......
...@@ -982,8 +982,10 @@ class SizeofTest(unittest.TestCase): ...@@ -982,8 +982,10 @@ class SizeofTest(unittest.TestCase):
check(int.__add__, size('3P2P')) check(int.__add__, size('3P2P'))
# method-wrapper (descriptor object) # method-wrapper (descriptor object)
check({}.__iter__, size('2P')) check({}.__iter__, size('2P'))
# empty dict
check({}, size('nQ2P'))
# dict # dict
check({}, size('nQ2P') + calcsize('2nP2n') + 8 + (8*2//3)*calcsize('n2P')) check({"a": 1}, size('nQ2P') + calcsize('2nP2n') + 8 + (8*2//3)*calcsize('n2P'))
longdict = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8} longdict = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8}
check(longdict, size('nQ2P') + calcsize('2nP2n') + 16 + (16*2//3)*calcsize('n2P')) check(longdict, size('nQ2P') + calcsize('2nP2n') + 16 + (16*2//3)*calcsize('n2P'))
# dictionary-keyview # dictionary-keyview
......
New empty dict uses fewer memory for now. It used more memory than empty
dict created by ``dict.clear()``. Patch by Inada Naoki.
...@@ -691,10 +691,8 @@ clone_combined_dict(PyDictObject *orig) ...@@ -691,10 +691,8 @@ clone_combined_dict(PyDictObject *orig)
PyObject * PyObject *
PyDict_New(void) PyDict_New(void)
{ {
PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE); dictkeys_incref(Py_EMPTY_KEYS);
if (keys == NULL) return new_dict(Py_EMPTY_KEYS, empty_values);
return NULL;
return new_dict(keys, NULL);
} }
/* Search index of hash table from offset of entry table */ /* Search index of hash table from offset of entry table */
...@@ -1276,6 +1274,9 @@ _PyDict_NewPresized(Py_ssize_t minused) ...@@ -1276,6 +1274,9 @@ _PyDict_NewPresized(Py_ssize_t minused)
Py_ssize_t newsize; Py_ssize_t newsize;
PyDictKeysObject *new_keys; PyDictKeysObject *new_keys;
if (minused == 0) {
return PyDict_New();
}
/* There are no strict guarantee that returned dict can contain minused /* There are no strict guarantee that returned dict can contain minused
* items without resize. So we create medium size dict instead of very * items without resize. So we create medium size dict instead of very
* large dict or MemoryError. * large dict or MemoryError.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment