Optimize C extension hot paths#353
Merged
jensens merged 5 commits intozopefoundation:masterfrom Feb 27, 2026
Merged
Conversation
7a7358e to
e13815a
Compare
Eliminates PyTuple_New + N x PyTuple_SET_ITEM + Py_DECREF on every cache-valid lookup call. Also enables early exit on first mismatch.
Direct struct field access avoids the generic truth protocol dispatch (type slot lookup -> sq_length or nb_bool) for the name parameter.
… a tuple Use PyTuple_CheckExact fast path to avoid allocating a copy when the required argument is already a tuple (the common case from Python callers).
Use a pre-interned static string with PyDict_GetItem instead of PyDict_GetItemString with a C literal (which creates a temporary Python string on each call). Also replace direct ob_type struct access with the Py_TYPE() macro for free-threaded compatibility.
e13815a to
0a3ecc7
Compare
tseaver
approved these changes
Feb 26, 2026
Member
Author
|
https://status.coveralls.io/ is down and blocks the CI |
Member
|
@jensens I just subscribed to the issue at https://status.coveralls.io/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Four targeted optimizations to the C extension hot paths, reducing
overhead in adapter lookups and interface calls.
Changes
_verify(): in-place generation comparison — Compare registrygeneration counters directly against the stored snapshot instead of
allocating a temporary tuple via
_generations_tuple()on everycache-valid lookup. Enables early exit on first mismatch.
_getcache():PyUnicode_GET_LENGTHinstead ofPyObject_IsTrue—Direct struct field access instead of generic truth protocol dispatch.
_lookup/_lookupAll/_subscriptions:PyTuple_CheckExactfast path —Skip
PySequence_Tupleallocation whenrequiredis already a tuple(the common case from Python callers).
IB__call__: intern_CALL_CUSTOM_ADAPTstring +Py_TYPEmacro —Pre-interned static string with
PyDict_GetIteminstead ofPyDict_GetItemString(which creates a temporary Python string eachcall). Also replaces direct
ob_typeaccess withPy_TYPE()forfree-threaded Python compatibility.
Benchmark (Python 3.14.0, Linux, best of 3 runs)
lb.lookup(...))IFoo(foo)(call + adapt)providedBy(foo)