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

fix 20ms startup penalty added by virtualenv #2317

Merged
merged 1 commit into from Mar 18, 2022
Merged

fix 20ms startup penalty added by virtualenv #2317

merged 1 commit into from Mar 18, 2022

Conversation

asottile
Copy link
Contributor

@asottile asottile commented Mar 14, 2022

before:

$ rm -rf vvv; virtualenv vvv -ppython3.10; time vvv/bin/python3 -c ''; time vvv/bin/python3 -c ''
created virtual environment CPython3.10.2.final.0-64 in 712ms
  creator CPython3Posix(dest=/tmp/virtualenv/vvv, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/asottile/.local/share/virtualenv)
    added seed packages: pip==22.0.4, setuptools==60.9.3, wheel==0.37.1
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

real	0m0.037s
user	0m0.037s
sys	0m0.000s

real	0m0.032s
user	0m0.031s
sys	0m0.000s

after:

$ rm -rf vvv; virtualenv vvv -ppython3.10; time vvv/bin/python3 -c ''; time vvv/bin/python3 -c ''
created virtual environment CPython3.10.2.final.0-64 in 677ms
  creator CPython3Posix(dest=/tmp/virtualenv/vvv, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/asottile/.local/share/virtualenv)
    added seed packages: pip==22.0.4, setuptools==60.9.3, wheel==0.37.1
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

real	0m0.022s
user	0m0.022s
sys	0m0.000s

real	0m0.015s
user	0m0.015s
sys	0m0.000s

just to confirm the hack still works as well:

$ vvv/bin/python3 -c 'import distutils.dist; print(distutils.dist.Distribution.parse_config_files.__module__)'
_virtualenv

Thanks for contributing, make sure you address all the checklists (for details on how see

development documentation)!

  • ran the linter to address style issues (tox -e fix_lint)
  • wrote descriptive pull request text
  • ensured there are test(s) validating the fix (N/A)
  • added news fragment in docs/changelog folder
  • updated/extended the documentation (N/A)

@asottile asottile changed the title fix 30ms startup penalty added by virtualenv fix 20ms startup penalty added by virtualenv Mar 14, 2022
@asottile asottile marked this pull request as ready for review March 14, 2022 16:52
Copy link
Contributor

@gaborbernat gaborbernat left a comment

Choose a reason for hiding this comment

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

Can you explain from where does the big performance gain comes?

@asottile
Copy link
Contributor Author

Can you explain from where does the big performance gain comes?

deferring the imports so they aren't in the critical path

@gaborbernat
Copy link
Contributor

deferring the imports so they aren't in the critical path

But the import needs to happen no matter what, so does it matter if you run it at import time or when the pth file is loaded? Also how come this import is so expensive?

@asottile
Copy link
Contributor Author

deferring the imports so they aren't in the critical path

But the import needs to happen no matter what, so does it matter if you run it at import time or when the pth file is loaded? Also how come this import is so expensive?

nope! the import only needs to happen when distutils is imported

the import is expensive because it pulls in 15+ other modules (including typing):

$ importtime-waterfall --include-interpreter-startup t
_frozen_importlib_external (320)
  _io (170)
  marshal (43)
  posix (386)
zipimport (157)
  time (97)
encodings (578)
  codecs (351)
    _codecs (56)
  encodings.aliases (447)
encodings.utf_8 (216)
_signal (124)
io (176)
  abc (126)
    _abc (35)
site (950)
  os (421)
    stat (72)
      _stat (55)
    _collections_abc (950)
    posixpath (74)
      genericpath (42)
  _sitebuiltins (72)
  _virtualenv (386)
    functools (724)
      collections (985)
        itertools (220)
        keyword (140)
        operator (325)
          _operator (73)
        reprlib (184)
        _collections (70)
      types (263)
      _functools (57)
    importlib.abc (549)
      importlib (182)
        warnings (261)
      importlib.machinery (69)
      importlib._abc (151)
      importlib.resources.abc (27)
        importlib.resources (162)
          importlib.resources._common (311)
            pathlib (945)
              fnmatch (177)
                re (538)
                  enum (2545)
                  sre_compile (322)
                    _sre (75)
                    sre_parse (371)
                      sre_constants (329)
                  _locale (212)
                  copyreg (162)
              ntpath (131)
                nt (76)
                nt (70)
                nt (104)
                nt (101)
                nt (68)
              errno (70)
              urllib.parse (1284)
                urllib (123)
            tempfile (421)
              shutil (634)
                zlib (81)
                bz2 (403)
                  _compression (223)
                  _bz2 (213)
                lzma (259)
                  _lzma (299)
              random (517)
                math (71)
                bisect (142)
                  _bisect (36)
                _random (43)
                _sha512 (46)
              weakref (434)
                _weakrefset (230)
            contextlib (739)
            typing (2645)
              collections.abc (185)
              _typing (118)
            importlib.resources.abc (266)
            importlib.resources._adapters (322)
          importlib.resources._legacy (237)
    importlib.util (122)
  _distutils_hack (328)
  sitecustomize (254)
