o-nikolas opened a new pull request, #30361:
URL: https://github.com/apache/airflow/pull/30361

   ### Overview
   This PR aims to improve the time it takes to load/import the various 
executor modules we have in Airflow.
   ### Motivation
   The executors are imported in more places now that various compatibility 
checks are in core Airflow code (re: AIP-51). Also, decreasing import times is 
very important for the work of executors vending CLI commands (see #29055), 
since the CLI code in Airflow is particularly sensitive to slow imports 
(because all code is loaded fresh each time you run an individual Airflow CLI 
command).
   ### The changes
   This PR mostly includes changes to move some expensive imports that are only 
used for type checking under the `TYPE_CHECKING` flag so that they are not run 
at runtime. The most important changes are in the `BaseExecutor` module since 
all other executors load this module, and so benefits made here propagate 
outward.
   ### Testing
   I benchmarked these changes by writing a script to import the various 
executor modules in a fresh python runtime and timing how long that takes (you 
can test this yourself quickly from a bash shell by doing something like `time 
python -c 'from airflow.executors.local_executor import LocalExecutor'`). Then 
doing that in a loop for several samples (with some randomness in the order for 
fairness) both on main and on my development branch. 
   ### Results
   ![Screenshot from 2023-03-29 
12-39-25](https://user-images.githubusercontent.com/65743084/228649458-06db2caf-38dd-411b-9760-63c0cff40e12.png)
   
   Most executors saw a ~50% speed increase. Kubernetes, and to a lesser extent 
Celery, are still quite slow and will need more changes specifically targeted 
to those modules (in a future PR).
   The combined executors (e.g. LocalKubernetesExecutor) saw less gains since 
they import two executors each, so they're paying double the cost (so they saw 
half the gains, 25%)   
   
   <!--
   Thank you for contributing! Please make sure that your code changes
   are covered with tests. And in case of new features or big changes
   remember to adjust the documentation.
   
   Feel free to ping committers for the review!
   
   In case of an existing issue, reference it using one of the following:
   
   closes: #ISSUE
   related: #ISSUE
   
   How to write a good git commit message:
   http://chris.beams.io/posts/git-commit/
   -->
   
   ---
   **^ Add meaningful description above**
   
   Read the **[Pull Request 
Guidelines](https://github.com/apache/airflow/blob/main/CONTRIBUTING.rst#pull-request-guidelines)**
 for more information.
   In case of fundamental code changes, an Airflow Improvement Proposal 
([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals))
 is needed.
   In case of a new dependency, check compliance with the [ASF 3rd Party 
License Policy](https://www.apache.org/legal/resolved.html#category-x).
   In case of backwards incompatible changes please leave a note in a 
newsfragment file, named `{pr_number}.significant.rst` or 
`{issue_number}.significant.rst`, in 
[newsfragments](https://github.com/apache/airflow/tree/main/newsfragments).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to