Skip to content

Commit f11db7f

Browse files
committed
cext: remove _dict_new_presized util
As mentionned in #10029, since Python 3.6 `_PyDict_NewPresized()` allocates enough space and overallocating by `1 + x / 2*3` is not needed anymore¹. Using the private `_PyDict_NewPresized` may not be as relevant² as it was when its use was introduced a long time ago, but finding out is for another time. [1] https://github.com/python/cpython/blob/8d999cbf4adea053be6dbb612b9844635c4dfb8e/Objects/dictobject.c#L1378 [2] python/cpython#139772 (comment) --HG-- extra : topic : dict-presizing
1 parent e183aeb commit f11db7f

4 files changed

Lines changed: 3 additions & 15 deletions

File tree

‎mercurial/cext/charencode.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ PyObject *make_file_foldmap(PyObject *self, PyObject *args)
242242

243243
/* Add some more entries to deal with additions outside this
244244
function. */
245-
file_foldmap = _dict_new_presized((PyDict_Size(dmap) / 10) * 11);
245+
file_foldmap = _PyDict_NewPresized((PyDict_Size(dmap) / 10) * 11);
246246
if (file_foldmap == NULL) {
247247
goto quit;
248248
}

‎mercurial/cext/parsers.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static PyObject *dict_new_presized(PyObject *self, PyObject *args)
3131
return NULL;
3232
}
3333

34-
return _dict_new_presized(expected_size);
34+
return _PyDict_NewPresized(expected_size);
3535
}
3636

3737
static PyObject *dirstate_item_new(PyTypeObject *subtype, PyObject *args,

‎mercurial/cext/revlog.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,7 @@ static PyObject *compute_phases_map_sets(indexObject *self, PyObject *args)
12411241
Py_DECREF(pyrev);
12421242
}
12431243

1244-
phasesetsdict = _dict_new_presized(numphases);
1244+
phasesetsdict = _PyDict_NewPresized(numphases);
12451245
if (phasesetsdict == NULL)
12461246
goto release;
12471247
for (i = 0; i < numphases; ++i) {

‎mercurial/cext/util.h‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,6 @@ typedef unsigned char bool;
5353
#include <stdbool.h>
5454
#endif
5555

56-
static inline PyObject *_dict_new_presized(Py_ssize_t expected_size)
57-
{
58-
/* _PyDict_NewPresized expects a minused parameter, but it actually
59-
creates a dictionary that's the nearest power of two bigger than the
60-
parameter. For example, with the initial minused = 1000, the
61-
dictionary created has size 1024. Of course in a lot of cases that
62-
can be greater than the maximum load factor Python's dict object
63-
expects (= 2/3), so as soon as we cross the threshold we'll resize
64-
anyway. So create a dictionary that's at least 3/2 the size. */
65-
return _PyDict_NewPresized(((1 + expected_size) / 2) * 3);
66-
}
67-
6856
/* Convert a PyInt or PyLong to a long. Returns false if there is an
6957
error, in which case an exception will already have been set. */
7058
static inline bool pylong_to_long(PyObject *pylong, long *out)

0 commit comments

Comments
 (0)