t (218)

after:

$ importtime-waterfall --include-interpreter-startup t
_frozen_importlib_external (305)
  _io (165)
  marshal (42)
  posix (385)
zipimport (123)
  time (93)
encodings (603)
  codecs (348)
    _codecs (56)
  encodings.aliases (443)
encodings.utf_8 (215)
_signal (122)
io (170)
  abc (123)
    _abc (35)
site (806)
  os (414)
    stat (68)
      _stat (53)
    _collections_abc (889)
    posixpath (72)
      genericpath (40)
  _sitebuiltins (71)
  _virtualenv (371)
  _distutils_hack (408)
  sitecustomize (123)
t (209)

@gaborbernat gaborbernat merged commit a16f609 into pypa:main Mar 18, 2022
@asottile asottile deleted the perf-fix branch March 18, 2022 14:16
@ofek
Copy link
Sponsor Contributor

ofek commented Mar 18, 2022

Nice!

bors bot added a commit to ChrisRBe/PP-P2P-Parser that referenced this pull request Mar 21, 2022
526: build(deps): bump virtualenv from 20.13.3 to 20.13.4 r=ChrisRBe a=dependabot[bot]

Bumps [virtualenv](https://github.com/pypa/virtualenv) from 20.13.3 to 20.13.4.
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a href="https://github.com/pypa/virtualenv/blob/20.13.4/docs/changelog.rst">virtualenv's changelog</a>.</em></p>
<blockquote>
<h2>v20.13.4 (2022-03-18)</h2>
<p>Bugfixes - 20.13.4</p>
<pre><code>- Improve performance of python startup inside created virtualenvs - by :user:`asottile`. (`[#2317](pypa/virtualenv#2317) &lt;https://github.com/pypa/virtualenv/issues/2317&gt;`_)
- Upgrade embedded setuptools to ``60.10.0`` from ``60.9.3`` - by :user:`gaborbernat`. (`[#2320](pypa/virtualenv#2320) &lt;https://github.com/pypa/virtualenv/issues/2320&gt;`_)
</code></pre>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a href="https://github.com/pypa/virtualenv/commit/775c1893e555139a9fbe7429c04062f4904afbe8"><code>775c189</code></a> release 20.13.4</li>
<li><a href="https://github.com/pypa/virtualenv/commit/63c94afc2487013b49cd816ccbde854bda85688d"><code>63c94af</code></a> Bump setuptools (<a href="https://github-redirect.dependabot.com/pypa/virtualenv/issues/2320">#2320</a>)</li>
<li><a href="https://github.com/pypa/virtualenv/commit/a16f609ec9ff6797d7694591151371c51698d911"><code>a16f609</code></a> fix 20ms startup penalty added by virtualenv (<a href="https://github-redirect.dependabot.com/pypa/virtualenv/issues/2317">#2317</a>)</li>
<li><a href="https://github.com/pypa/virtualenv/commit/d865c3e2940f287b065a4071cac59f543a2b23be"><code>d865c3e</code></a> [pre-commit.ci] pre-commit autoupdate (<a href="https://github-redirect.dependabot.com/pypa/virtualenv/issues/2318">#2318</a>)</li>
<li><a href="https://github.com/pypa/virtualenv/commit/4d1176c2602f880c64b488a0e7cc24db89da2424"><code>4d1176c</code></a> Merge pull request <a href="https://github-redirect.dependabot.com/pypa/virtualenv/issues/2312">#2312</a> from pypa/release-20.13.3</li>
<li>See full diff in <a href="https://github.com/pypa/virtualenv/compare/20.13.3...20.13.4">compare view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=virtualenv&package-manager=pip&previous-version=20.13.3&new-version=20.13.4)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting ``@dependabot` rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- ``@dependabot` rebase` will rebase this PR
- ``@dependabot` recreate` will recreate this PR, overwriting any edits that have been made to it
- ``@dependabot` merge` will merge this PR after your CI passes on it
- ``@dependabot` squash and merge` will squash and merge this PR after your CI passes on it
- ``@dependabot` cancel merge` will cancel a previously requested merge and block automerging
- ``@dependabot` reopen` will reopen this PR if it is closed
- ``@dependabot` close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
- ``@dependabot` ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- ``@dependabot` ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- ``@dependabot` ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)


</details>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
mergify bot pushed a commit to andrewbolster/bolster that referenced this pull request Mar 23, 2022
Bumps [virtualenv](https://github.com/pypa/virtualenv) from 20.13.3 to 20.13.4.
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a href="https://github.com/pypa/virtualenv/blob/20.13.4/docs/changelog.rst">virtualenv's changelog</a>.</em></p>
<blockquote>
<h2>v20.13.4 (2022-03-18)</h2>
<p>Bugfixes - 20.13.4</p>
<pre><code>- Improve performance of python startup inside created virtualenvs - by :user:`asottile`. (`[#2317](pypa/virtualenv#2317) &lt;https://github.com/pypa/virtualenv/issues/2317&gt;`_)
- Upgrade embedded setuptools to ``60.10.0`` from ``60.9.3`` - by :user:`gaborbernat`. (`[#2320](pypa/virtualenv#2320) &lt;https://github.com/pypa/virtualenv/issues/2320&gt;`_)
</code></pre>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a href="https://github.com/pypa/virtualenv/commit/775c1893e555139a9fbe7429c04062f4904afbe8"><code>775c189</code></a> release 20.13.4</li>
<li><a href="https://github.com/pypa/virtualenv/commit/63c94afc2487013b49cd816ccbde854bda85688d"><code>63c94af</code></a> Bump setuptools (<a href="https://github-redirect.dependabot.com/pypa/virtualenv/issues/2320">#2320</a>)</li>
<li><a href="https://github.com/pypa/virtualenv/commit/a16f609ec9ff6797d7694591151371c51698d911"><code>a16f609</code></a> fix 20ms startup penalty added by virtualenv (<a href="https://github-redirect.dependabot.com/pypa/virtualenv/issues/2317">#2317</a>)</li>
<li><a href="https://github.com/pypa/virtualenv/commit/d865c3e2940f287b065a4071cac59f543a2b23be"><code>d865c3e</code></a> [pre-commit.ci] pre-commit autoupdate (<a href="https://github-redirect.dependabot.com/pypa/virtualenv/issues/2318">#2318</a>)</li>
<li><a href="https://github.com/pypa/virtualenv/commit/4d1176c2602f880c64b488a0e7cc24db89da2424"><code>4d1176c</code></a> Merge pull request <a href="https://github-redirect.dependabot.com/pypa/virtualenv/issues/2312">#2312</a> from pypa/release-20.13.3</li>
<li>See full diff in <a href="https://github.com/pypa/virtualenv/compare/20.13.3...20.13.4">compare view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=virtualenv&package-manager=pip&previous-version=20.13.3&new-version=20.13.4)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)


</details>
mergify bot pushed a commit to andrewbolster/bolster that referenced this pull request Mar 25, 2022
Bumps [virtualenv](https://github.com/pypa/virtualenv) from 20.13.4 to 20.14.0.
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a href="https://github.com/pypa/virtualenv/blob/20.14.0/docs/changelog.rst">virtualenv's changelog</a>.</em></p>
<blockquote>
<h2>v20.14.0 (2022-03-25)</h2>
<p>Features - 20.14.0</p>
<pre><code>- Support Nushell activation scripts with nu version ``0.60`` - by :user:`kubouch`. (`[#2321](pypa/virtualenv#2321) &lt;https://github.com/pypa/virtualenv/issues/2321&gt;`_)
<p>Bugfixes - 20.14.0
</code></pre></p>
<ul>
<li>Improve performance of python startup inside created virtualenvs - by :user:<code>asottile</code>. (<code>[#2317](pypa/virtualenv#2317) &lt;https://github.com/pypa/virtualenv/issues/2317&gt;</code>_)</li>
<li>Upgrade embedded setuptools to <code>60.10.0</code> from <code>60.9.3</code> - by :user:<code>gaborbernat</code>. (<code>[#2320](pypa/virtualenv#2320) &lt;https://github.com/pypa/virtualenv/issues/2320&gt;</code>_)</li>
<li>Upgrade embedded setuptools to <code>61.0.0</code> from <code>60.10.0</code> - by :user:<code>gaborbernat</code>. (<code>[#2322](pypa/virtualenv#2322) &lt;https://github.com/pypa/virtualenv/issues/2322&gt;</code>_)</li>
</ul>
<h2>v20.13.3 (2022-03-07)</h2>
<p>Bugfixes - 20.13.3</p>
<pre><code>- Avoid symlinking the contents of ``/usr`` into PyPy3.8+ virtualenvs - by :user:`stefanor`. (`[#2310](pypa/virtualenv#2310) &lt;https://github.com/pypa/virtualenv/issues/2310&gt;`_)
- Bump embed pip from ``22.0.3`` to ``22.0.4`` - by :user:`gaborbernat`. (`[#2311](pypa/virtualenv#2311) &lt;https://github.com/pypa/virtualenv/issues/2311&gt;`_)
<h2>v20.13.2 (2022-02-24)</h2>
<p>Bugfixes - 20.13.2
</code></pre></p>
<ul>
<li>Upgrade embedded setuptools to <code>60.9.3</code> from <code>60.6.0</code> - by :user:<code>gaborbernat</code>. (<code>[#2306](pypa/virtualenv#2306) &lt;https://github.com/pypa/virtualenv/issues/2306&gt;</code>_)</li>
</ul>
<h2>v20.13.1 (2022-02-05)</h2>
<p>Bugfixes - 20.13.1</p>
<pre><code>- fix &quot;execv() arg 2 must contain only strings&quot; error on M1 MacOS (`[#2282](pypa/virtualenv#2282) &lt;https://github.com/pypa/virtualenv/issues/2282&gt;`_)
- Ugrade embedded setuptools to ``60.5.0`` from ``60.2.0`` - by :user:`asottile`. (`[#2289](pypa/virtualenv#2289) &lt;https://github.com/pypa/virtualenv/issues/2289&gt;`_)
- Upgrade embedded pip to ``22.0.3`` and setuptools to ``60.6.0`` - by :user:`gaborbernat` and :user:`asottile`. (`[#2294](pypa/virtualenv#2294) &lt;https://github.com/pypa/virtualenv/issues/2294&gt;`_)
<h2>v20.13.0 (2022-01-02)</h2>
<p>Features - 20.13.0
</code></pre></p>
<ul>
<li>Add downloaded wheel information in the relevant JSON embed file to
prevent additional downloads of the same wheel. - by :user:<code>mayeut</code>. (<code>[#2268](pypa/virtualenv#2268) &lt;https://github.com/pypa/virtualenv/issues/2268&gt;</code>_)</li>
</ul>
<p>Bugfixes - 20.13.0</p>

</blockquote>
<p>... (truncated)</p>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a href="https://github.com/pypa/virtualenv/commit/a731637cbaf2da326a8ce5c857e2e873ef1120ce"><code>a731637</code></a> release 20.14.0</li>
<li><a href="https://github.com/pypa/virtualenv/commit/04296970d44c5399ed84cd6008901bd1be96b20d"><code>0429697</code></a> Bump embeded setuptools to v61 (<a href="https://github-redirect.dependabot.com/pypa/virtualenv/issues/2322">#2322</a>)</li>
<li><a href="https://github.com/pypa/virtualenv/commit/ee018a3b2d6684f6965e5ddbf14ed7477627160e"><code>ee018a3</code></a> Update Nushell activation scripts to 0.60 (<a href="https://github-redirect.dependabot.com/pypa/virtualenv/issues/2321">#2321</a>)</li>
<li>See full diff in <a href="https://github.com/pypa/virtualenv/compare/20.13.4...20.14.0">compare view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=virtualenv&package-manager=pip&previous-version=20.13.4&new-version=20.14.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)


</details>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants