Skip to content

Commit ec05341

Browse files
committed
remove int cache
1 parent 595917d commit ec05341

File tree

1 file changed

+80
-70
lines changed

1 file changed

+80
-70
lines changed

src/auto_map.c

Lines changed: 80 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -399,48 +399,48 @@ string_to_hash(char *str, Py_ssize_t len) {
399399
//------------------------------------------------------------------------------
400400
// the global int_cache is shared among all instances
401401

402-
static PyObject *int_cache = NULL;
403-
404-
// NOTE: this used to be a Py_ssize_t, which can be 32 bits on some machines and might easily overflow with a few very large indices. Using an explicit 64-bit int seems safer
405-
static npy_int64 key_count_global = 0;
406-
407-
// Fill the int_cache up to size_needed with PyObject ints; `size` is not the key_count_global.
408-
static int
409-
int_cache_fill(Py_ssize_t size_needed)
410-
{
411-
PyObject *item;
412-
if (!int_cache) {
413-
int_cache = PyList_New(0);
414-
if (!int_cache) {
415-
return -1;
416-
}
417-
}
418-
for (Py_ssize_t i = PyList_GET_SIZE(int_cache); i < size_needed; i++) {
419-
item = PyLong_FromSsize_t(i);
420-
if (!item) {
421-
return -1;
422-
}
423-
if (PyList_Append(int_cache, item)) {
424-
Py_DECREF(item);
425-
return -1;
426-
}
427-
Py_DECREF(item);
428-
}
429-
return 0;
430-
}
431-
432-
// Given the current key_count_global, remove cache elements only if the key_count is less than the the current size of the int_cache.
433-
void
434-
int_cache_remove(Py_ssize_t key_count)
435-
{
436-
if (!key_count) {
437-
Py_CLEAR(int_cache);
438-
}
439-
else if (key_count < PyList_GET_SIZE(int_cache)) {
440-
// del int_cache[key_count:]
441-
PyList_SetSlice(int_cache, key_count, PyList_GET_SIZE(int_cache), NULL);
442-
}
443-
}
402+
// static PyObject *int_cache = NULL;
403+
404+
// // NOTE: this used to be a Py_ssize_t, which can be 32 bits on some machines and might easily overflow with a few very large indices. Using an explicit 64-bit int seems safer
405+
// static npy_int64 key_count_global = 0;
406+
407+
// // Fill the int_cache up to size_needed with PyObject ints; `size` is not the key_count_global.
408+
// static int
409+
// int_cache_fill(Py_ssize_t size_needed)
410+
// {
411+
// PyObject *item;
412+
// if (!int_cache) {
413+
// int_cache = PyList_New(0);
414+
// if (!int_cache) {
415+
// return -1;
416+
// }
417+
// }
418+
// for (Py_ssize_t i = PyList_GET_SIZE(int_cache); i < size_needed; i++) {
419+
// item = PyLong_FromSsize_t(i);
420+
// if (!item) {
421+
// return -1;
422+
// }
423+
// if (PyList_Append(int_cache, item)) {
424+
// Py_DECREF(item);
425+
// return -1;
426+
// }
427+
// Py_DECREF(item);
428+
// }
429+
// return 0;
430+
// }
431+
432+
// // Given the current key_count_global, remove cache elements only if the key_count is less than the the current size of the int_cache.
433+
// void
434+
// int_cache_remove(Py_ssize_t key_count)
435+
// {
436+
// if (!key_count) {
437+
// Py_CLEAR(int_cache);
438+
// }
439+
// else if (key_count < PyList_GET_SIZE(int_cache)) {
440+
// // del int_cache[key_count:]
441+
// PyList_SetSlice(int_cache, key_count, PyList_GET_SIZE(int_cache), NULL);
442+
// }
443+
// }
444444

445445
//------------------------------------------------------------------------------
446446
// FrozenAutoMapIterator functions
@@ -488,18 +488,20 @@ fami_iternext(FAMIObject *self)
488488
switch (self->kind) {
489489
case ITEMS: {
490490
if (self->fam->keys_array_type) {
491-
return PyTuple_Pack(
492-
2,
491+
return Py_BuildValue(
492+
"NN",
493493
PyArray_ToScalar(PyArray_GETPTR1(self->keys_array, index), self->keys_array),
494-
PyList_GET_ITEM(int_cache, index)
494+
PyLong_FromSsize_t(index)
495495
);
496496
}
497497
else {
498-
return PyTuple_Pack(
499-
2,
500-
PyList_GET_ITEM(self->fam->keys, index),
501-
PyList_GET_ITEM(int_cache, index)
502-
);
498+
PyObject* t = PyTuple_New(2);
499+
if (!t) { return NULL; }
500+
PyObject* k = PyList_GET_ITEM(self->fam->keys, index);
501+
Py_INCREF(k);
502+
PyTuple_SET_ITEM(t, 0, k);
503+
PyTuple_SET_ITEM(t, 1, PyLong_FromSsize_t(index));
504+
return t;
503505
}
504506
}
505507
case KEYS: {
@@ -513,9 +515,7 @@ fami_iternext(FAMIObject *self)
513515
}
514516
}
515517
case VALUES: {
516-
PyObject *yield = PyList_GET_ITEM(int_cache, index);
517-
Py_INCREF(yield);
518-
return yield;
518+
return PyLong_FromSsize_t(index);
519519
}
520520
}
521521
Py_UNREACHABLE();
@@ -1557,9 +1557,9 @@ static int
15571557
grow_table(FAMObject *self, Py_ssize_t keys_size)
15581558
{
15591559
// NOTE: this is the only place int_cache_fill is called; it is not called with key_count_global, but with the max value needed
1560-
if (int_cache_fill(keys_size)) {
1561-
return -1;
1562-
}
1560+
// if (int_cache_fill(keys_size)) {
1561+
// return -1;
1562+
// }
15631563
Py_ssize_t keys_load = keys_size / LOAD;
15641564
Py_ssize_t size_old = self->table_size;
15651565
if (keys_load < size_old) {
@@ -1628,7 +1628,7 @@ copy_to_new(PyTypeObject *cls, FAMObject *self, FAMObject *new)
16281628
return -1;
16291629
}
16301630
}
1631-
key_count_global += self->keys_size;
1631+
// key_count_global += self->keys_size;
16321632

16331633
new->table_size = self->table_size;
16341634
new->keys_array_type = self->keys_array_type;
@@ -1691,7 +1691,7 @@ extend(FAMObject *self, PyObject *keys)
16911691
return -1;
16921692
}
16931693
Py_ssize_t size_extend = PySequence_Fast_GET_SIZE(keys);
1694-
key_count_global += size_extend;
1694+
// key_count_global += size_extend;
16951695
self->keys_size += size_extend;
16961696

