john-rodriguez-mgni commented on code in PR #61483:
URL: https://github.com/apache/airflow/pull/61483#discussion_r2770081933


##########
airflow-core/src/airflow/api_fastapi/core_api/routes/ui/dags.py:
##########
@@ -234,18 +234,22 @@ def get_dags(
             pending_actions_by_dag_id[dag_id].append(hitl_detail)
 
     # aggregate rows by dag_id
-    dag_runs_by_dag_id: dict[str, DAGWithLatestDagRunsResponse] = {
-        dag.dag_id: DAGWithLatestDagRunsResponse.model_validate(
-            {
-                **DAGResponse.model_validate(dag).model_dump(),
-                "asset_expression": dag.asset_expression,
-                "latest_dag_runs": [],
-                "pending_actions": pending_actions_by_dag_id[dag.dag_id],
-                "is_favorite": dag.dag_id in favorite_dag_ids,
-            }
+    # Performance optimization: Validate ORM to DAGResponse once per DAG, then 
use model_construct
+    # to build DAGWithLatestDagRunsResponse without redundant validation.
+    # The original pattern (model_validate -> model_dump -> model_validate) 
caused triple
+    # serialization overhead that scaled poorly with the number of DAGs.
+    dag_runs_by_dag_id: dict[str, DAGWithLatestDagRunsResponse] = {}
+    for dag in dags:
+        dag_response = DAGResponse.model_validate(dag)
+        dag_data = dag_response.model_dump()
+        dag_data["asset_expression"] = dag.asset_expression
+        dag_data["latest_dag_runs"] = []
+        dag_data["pending_actions"] = pending_actions_by_dag_id[dag.dag_id]
+        dag_data["is_favorite"] = dag.dag_id in favorite_dag_ids
+        dag_runs_by_dag_id[dag.dag_id] = 
DAGWithLatestDagRunsResponse.model_construct(

Review Comment:
   I am sorry I accidentally included the wrong dags.py in the PR because I had 
my patch which I applied to our custom Airflow image for testing in a different 
directory.  I have updated the PR with the same dags.py that is in my Airflow 
installation and I can confirm that I have DAGs with tags and don't see the 
issue.
   ```
   airflow@ip-10-128-236-181:/opt/airflow$ python3 -c "
   from sqlalchemy import text
   from airflow.settings import Session
   with Session() as session:
       result = session.execute(text('SELECT COUNT(DISTINCT dag_id) FROM 
dag_tag'))
       print(f'DAGs with tags: {result.scalar()}')
   "
   DAGs with tags: 26
   ```
   



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