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

Faster import part2: Delay import of heavy packages #6106

Merged
merged 10 commits into from
Sep 5, 2023

Conversation

danielhollas
Copy link
Collaborator

@danielhollas danielhollas commented Sep 1, 2023

Delay import of disk_objectstore, jsonschema, requests, plumpy and paramiko. Obviously, many of these will be eventually imported for many tasks, but right now they are imported indiscriminately for everything, making everything slower, see benchmarks below.

There are other avenues for improvement, but I wanted to get the most impactful stuff first.

Timings against the main branch

import aiida

Benchmark 1: PYTHONPATH=/home/bl22441/atmospec/aiida-core python -c 'import aiida'
  Time (mean ± σ):     102.4 ms ±  10.5 ms    [User: 89.6 ms, System: 12.4 ms]
  Range (minmax):    85.6 ms127.2 ms    23 runs
 
Benchmark 2: python -c 'import aiida'
  Time (mean ± σ):     263.4 ms ±  16.1 ms    [User: 236.3 ms, System: 26.6 ms]
  Range (minmax):   240.9 ms298.2 ms    11 runs
 
Summary
  'PYTHONPATH=/home/bl22441/atmospec/aiida-core python -c 'import aiida'' ran
    2.57 ± 0.31 times faster than 'python -c 'import aiida''

import aiida.orm

Benchmark 1: PYTHONPATH=/home/bl22441/atmospec/aiida-core python -c 'import aiida.orm'
  Time (mean ± σ):     333.8 ms ±  40.1 ms    [User: 960.0 ms, System: 2257.9 ms]
  Range (minmax):   302.6 ms410.4 ms    10 runs
 
Benchmark 2: python -c 'import aiida.orm'
  Time (mean ± σ):     586.0 ms ±  24.1 ms    [User: 1246.8 ms, System: 2339.1 ms]
  Range (minmax):   560.4 ms620.8 ms    10 runs
 
Summary
  'PYTHONPATH=/home/bl22441/atmospec/aiida-core python -c 'import aiida.orm'' ran
    1.76 ± 0.22 times faster than 'python -c 'import aiida.orm''

import aiida.cmdline.commands

Benchmark 1: PYTHONPATH=/home/bl22441/atmospec/aiida-core python -c 'import aiida.cmdline.commands'
  Time (mean ± σ):     280.0 ms ±  14.6 ms    [User: 256.8 ms, System: 22.6 ms]
  Range (minmax):   248.1 ms300.0 ms    12 runs
 
Benchmark 2: python -c 'import aiida.cmdline.commands'
  Time (mean ± σ):     346.3 ms ±  32.9 ms    [User: 315.1 ms, System: 30.6 ms]
  Range (minmax):   318.2 ms421.6 ms    10 runs
 
Summary
  'PYTHONPATH=/home/bl22441/atmospec/aiida-core python -c 'import aiida.cmdline.commands'' ran
    1.24 ± 0.13 times faster than 'python -c 'import aiida.cmdline.commands''

Copy link
Collaborator Author

@danielhollas danielhollas left a comment

Choose a reason for hiding this comment

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

@sphuber this is ready for review

@@ -9,11 +9,12 @@
###########################################################################
# pylint: disable=cyclic-import
"""AiiDA manager for global settings"""
import asyncio
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Note: About half of the cost of importing plumpy (30ms) comes from importing asyncio

@@ -9,8 +9,6 @@

LOGGER = logging.getLogger(__name__)

__all__ = ('ProcessLauncher',)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

NOTE: This is a breaking API change in principle, but since this only seems to be used in aiida/manage/manager.py I think it is okay. This is AFAIK the only way how to avoid importing plumpy upon every import, since the ProcessLauncher uses plumpy.ProcessLauncher as a parent class.

@danielhollas danielhollas marked this pull request as ready for review September 3, 2023 19:54
@sphuber
Copy link
Contributor

sphuber commented Sep 4, 2023

Thanks @danielhollas . The changes look good to me. You are right that the removal of the export of ProcessLauncher to higher modules is technically breaking, but I agree that it is unlikely that it affects anyone, so I think this is an acceptable change. Are these the last of the changes related to the import speed? Or do you have more lined up?

@danielhollas
Copy link
Collaborator Author

Thanks @sphuber ! I have more lined up, specifically targeting verdi.

Copy link
Contributor

@sphuber sphuber left a comment

Choose a reason for hiding this comment

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

Thanks @danielhollas

@sphuber sphuber merged commit 8e6e08d into aiidateam:main Sep 5, 2023
10 checks passed
@danielhollas danielhollas deleted the faster-import-part2 branch September 5, 2023 10:20
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.

2 participants