16971697
if (grow_table(self, self->keys_size)) {
@@ -1723,7 +1723,7 @@ append(FAMObject *self, PyObject *key)
17231723
PyErr_SetString(PyExc_NotImplementedError, "Not supported for array keys");
17241724
return -1;
17251725
}
1726-
key_count_global++;
1726+
// key_count_global++;
17271727
self->keys_size++;
17281728

17291729
if (grow_table(self, self->keys_size)) {
@@ -1761,10 +1761,10 @@ get(FAMObject *self, PyObject *key, PyObject *missing) {
17611761
PyErr_SetObject(PyExc_KeyError, key);
17621762
return NULL;
17631763
}
1764-
// use a C-integer to fetch the Python integer
1765-
PyObject *index = PyList_GET_ITEM(int_cache, keys_pos);
1766-
Py_INCREF(index);
1767-
return index;
1764+
return PyLong_FromSsize_t(index);
1765+
// PyObject *index = PyList_GET_ITEM(int_cache, keys_pos);
1766+
// Py_INCREF(index);
1767+
// return index;
17681768
}
17691769

17701770

@@ -1996,10 +1996,13 @@ fam_get_all(FAMObject *self, PyObject *key) {
19961996
continue; \
19971997
} \
19981998
keys_pos = self->table[table_pos].keys_pos; \
1999-
if (PyList_Append(values, PyList_GET_ITEM(int_cache, keys_pos))) { \
1999+
PyObject* p = PyLong_FromSsize_t(keys_pos); \
2000+
if (PyList_Append(values, p)) { \
2001+
Py_DECREF(p); \
20002002
Py_DECREF(values); \
20012003
return NULL; \
20022004
} \
2005+
Py_DECREF(p); \
20032006
} \
20042007
} \
20052008

@@ -2020,10 +2023,13 @@ fam_get_all(FAMObject *self, PyObject *key) {
20202023
continue; \
20212024
} \
20222025
keys_pos = self->table[table_pos].keys_pos; \
2023-
if (PyList_Append(values, PyList_GET_ITEM(int_cache, keys_pos))) { \
2026+
PyObject* p = PyLong_FromSsize_t(keys_pos); \
2027+
if (PyList_Append(values, p)) { \
2028+
Py_DECREF(p); \
20242029
Py_DECREF(values); \
20252030
return NULL; \
20262031
} \
2032+
Py_DECREF(p); \
20272033
} \
20282034
} \
20292035

@@ -2066,10 +2072,12 @@ fam_get_any(FAMObject *self, PyObject *key) {
20662072
}
20672073
continue;
20682074
}
2069-
if (PyList_Append(values, PyList_GET_ITEM(int_cache, keys_pos))) {
2075+
PyObject* p = PyLong_FromSsize_t(keys_pos);
2076+
if (PyList_Append(values, p)) {
20702077
Py_DECREF(values);
20712078
return NULL;
20722079
}
2080+
Py_DECREF(p);
20732081
}
20742082
}
20752083
else {
@@ -2145,10 +2153,12 @@ fam_get_any(FAMObject *self, PyObject *key) {
21452153
}
21462154
continue; // do not raise
21472155
}
2148-
if (PyList_Append(values, PyList_GET_ITEM(int_cache, keys_pos))) {
2156+
PyObject* p = PyLong_FromSsize_t(keys_pos);
2157+
if (PyList_Append(values, p)) {
21492158
Py_DECREF(values);
21502159
return NULL;
21512160
}
2161+
Py_DECREF(p);
21522162
}
21532163
}
21542164
}
@@ -2229,10 +2239,10 @@ fam_dealloc(FAMObject *self)
22292239
Py_DECREF(self->keys);
22302240
}
22312241

2232-
key_count_global -= self->keys_size;
2242+
// key_count_global -= self->keys_size;
22332243

22342244
Py_TYPE(self)->tp_free((PyObject *)self);
2235-
int_cache_remove(key_count_global);
2245+
// int_cache_remove(key_count_global);
22362246
}
22372247

22382248

@@ -2480,7 +2490,7 @@ fam_init(PyObject *self, PyObject *args, PyObject *kwargs)
24802490
fam->keys_array_type = keys_array_type;
24812491
fam->keys_size = keys_size;
24822492
fam->key_buffer = NULL;
2483-
key_count_global += keys_size;
2493+
// key_count_global += keys_size;
24842494

24852495
// NOTE: on itialization, grow_table() does not use keys
24862496
if (grow_table(fam, keys_size)) {

0 commit comments

Comments
 (0)