Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PGO (profile-guided optimizations) build for Python #423

Closed
pitrou opened this issue Aug 27, 2015 · 16 comments
Closed

PGO (profile-guided optimizations) build for Python #423

pitrou opened this issue Aug 27, 2015 · 16 comments
Assignees
Labels
os-linux os-win package_request Package build requests (new, updates, and rebulds) type-enhancement

Comments

@pitrou
Copy link

pitrou commented Aug 27, 2015

To enable PGO in the Python builds, replace the make line in build.sh with the following:

# The following is basically "make profile-opt", but with additional
# flexibility if you want to add e.g. "-j4" when building

# Tests to exclude from the profiling run (too slow, or crashy)
EXCLUDE_TESTS="test_gdb test_subprocess test_faulthandler \
    test_multiprocessing test_multiprocessing_fork \
    test_multiprocessing_forkserver test_multiprocessing_spawn \
    test_multiprocessing_main_handling test_signal \
    test_concurrent_futures"

PROFILE_TASK="-m test.regrtest -w -uall,-audio -x $EXCLUDE_TESTS"

# Build with support for profile generation
make build_all_generate_profile
# Run workload to generate profile data
# Errors in the test suite are ignored
make run_profile_task PROFILE_TASK="$PROFILE_TASK" || true
# Rebuild with profile guided optimizations
make clean
make build_all_use_profile

# End "make profile-opt"

On my machine, this makes most benchmarks from the Python benchmarks suite (https://hg.python.org/benchmarks) between 10% and 25% faster.

Note this was done with the Python 3.5.0rc1 build, but it should be quite portable to other Python versions. As for Windows, I'm afraid the way to do it would be entirely different...

@pitrou
Copy link
Author

pitrou commented Aug 27, 2015

For Windows, you could take a look at the PCbuild/build_pgo.bat script in the CPython source tree. You'll have to modify the %job% variable to contain the right training workload (the PROFILE_TASK in the snippet above).

@ahmadia
Copy link

ahmadia commented Aug 27, 2015

+1

@groutr
Copy link

groutr commented Aug 27, 2015

For reference: https://bugs.python.org/issue24915

@groutr
Copy link

groutr commented Aug 27, 2015

Are there any implications to compiling Python in this manner?

@pitrou
Copy link
Author

pitrou commented Aug 27, 2015

The main implication is that building is much longer (mostly due to the training part) :-)

@pitrou
Copy link
Author

pitrou commented Aug 27, 2015

The package size may be a tiny bit larger too. On my machine it grew from 29 MB to 31.5 MB. I don't think that's important at all.

@groutr
Copy link

groutr commented Sep 1, 2015

I don't think the size increase is a problem.
I can test building our python with this. I'll report back when it's finished.

@groutr
Copy link

groutr commented Sep 21, 2015

So, I have done a little experimenting with the PGO builds. Using your test code above, it achieved a nice 20% (tested with PyBench) overall speedup on my OSX machine. A discussion has been started to possibly release PGO builds in the future for Python 2.7, 3.4, and 3.5 on Linux/OSX. It may be possible to release Windows as well (I hope to figure out PGO on windows in the near future).

Thank you for suggesting this.

@groutr groutr self-assigned this Sep 21, 2015
@groutr groutr added os-win os-linux package_request Package build requests (new, updates, and rebulds) labels Sep 21, 2015
@groutr
Copy link

groutr commented Sep 30, 2015

My tests with windows PGO builds using Python 3.5 only show a modest overall improvement of ~8%.
EDIT: I forgot to exclude the tests that are excluded in the first post. The new builds show ~15% improvement.

@pitrou
Copy link
Author

pitrou commented Nov 25, 2015

Any recent update on this? :-)

@pitrou
Copy link
Author

pitrou commented Mar 17, 2016

Also https://bugs.python.org/issue25702 (Link Time Optimizations support for GCC and CLANG) and https://bugs.python.org/issue26359 (CPython build options for out-of-the box performance).

@ijstokes
Copy link

@tpn do you have any insights on this from the Windows perspective?

@pitrou have you or anyone else (e.g. @groutr ) tried this with icc or pgc? Or are these results just for gcc? IIRC there was discussion about gcc, alternatives to gcc, and using a more recent gcc that wouldn't be as widely supported for "legacy hardware" but could get us even better performance for ~80% of users (x86_64).

@pitrou
Copy link
Author

pitrou commented Mar 17, 2016

Just for gcc. I don't think icc is a reasonable alternative as it can break some expectations in certain cases (see reports at https://bugs.python.org/issue?%40columns=id%2Cactivity%2Ctitle%2Ccreator%2Cassignee%2Cstatus%2Ctype&%40sort=-activity&%40filter=status&%40action=searchid&ignore=file%3Acontent&%40search_text=icc&submit=search&status=-1%2C1%2C2%2C3).

I haven't tried with old gcc, all my experiments have been with a reasonably recent gcc (which is required for LTO, by the way).

@insertinterestingnamehere

Switching to a newer toolchain (see anaconda/docker-images#20) should make it possible to build with a newer gcc on older systems, so, hopefully, the gcc version will no longer be a major limitation.

@csoja csoja assigned ilanschnell and unassigned groutr May 3, 2016
@mingwandroid mingwandroid self-assigned this Aug 15, 2017
@mingwandroid
Copy link

Closed, Python 3.6 uses LTO and PGO on all platforms and 3.5 uses LTO and PGO on macOS and Linux.

To enable these features on Python 2 I would probably need to back-port lots of changes to the build scripts and I do not want to risk breaking things for people using this legacy Python version. Thanks for the pointers @pitrou.

@mingwandroid
Copy link

Small update to this, LTO and PGO is also enabled now on Python 3.5 on Windows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
os-linux os-win package_request Package build requests (new, updates, and rebulds) type-enhancement
Projects
None yet
Development

No branches or pull requests

7 participants