如何在CPython 2.7.2中停用方法缓存?

如何在CPython 2.7.2中停用方法缓存?,python,caching,methods,cpython,Python,Caching,Methods,Cpython,我正在尝试实现我自己的方法缓存。为此,首先我想禁用在CPython 2.7.2中实现的现有方法缓存,因为我还想在没有此方法缓存的情况下对CPython进行基准测试 我一直在研究代码,在“typeobject.c”文件中发现了一些方法缓存代码: /* Internal API to look for a name through the MRO. This returns a borrowed reference, and doesn't set an exception! */ PyObj

我正在尝试实现我自己的方法缓存。为此,首先我想禁用在CPython 2.7.2中实现的现有方法缓存,因为我还想在没有此方法缓存的情况下对CPython进行基准测试

我一直在研究代码,在“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;
据我所知,如果方法不在方法缓存中,则遍历MRO。我只是想以最干净的方式停用整个方法缓存。有什么建议吗?:)


Antonio

我认为最干净的方法是在typeobject.c中找到(!PyType\u HasFeature(type,Py\u TPFLAGS\u HAVE\u VERSION\u TAG))并替换
!PyType\u HasFeature(类型、Py\u TPFLAGS\u具有版本标签)
带有0。我想你得换3行。 然后将文件顶部附近的
#define MCACHE_CACHEABLE_NAME(NAME)
宏编辑为始终为false

然后只需重新编译Python,方法缓存就会消失。做这些更改中的任何一个都足以停止缓存工作,但是我认为从代码来看,您希望停止它执行维护未使用缓存的不必要工作

但我的问题是,如果您试图用其他东西替换它,那么无论如何,您肯定在处理该代码,那么您不应该首先删除所有现有的方法缓存代码,以便给自己一个干净的开始吗