Skip to content

Commit deaa5c7

Browse files
authored
[mypyc] include instead of compiling rt .c files in non-multifile mode (#7770)
The idea here is just that it reduces the number of compiler invocations which speeds things up and reduces spew.
1 parent abf63d1 commit deaa5c7

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

mypyc/build.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -472,13 +472,15 @@ def mypycify(
472472
'/wd9025', # warning about overriding /GL
473473
]
474474

475-
# Copy the runtime library in
475+
# In multi-file mode, copy the runtime library in.
476+
# Otherwise it just gets #included to save on compiler invocations
476477
shared_cfilenames = []
477-
for name in ['CPy.c', 'getargs.c']:
478-
rt_file = os.path.join(build_dir, name)
479-
with open(os.path.join(include_dir(), name), encoding='utf-8') as f:
480-
write_file(rt_file, f.read())
481-
shared_cfilenames.append(rt_file)
478+
if multi_file:
479+
for name in ['CPy.c', 'getargs.c']:
480+
rt_file = os.path.join(build_dir, name)
481+
with open(os.path.join(include_dir(), name), encoding='utf-8') as f:
482+
write_file(rt_file, f.read())
483+
shared_cfilenames.append(rt_file)
482484

483485
extensions = []
484486
for (group_sources, lib_name), (cfilenames, deps) in zip(groups, group_cfilenames):

mypyc/emitmodule.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ def generate_c_for_modules(self) -> List[Tuple[str, str]]:
227227
multi_file = self.use_shared_lib and self.multi_file
228228

229229
base_emitter = Emitter(self.context)
230+
# When not in multi-file mode we just include the runtime
231+
# library c files to reduce the number of compiler invocations
232+
# needed
233+
if not self.multi_file:
234+
base_emitter.emit_line('#include "CPy.c"')
235+
base_emitter.emit_line('#include "getargs.c"')
230236
base_emitter.emit_line('#include "__native{}.h"'.format(self.group_suffix))
231237
base_emitter.emit_line('#include "__native_internal{}.h"'.format(self.group_suffix))
232238
emitter = base_emitter

mypyc/test/test_run.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
from mypyc.build import mypycify
4242
4343
setup(name='test_run_output',
44-
ext_modules=mypycify({}, separate={}, skip_cgen_input={!r}, strip_asserts=False),
44+
ext_modules=mypycify({}, separate={}, skip_cgen_input={!r}, strip_asserts=False,
45+
multi_file={}),
4546
)
4647
"""
4748

@@ -192,7 +193,7 @@ def run_case_inner(self, testcase: DataDrivenTestCase) -> None:
192193
setup_file = os.path.abspath(os.path.join(WORKDIR, 'setup.py'))
193194
# We pass the C file information to the build script via setup.py unfortunately
194195
with open(setup_file, 'w', encoding='utf-8') as f:
195-
f.write(setup_format.format(module_paths, self.separate, cfiles))
196+
f.write(setup_format.format(module_paths, self.separate, cfiles, self.multi_file))
196197

197198
if not run_setup(setup_file, ['build_ext', '--inplace']):
198199
if testcase.config.getoption('--mypyc-showc'):

0 commit comments

Comments
 (0)