[Python-checkins] bpo-46576: bpo-46524: Disable compiler optimization within test_peg_generator. (#31015)

gpshead webhook-mailer at python.org
Wed Feb 2 15:15:26 EST 2022


https://github.com/python/cpython/commit/164a017e13ee96bd1ea1ae79f5ac9e25fe83994e
commit: 164a017e13ee96bd1ea1ae79f5ac9e25fe83994e
branch: main
author: Gregory P. Smith <greg at krypto.org>
committer: gpshead <greg at krypto.org>
date: 2022-02-02T12:15:16-08:00
summary:

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

files:
A Misc/NEWS.d/next/Tests/2022-01-29-12-37-53.bpo-46576.-prRaV.rst
M Tools/peg_generator/pegen/build.py

diff --git a/Misc/NEWS.d/next/Tests/2022-01-29-12-37-53.bpo-46576.-prRaV.rst b/Misc/NEWS.d/next/Tests/2022-01-29-12-37-53.bpo-46576.-prRaV.rst
new file mode 100644
index 0000000000000..be50fc8cbe0a5
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2022-01-29-12-37-53.bpo-46576.-prRaV.rst
@@ -0,0 +1,3 @@
+test_peg_generator now disables compiler optimization when testing
+compilation of its own C extensions to significantly speed up the
+testing on non-debug builds of CPython.
diff --git a/Tools/peg_generator/pegen/build.py b/Tools/peg_generator/pegen/build.py
index c69e5c9a5f26a..78789b94df2e4 100644
--- a/Tools/peg_generator/pegen/build.py
+++ b/Tools/peg_generator/pegen/build.py
@@ -1,6 +1,7 @@
 import itertools
 import pathlib
 import shutil
+import sys
 import sysconfig
 import tempfile
 import tokenize
@@ -32,6 +33,7 @@ def compile_c_extension(
     build_dir: Optional[str] = None,
     verbose: bool = False,
     keep_asserts: bool = True,
+    disable_optimization: bool = True,  # Significant test_peg_generator speedup.
 ) -> str:
     """Compile the generated source for a parser generator into an extension module.
 
@@ -61,6 +63,14 @@ def compile_c_extension(
     extra_link_args = get_extra_flags("LDFLAGS", "PY_LDFLAGS_NODIST")
     if keep_asserts:
         extra_compile_args.append("-UNDEBUG")
+    if disable_optimization:
+        if sys.platform == 'win32':
+            extra_compile_args.append("/Od")
+            extra_link_args.append("/LTCG:OFF")
+        else:
+            extra_compile_args.append("-O0")
+            if sysconfig.get_config_var("GNULD") == "yes":
+                extra_link_args.append("-fno-lto")
     extension = [
         Extension(
             extension_name,



More information about the Python-checkins mailing list