我正在尝试实现我自己的方法缓存。为此,首先我想禁用在 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。我只是想以最干净的方式停用整个方法缓存。有什么建议么?:)
安东尼奥