Skip to content

Commit 164a017

Browse files
authored
bpo-46576: bpo-46524: Disable compiler optimization within test_peg_generator. (#31015)
Disable compiler optimization within test_peg_generator. This speed up test_peg_generator by always disabling compiler optimizations by using -O0 or equivalent when the test is building its own C extensions. A build not using --with-pydebug in order to speed up test execution winds up with this test taking a very long time as it would do repeated compilation of parser C code using the same optimization flags as CPython was built with. This speeds the test up 6-8x on gps-raspbian. Also incorporate's #31017's win32 conditional and flags. Co-authored-by: Kumar Aditya kumaraditya303
1 parent 89a0a90 commit 164a017

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test_peg_generator now disables compiler optimization when testing
2+
compilation of its own C extensions to significantly speed up the
3+
testing on non-debug builds of CPython.

Tools/peg_generator/pegen/build.py

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import itertools
22
import pathlib
33
import shutil
4+
import sys
45
import sysconfig
56
import tempfile
67
import tokenize
@@ -32,6 +33,7 @@ def compile_c_extension(
3233
build_dir: Optional[str] = None,
3334
verbose: bool = False,
3435
keep_asserts: bool = True,
36+
disable_optimization: bool = True, # Significant test_peg_generator speedup.
3537
) -> str:
3638
"""Compile the generated source for a parser generator into an extension module.
3739
@@ -61,6 +63,14 @@ def compile_c_extension(
6163
extra_link_args = get_extra_flags("LDFLAGS", "PY_LDFLAGS_NODIST")
6264
if keep_asserts:
6365
extra_compile_args.append("-UNDEBUG")
66+
if disable_optimization:
67+
if sys.platform == 'win32':
68+
extra_compile_args.append("/Od")
69+
extra_link_args.append("/LTCG:OFF")
70+
else:
71+
extra_compile_args.append("-O0")
72+
if sysconfig.get_config_var("GNULD") == "yes":
73+
extra_link_args.append("-fno-lto")
6474
extension = [
6575
Extension(
6676
extension_name,

0 commit comments

Comments
 (0)