How to deactivate method cache in CPython 2.7.2?

I am trying to implement my own method cache method. To do this, first I want to disable the existing method cache implemented in CPython 2.7.2, since I would also like to compare CPython without this method cache.

I searched the code and found the code of the method code in the file 'typeobject.c':

/* Internal API to look for a name through the MRO. This returns a borrowed reference, and doesn't set an exception! */ PyObject * _PyType_Lookup(PyTypeObject *type, PyObject *name) { Py_ssize_t i, n; PyObject *mro, *res, *base, *dict; unsigned int h; if (MCACHE_CACHEABLE_NAME(name) && PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) { /* fast path */ h = MCACHE_HASH_METHOD(type, name); if (method_cache[h].version == type->tp_version_tag && method_cache[h].name == name) return method_cache[h].value; } /* Look in tp_dict of types in MRO */ mro = type->tp_mro; 

From what I understand, if the method is not in the method cache, you traverse the MRO. I just want to deactivate the entire method cache in the cleanest way. Any suggestions? :)

Antonio

+4
source share
1 answer

I think the cleanest way would be to find the occurrences of if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG)) in typeobject.c and replace !PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG) with 0. I have to change the lines to 3. Then edit the #define MCACHE_CACHEABLE_NAME(name) macro at the top of the file so that it is always false.

Then just recompile Python and the method cache will disappear. To make one of these changes would be enough to stop the cache, but I think that looking at the code you would like to stop it, doing unnecessary work, keeping the cache unused.

My question, if only there was, if you are trying to replace it with something else, then, of course, you are still working on this code, so do you need to delete all existing method cache code first to give you a clean start?

+5
source

Source: https://habr.com/ru/post/1384100/


All Articles