87

I'm trying to compile Python 3.6 on an arm based Linux machine, ./configure outputs this:

If you want a release build with all optimizations active (LTO, PGO, etc), please run ./configure --enable-optimizations.

what does --enable-optimizations do?

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
Yashar
  • 2,455
  • 3
  • 25
  • 31
  • 9
    What I read is that it turns on something called `Profile Guided Optimizations`. This type of optimization takes a long time to configure, but the resulting python binary interpreter is 10% faster at executing Python code. Check this reference (actually a github issue) - [Issue #160](https://github.com/docker-library/python/issues/160) – Siddharth Srinivasan Dec 31 '16 at 05:33
  • 2
    This used to add about 30 minutes to a compile of CPython, but as of Python3.8, it now runs a small subset of the regression tests for profiling. I didn't time that part, but my whole config/compile/install time was 6 minutes. – Jonathan Hartley Oct 23 '19 at 03:18

1 Answers1

86

This flag enables Profile guided optimization (PGO) and Link Time Optimization (LTO).

Both are expensive optimizations that slow down the build process but yield a significant speed boost (around 10-20% from what I remember reading).

The discussion of what these exactly do is beyond my knowledge and probably too broad for a single question. Either way, you can read a bit about LTO from the the docs on GCC which has an implementation for it and get a start on PGO by reading its wiki page.

Also, see the relevant issues opened on the Python Bug Tracker that added these:

  • Issue 24915: Profile Guided Optimization improvements (better training, llvm support, etc) (Added PGO.)
  • Issue 25702: Link Time Optimizations support for GCC and CLANG (Added LTO.)
  • Issue 26359: CPython build options for out-of-the box performance (Adds the --enable-optimizations flag to the configure script which enables the aforementioned optimizations.)

As pointed out by @Shuo in a comment and stated in Issue 28032, LTO isn't always enabled with the --enable-optimizations flag. Some platforms (depending on the supported version of gcc) will disable it in the configuration script.

Future versions of this flag will probably always have it enabled though, so it's pretty safe to talk about them both here.

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
  • 6
    Per https://bugs.python.org/issue28032, enable-optimizations only apply PGO, not LTO. – Shuo Mar 09 '17 at 04:42
  • @Shuo good catch, seems it broke some of the buildbots. I've updated the question to address this. – Dimitris Fasarakis Hilliard Mar 09 '17 at 09:49
  • on Centos 6, `--with-lto` won't work. It's gcc is old. – Farshid Ashouri May 23 '19 at 10:09
  • 2
    Just for clarity, is the downside here that the configuration of the Python installation itself is slowed, or the build portion of the program interpretation? That is, is this detrimental for small python scripts that are run many times, or is it a benefit for all runtime operations? – Nathaniel Ford Oct 30 '19 at 17:44
  • 6
    @NathanielFord It's the build time for Python itself that's slowed down by enabling the optimizations. Most source builds are debug ones for development purposes, so the default config settings favour faster binary builds at the expense of slightly slower test case execution times. – ncoghlan Jun 17 '20 at 03:51