comparison mercurial/parsers.c @ 25583:ce64c9ab19f2

parsers: factor out code to create a presized dict In upcoming patches we'll expose this as an API.
author Siddharth Agarwal <sid0@fb.com>
date Mon, 15 Jun 2015 22:37:33 -0700
parents 5fa399a0c385
children 72b2711f12ea
comparison
equal deleted inserted replaced
25582:5fa399a0c385 25583:ce64c9ab19f2
171 if (!PyArg_ParseTuple(args, "O!:asciiupper", &PyBytes_Type, &str_obj)) 171 if (!PyArg_ParseTuple(args, "O!:asciiupper", &PyBytes_Type, &str_obj))
172 return NULL; 172 return NULL;
173 return _asciitransform(str_obj, uppertable, NULL); 173 return _asciitransform(str_obj, uppertable, NULL);
174 } 174 }
175 175
176 static inline PyObject *_dict_new_presized(Py_ssize_t expected_size)
177 {
178 /* _PyDict_NewPresized expects a minused parameter, but it actually
179 creates a dictionary that's the nearest power of two bigger than the
180 parameter. For example, with the initial minused = 1000, the
181 dictionary created has size 1024. Of course in a lot of cases that
182 can be greater than the maximum load factor Python's dict object
183 expects (= 2/3), so as soon as we cross the threshold we'll resize
184 anyway. So create a dictionary that's at least 3/2 the size. */
185 return _PyDict_NewPresized(((1 + expected_size) / 2) * 3);
186 }
187
176 static PyObject *make_file_foldmap(PyObject *self, PyObject *args) 188 static PyObject *make_file_foldmap(PyObject *self, PyObject *args)
177 { 189 {
178 PyObject *dmap, *spec_obj, *normcase_fallback; 190 PyObject *dmap, *spec_obj, *normcase_fallback;
179 PyObject *file_foldmap = NULL; 191 PyObject *file_foldmap = NULL;
180 enum normcase_spec spec; 192 enum normcase_spec spec;
203 default: 215 default:
204 PyErr_SetString(PyExc_TypeError, "invalid normcasespec"); 216 PyErr_SetString(PyExc_TypeError, "invalid normcasespec");
205 goto quit; 217 goto quit;
206 } 218 }
207 219
208 /* _PyDict_NewPresized expects a minused parameter, but it actually 220 /* Add some more entries to deal with additions outside this
209 creates a dictionary that's the nearest power of two bigger than the 221 function. */
210 parameter. For example, with the initial minused = 1000, the 222 file_foldmap = _dict_new_presized((PyDict_Size(dmap) / 10) * 11);
211 dictionary created has size 1024. Of course in a lot of cases that
212 can be greater than the maximum load factor Python's dict object
213 expects (= 2/3), so as soon as we cross the threshold we'll resize
214 anyway. So create a dictionary that's 3/2 the size. Also add some
215 more to deal with additions outside this function. */
216 file_foldmap = _PyDict_NewPresized((PyDict_Size(dmap) / 5) * 8);
217 if (file_foldmap == NULL) 223 if (file_foldmap == NULL)
218 goto quit; 224 goto quit;
219 225
220 while (PyDict_Next(dmap, &pos, &k, &v)) { 226 while (PyDict_Next(dmap, &pos, &k, &v)) {
221 if (!dirstate_tuple_check(v)) { 227 if (!dirstate_tuple_check(v)) {