Some form of inline cache for C API attribute/method loading #406
Replies: 5 comments 8 replies
-
One big difference is that the same C-API can have many diverse call sites, while the current bytecode caches are extremely localized. I'm not sure that there are typically many identical calls to
static _PyAttrCache cache = {0};
attr = _PyObject_GetAttrCached(&cache, o, name); I'm not sure I'm a huge fan of either option, since the first one adds a large-ish maintenance burden for every API we cache, and the second still requires C-extension authors to modify their code. (I personally believe that code that makes heavy use of the Python object model ( |
Beta Was this translation helpful? Give feedback.
-
I dunno. I agree that neither option is that attractive, but as an API user I'd rather have the API automatically do the caching for me without me changing my code (not even recompiling, assuming it's in the stable ABI). I think this is what made |
Beta Was this translation helpful? Give feedback.
-
Update: I tried it and it's not worth it. It's only a 5% improvement when I removed one lookup from
Branch: https://github.com/Fidget-Spinner/cpython/tree/inca_capi |
Beta Was this translation helpful? Give feedback.
-
FYI, this got me thinking more generally about the C-API. I wrote up my thoughts in #410. |
Beta Was this translation helpful? Give feedback.
-
We did this sort of thing a bit in Pyston v1, though mechanically it worked a bit differently, where the runtime could invoke the jit. We did find some cases where it made sense, such as |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Watching Kevin's PyCon talk on modern code for interpreters made me learn that Python method/attribute loads are now faster in Python than in the C API.
All the
CallMethod
C API functions need to lookup the names of the methods. This is also the case forPyObject_GetAttr
and attributes.I wonder if we can implement some sort of "inline cache" infrastructure for C API attribute/method loads. The cache would behave similarly to the current
_Py_ID
macros and would mimic the behavior and logic of itsLOAD_METHOD/LOAD_ATTR
PEP 659 cousin. However, it would only specialize for one "form". Ideally, we should only use this at call sites where there's largely only one "specialization" form that is well known. The specialization will thus be simpler than the Python counterpart as we have much more information in C.I plan to try this out for
_PySys_GetAttr
first because its basically just one form (LOAD_ATTR_MODULE
). Following that we can then try methods because the majority of those are two forms.My wishful thinking end goal is that eventually we can have something for the public C API to boost the performance of C extensions, rather than C extension authors having to rewrite large swathes of code.However, I think that's too far out there for now.Beta Was this translation helpful? Give feedback.
All reactions