[mypyc] Move API table definitions to .c files#21183
Open
p-sawicki wants to merge 5 commits intopython:masterfrom
Open
[mypyc] Move API table definitions to .c files#21183p-sawicki wants to merge 5 commits intopython:masterfrom
p-sawicki wants to merge 5 commits intopython:masterfrom
Conversation
JukkaL
reviewed
Apr 8, 2026
mypyc/lib-rt/static_data.c
Outdated
|
|
||
| #ifdef MYPYC_EXPERIMENTAL | ||
|
|
||
| void *LibRTStrings_API[LIBRT_STRINGS_API_LEN] = {0}; |
Collaborator
There was a problem hiding this comment.
Now all the API tables will always be included in each build, which increases the memory usage of each module a little. It's not a big deal right now, but this is not scalable if we add many librt submodules in the future. Could we instead have librt_vecs_static.c etc. and add them as dependencies?
Collaborator
Author
There was a problem hiding this comment.
yeah that's a good point. i've moved the definitions to separate files and extended the dependency handling to compile/include them when needed.
for more information, see https://pre-commit.ci
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.
lib-rt API tables are defined as static variables in lib-rt headers. This means that each translation unit gets its own independent instance of these variables.
That becomes a problem in multi-file compilation mode when using
BytesWriter.write, as this function is translated toCPyBytesWriter_Writeby mypyc which is defined inbytes_writer_extra_ops.c. With multi-file compilation,bytes_writer_extra_ops.cis compiled as its own translation unit and gets linked with the C extension compiled from python files.The C extension TU copies the
librt.stringscapsule contents into the global table but because it's static this is not visible in the table in thebytes_writer_extra_ops.cTU, which stays zero-initialized. This results in a seg fault with the following call chain:CPyBytesWriter_Write->CPyBytesWriter_EnsureSize->LibRTStrings_ByteWriter_grow_buffer_internal->LibRTStrings_API[5]-> oops all zerosTo fix this, declare the tables as
externand define them in .c files so there's only one global version of each variable. There's one new .c file for each lib-rt module that is compiled or included when mypyc detects a dependency on that module.