Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-127295: ctypes: Switch field accessors to fixed-width integers #127297

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

encukou
Copy link
Member

@encukou encukou commented Nov 26, 2024

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:

  • Pass a meaningful size argument, rather than zero, to non-bitfield accessors. (This would be great to have for size-generic functions, and it should be a private implementation detail. I want to do this now and get the asserts in the wild, to see if I missed someone who's relying on the detail.)
  • Use a 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.)
  • Rearrange struct fielddesc to move all the accessors together, making code generation a bit easier
  • Motley consistency improvements, like changing BSTR in function names to its code char X, to match the other accessors

Replace formattable by a switch.

Generate some repetitive parts of handling individual C types.
@encukou
Copy link
Member Author

encukou commented Nov 26, 2024

(Yes, this is wrong as-is; hopefully due to a stupid mistake on my part.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant