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

bpo-29585: optimize site.py startup time #136

Merged
merged 9 commits into from
Jun 28, 2017

Conversation

methane
Copy link
Member

@methane methane commented Feb 16, 2017

Skip importing sysconfig when possible.

Median +- std dev: [default] 15.8 ms +- 0.0 ms -> [patched] 14.7 ms +- 0.0 ms: 1.07x faster (-7%)

(bpo-29585)

@methane
Copy link
Member Author

methane commented Feb 16, 2017

$ cp  build/lib.linux-x86_64-3.7/_sysconfigdata_m_linux_x86_64-linux-gnu.py sysconfigdata
$ ./python -c 'import sysconfigdata'  # create pyc file
$ ./python -m timeit -s 'import sysconfigdata, importlib' -- 'importlib.reload(sysconfigdata)'
1000 loops, best of 5: 269 usec per loop

Since 'PYTHONFRAMEWORK' is in sysconfigdata, I cannot stop importing them.
But other cases, I can skip importing sysconfig and sysconfigdata completely.

@methane methane force-pushed the optimize-site-startup branch from 4f58b0c to 483769e Compare February 16, 2017 14:35
Copy link
Member

@ned-deily ned-deily left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a major change: by duplicating code from sysconfig.py into site.py, there would now be a implicit link between the two and a potential maintenance problem. If we were to do this, there should at least be mention of these duplications in sysconfig.py and/or tests for the duplicated behavior. Also, there should be a b.p.o issue for this proposed change.

@methane methane changed the title optimize site.py [RFC] bpo-29585: optimize site.py startup time Feb 17, 2017
Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dislike this approach. I suggest to experiment a _site extension module: see the issue.

@methane
Copy link
Member Author

methane commented Feb 18, 2017

I've addes sys._framework and remove sysconf dependency completely.

@methane methane force-pushed the optimize-site-startup branch from dfeced1 to 45f5e9a Compare February 18, 2017 05:04
@methane methane changed the title [RFC] bpo-29585: optimize site.py startup time [RFC] [bpo-29585](https://bugs.python.org/issue29585): optimize site.py startup time Feb 18, 2017
@methane methane changed the title [RFC] [bpo-29585](https://bugs.python.org/issue29585): optimize site.py startup time [RFC] bpo-29585: optimize site.py startup time Feb 18, 2017
@methane methane force-pushed the optimize-site-startup branch from 45f5e9a to 07369f1 Compare February 18, 2017 05:10
@methane methane changed the title [RFC] bpo-29585: optimize site.py startup time bpo-29585: optimize site.py startup time Feb 19, 2017
methane added 3 commits June 28, 2017 19:28
Skip importing sysconfig when possible.

Median +- std dev: [default] 15.8 ms +- 0.0 ms -> [patched] 14.7 ms +- 0.0 ms: 1.07x faster (-7%)
@methane methane force-pushed the optimize-site-startup branch from 75b4c6c to ff0d05c Compare June 28, 2017 10:29
@vstinner vstinner added the performance Performance or resource usage label Jun 28, 2017
@methane
Copy link
Member Author

methane commented Jun 28, 2017

@ned-deily I created bpo issue and added comment about duplicated code.

@methane
Copy link
Member Author

methane commented Jun 28, 2017

@Haypo sysconfig (and _sysconfigdata_...) module is relatively large and
most applications (except packaging tools) doesn't use it at all.
Now this pull request is focusing it.

Slow code path is skipped by #167 although abspath is still slow.

@vstinner
Copy link
Member

vstinner commented Jun 28, 2017

A quick & dirty benchmark using my perf module: I see an improvement of -0.8 ms (1.05x faster) on Python startup time.

haypo@selma$ ./python -m perf command --inherit=PYTHONPATH -v -o pr136.json -- ./python -c pass  
...
haypo@selma$ ./python -m perf command --inherit=PYTHONPATH -v -o ref.json -- ./python -c pass
...
haypo@selma$ ./python -m perf compare_to ref.json pr136.json 
Mean +- std dev: [ref] 17.4 ms +- 0.8 ms -> [pr136] 16.6 ms +- 1.1 ms: 1.05x faster (-5%)

EDIT: this benchmark was run on my Linux laptop.

@vstinner
Copy link
Member

Hum, did I miss something? sysconfig is still imported by the site module on macOS by getsitepackages():

        if sys.platform == "darwin":
            # for framework builds *only* we add the standard Apple
            # locations.
            from sysconfig import get_config_var
            framework = get_config_var("PYTHONFRAMEWORK")
            if framework:
                sitepackages.append(
                        os.path.join("/Library", framework,
                            '%d.%d' % sys.version_info[:2], "site-packages"))

macbook:master haypo$ ./python.exe -c 'import sys; print("sysconfig" in sys.modules)'
True

I suggest this additionnal change:

diff --git a/Lib/site.py b/Lib/site.py
index 500c59b..929252d 100644
--- a/Lib/site.py
+++ b/Lib/site.py
@@ -334,15 +334,12 @@ def getsitepackages(prefixes=None):
         else:
             sitepackages.append(prefix)
             sitepackages.append(os.path.join(prefix, "lib", "site-packages"))
-        if sys.platform == "darwin":
-            # for framework builds *only* we add the standard Apple
-            # locations.
-            from sysconfig import get_config_var
-            framework = get_config_var("PYTHONFRAMEWORK")
-            if framework:
-                sitepackages.append(
-                        os.path.join("/Library", framework,
-                            '%d.%d' % sys.version_info[:2], "site-packages"))
+        # for framework builds *only* we add the standard Apple
+        # locations.
+        if sys.platform == "darwin" and sys._framework:
+            sitepackages.append(
+                    os.path.join("/Library", sys._framework,
+                        '%d.%d' % sys.version_info[:2], "site-packages"))
     return sitepackages
 
 def addsitepackages(known_paths, prefixes=None):

With this additionnal change, the speedup on macOS is quite significant: -13.4 ms (1.61x faster)!

macbook:master haypo$ ./python.exe -m perf command --inherit=PYTHONPATH -v -o ref.json -- ./python.exe -c pass 
...
macbook:master haypo$ ./python.exe -m perf command --inherit=PYTHONPATH -v -o pr136.json -- ./python.exe -c pass
...
macbook:master haypo$ ./python.exe -m perf compare_to ref.json pr136.json 
Mean +- std dev: [ref] 35.4 ms +- 1.7 ms -> [pr136] 22.0 ms +- 1.9 ms: 1.61x faster (-38%)

cc @1st1: Yury, since you use macOS, you probably want to see this change merged :-)

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New review.

Lib/site.py Outdated
@@ -234,6 +234,27 @@ def check_enableusersite():

return True


def _getuserbase():
# Stripped version of sysconfig._getuserbase()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please elaborate this comment? Please explain that the function was duplicated to speedup Python startup and avoid the sysconfig import in the site module. Add a reference to the bpo 29585.

It seems like you modified this change: you moved "if env_base: return env_base" at the top. Please modify also sysconfig._getuserbase(). I would also prefer the sysconfig also uses sys._framework, instead of get_config_var("PYTHONFRAMEWORK").

Lib/site.py Outdated

def _get_path(userbase):
# stripped version of sysconfig.get_path('purelib', os.name + '_user')
version = sys.version_info[:2]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is [:2] really needed?

Lib/site.py Outdated
return USER_BASE


def _get_path(userbase):
# stripped version of sysconfig.get_path('purelib', os.name + '_user')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, please elaborate the comment. (Maybe point to _getuserbase() above?)

@vstinner
Copy link
Member

Ned Deily: "This is a major change: by duplicating code from sysconfig.py into site.py, there would now be a implicit link between the two and a potential maintenance problem. If we were to do this, there should at least be mention of these duplications in sysconfig.py and/or tests for the duplicated behavior."

@methane added a comment in sysconfig.py, IMHO it's enough.

About testing: @methane, can you try to write a test to check that site and sysconfig return the same value for the two private functions? You may have to tag the unit test with @cpython_only, since the two new site functions are private.

Ned Deily: "Also, there should be a b.p.o issue for this proposed change."

Done.

Since most Ned's requests are done, I dismiss his review.

@vstinner vstinner dismissed ned-deily’s stale review June 28, 2017 12:19

@methade modified this PR.

@vstinner
Copy link
Member

Except of my minor commits, I now like the overall shape of the PR. I now agree that writing a new _site module is not necessary, it's better to keep all code in the site.py file.

@methane
Copy link
Member Author

methane commented Jun 28, 2017

On macOS, performance gain is more impressive than Linux:

$ ./python.exe -m perf compare_to ref.json pr136.json
Mean +- std dev: [ref] 30.4 ms +- 0.9 ms -> [pr136] 21.6 ms +- 4.0 ms: 1.40x faster (-29%)

@methane
Copy link
Member Author

methane commented Jun 28, 2017

Oh, I'm sorry. I missed your above comment.

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I would like to see an answer to the question on tests (check that site functions return the same result than sysconfig?) before approving your change :-)

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The latest change is much better than the first iteration, and it now LGTM!

@methane methane merged commit a8f8d5b into python:master Jun 28, 2017
@methane methane deleted the optimize-site-startup branch June 28, 2017 15:31
@methane
Copy link
Member Author

methane commented Jun 28, 2017

Thank you for review.

@1st1
Copy link
Member

1st1 commented Jun 28, 2017

Why couldn't we make 'sysconfig' to import a couple of private functions from 'site', why copying?

@vstinner
Copy link
Member

Why couldn't we make 'sysconfig' to import a couple of private functions from 'site', why copying?

It's not a pure copy/paste, one function is specialized for site use case. I dislike the idea of importing site code from sysconfig.

@vstinner
Copy link
Member

Thank you for review.

Thanks for this cool and cheap speedup!

@methane
Copy link
Member Author

methane commented Jun 28, 2017

Ah, I've missed build failure on Windows.

@vstinner
Copy link
Member

Oh, Windows build is broken :-(

     2>..\Python\sysmodule.c(1968): error C2065: 'PYTHONFRAMEWORK': undeclared identifier [C:\buildbot.python.org\3.x.kloth-win64\build\PCbuild\pythoncore.vcxproj]

Need to fix PC/pyconfig.h?

@vstinner
Copy link
Member

Oh, AppVeyor didn't catch the bug simply because it wasn't run on this PR!

@vstinner
Copy link
Member

@methane proposed PR #2476 to fix PC/pyconfig.h, I proposed PR #2477.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance or resource usage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants