Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Let struct module's internal cache use FIFO policy. Patch by Ma Lin.
16 changes: 12 additions & 4 deletions Modules/_struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -2068,13 +2068,15 @@ PyTypeObject PyStructType = {

/* ---- Standalone functions ---- */

#define MAXCACHE 100
#define MAXCACHE 512
static PyObject *cache = NULL;

static int
cache_struct_converter(PyObject *fmt, PyStructObject **ptr)
{
PyObject * s_object;
PyObject *s_object;
PyObject *key, *value;
Py_ssize_t pos;

if (fmt == NULL) {
Py_DECREF(*ptr);
Expand All @@ -2100,8 +2102,14 @@ cache_struct_converter(PyObject *fmt, PyStructObject **ptr)

s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL);
if (s_object != NULL) {
if (PyDict_GET_SIZE(cache) >= MAXCACHE)
PyDict_Clear(cache);
if (PyDict_GET_SIZE(cache) >= MAXCACHE) {
/* Drop the oldest item */
pos = 0;
if (PyDict_Next(cache, &pos, &key, &value)) {
if (PyDict_DelItem(cache, key) == -1)
PyErr_Clear();
}
}
/* Attempt to cache the result */
if (PyDict_SetItem(cache, fmt, s_object) == -1)
PyErr_Clear();
Expand Down