gh-127295: ctypes: Switch field accessors to fixed-width integers #127297
+487
−646
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.
See the issue.
This refactoring has a miniscule but consistent performance benefit (1.01x geometric mean, no known cases with a slowdown). See my bench script & results. Thanks @vstinner for
pyperf
and instructions for isolating CPU cores!For the repetitive parts, this uses a combination of macros and Argument Clinic for code generation (inline, so one can easily inspect the results). This is the most readable/maintainable of various approaches I tried.
(No, I'm not a fan of the giant macros, but it beats both code generated from f-strings that lack syntax highlighting, and external multiple-include files. Oh, and the
///////////
lines make it easy to see misplaced backslashes.)Several related changes are included:
asserts
in the wild, to see if I missed someone who's relying on the detail.)switch
in_ctypes_get_fielddesc
, rather than a linear search. (This requires that there are now several boring chunks with a line for each of the format codes,sbBcdCEFgfhHiIlLqQPzuUZXvO
, but Argument Clinic makes this bearable.)struct fielddesc
to move all the accessors together, making code generation a bit easierBSTR
in function names to its code charX
, to match the other accessors