summeriver13 opened a new pull request, #74646:
URL: https://github.com/apache/airflow/pull/74646

   ### Summary
   
   `SerializedDAG.partial_subset()` is dominated by the loop that drops the 
upstream/downstream
   references of anything that did not make the cut, and that loop is quadratic 
in the size of the DAG.
   
   ### Why
   
   `set.intersection_update()` walks its argument unless it is given a set, and 
the loop passed the dicts
   held by the DAG, so every task and TaskGroup walked the entire DAG once. The 
loop that collects
   downstream relatives had the same problem in a different shape: it scanned 
the list of matched tasks
   (`rel not in matched_tasks`) for every relative task. For a 3,794 task DAG 
(271 TaskGroups of 14 tasks,
   one matched task per group):
   
   ```
   8,676 intersection_update calls taking 0.42s of a 0.72s call
   ```
   
   ### How
   
   Build the two id sets once and pass those, and look matched tasks up by id. 
Intersecting against a set
   gives the same result as intersecting against the dict it was built from, so 
no behaviour changes.
   
   | Tasks | Before | After |
   | --- | --- | --- |
   | 1,134 | 0.103s | 0.034s |
   | 3,794 | 0.644s | 0.199s |
   | 7,588 | 3.306s | 0.332s |
   
   This is the path taken by the structure view when a node is selected, by the 
grid view and by the CLI.
   `task-sdk`'s `DAG.partial_subset` has the same loop; I left it alone to keep 
this to the implementation
   the API and the CLI use.
   
   ### Tests
   
   `pytest airflow-core/tests/unit/serialization/test_dag_serialization.py -q` 
→ 256 passed, identical
   before and after. The added test pins the pruning behaviour with a subset 
that excludes a task and a
   TaskGroup; the numbers above are the evidence for the performance claim, as 
nothing changes functionally.
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [X] Yes (please specify the tool below)
   
   <!--
   Generated-by: pi coding agent
   -->
   
   ---
   
   * Read the **[Pull Request 
Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)**
 for more information. Note: commit author/co-author name and email in commits 
become permanently public when merged.
   * For fundamental code changes, an Airflow Improvement Proposal 
([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals))
 is needed.
   * When adding dependency, check compliance with the [ASF 3rd Party License 
Policy](https://www.apache.org/legal/resolved.html#category-x).
   * For significant user-facing changes create newsfragment: 
`{pr_number}.significant.rst`, in 
[airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
 You can add this file in a follow-up commit after the PR is created so you 
know the PR number.
   


-- 
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