Question

I'm trying to implement my own kind method cache. For that, first I want to disable the existing method cache implemented in CPython 2.7.2, since I would also like to benchmark CPython without this method cache.

I have been looking into the code and found some method cache code in the 'typeobject.c' file:

/* 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 simply want to deactivate the whole method cache, in the cleanest way. Any suggestions? :)

Antonio

Was it helpful?

Solution

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 think that's 3 lines you would have to change. Then edit the #define MCACHE_CACHEABLE_NAME(name) macro near the top of the file to always be false.

Then just recompile Python and the method cache will be gone. Making either of these changes would be enough to stop the cache working, but I think from looking at the code you would want both to stop it doing unnecessary work maintaining the unused cache.

My question though would be if you are trying to replace it with something else then surely your working on that code anyway so shouldn't you just be removing all of the existing method cache code first to give yourself a clean start?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top