Repository: apache/airflow
File: airflow-core/src/airflow/api_fastapi/core_api/routes/ui/grid.py (Python)

Code before:
            SerializedDagModel.dag_version_id.in_(
                select(TaskInstance.dag_version_id)
                .join(TaskInstance.dag_run)
                .where(
                    DagRun.id.in_(run_ids),
                )
                .distinct()
            ),
        )
        .execution_options(yield_per=5)
    )

    for serdag in session.scalars(serdags_query):
        if serdag:
            filtered_dag = serdag.dag
            # Apply the same filtering to historical DAG versions
            if root:
                filtered_dag = filtered_dag.partial_subset(
                    task_ids=root,
                    include_upstream=include_upstream,
                    include_downstream=include_downstream,
                    depth=depth,
                )
            # Merge immediately instead of collecting all DAGs in memory
            nodes = [task_group_to_dict_grid(x) for x in task_group_sort(filtered_dag.task_group)]
            _merge_node_dicts(merged_nodes, nodes)

            session.expunge(serdag)  # to allow garbage collection

    return [GridNodeResponse(**n) for n in merged_nodes]


@grid_router.get(
    "/runs/{dag_id}",
    responses=create_openapi_http_exception_doc(
        [
            status.HTTP_400_BAD_REQUEST,
            status.HTTP_404_NOT_FOUND,
        ]
    ),
    dependencies=[
        Depends(
            requires_access_dag(
                method="GET",
                access_entity=DagAccessEntity.TASK_INSTANCE,
            )
        ),
        Depends(
            requires_access_dag(
                method="GET",
                access_entity=DagAccessEntity.RUN,

Review comment (suggestion):
The variable `nodes` is created but not reused after merging. Consider inlining the list comprehension directly into the `_merge_node_dicts` call to avoid creating an intermediate list variable: `_merge_node_dicts(merged_nodes, [task_group_to_dict_grid(x) for x in task_group_sort(filtered_dag.task_group)])`

Code after:
            SerializedDagModel.dag_version_id.in_(
                select(TaskInstance.dag_version_id)
                .join(TaskInstance.dag_run)
                .where(
                    DagRun.id.in_(run_ids),
                )
                .distinct()
            ),
        )
        .execution_options(yield_per=5)  # balance between peak memory usage and round trips
    )

    for serdag in session.scalars(serdags_query):
        filtered_dag = serdag.dag
        # Apply the same filtering to historical DAG versions
        if root:
            filtered_dag = filtered_dag.partial_subset(
                task_ids=root,
                include_upstream=include_upstream,
                include_downstream=include_downstream,
                depth=depth,
            )
        # Merge immediately instead of collecting all DAGs in memory
        nodes = [task_group_to_dict_grid(x) for x in task_group_sort(filtered_dag.task_group)]
        _merge_node_dicts(merged_nodes, nodes)

        session.expunge(serdag)  # to allow garbage collection

    return [GridNodeResponse(**n) for n in merged_nodes]


@grid_router.get(
    "/runs/{dag_id}",
    responses=create_openapi_http_exception_doc(
        [
            status.HTTP_400_BAD_REQUEST,
            status.HTTP_404_NOT_FOUND,
        ]
    ),
    dependencies=[
        Depends(
            requires_access_dag(
                method="GET",
                access_entity=DagAccessEntity.TASK_INSTANCE,
            )
        ),
        Depends(
            requires_access_dag(
                method="GET",
                access_entity=DagAccessEntity.RUN,
            )