Repository: dask/dask
File: dask/order.py (Python)

Code before:
                            ):
                                for k in path:
                                    add_to_result(k)
                            else:
                                known_runnable_paths[current] = [path]
                        break

    # Pick strategy
    # Note: We're trying to be smart here by picking a strategy on how to
    # determine the critical path. This is not always clear and we may want to
    # consider just calculating both orderings and picking the one with less
    # pressure. The only concern to this would be performance but at time of
    # writing, the most expensive part of ordering is the prep work (mostly
    # connected roots + sort_key) which can be reused for multiple orderings.

    def get_target(longest_path: bool = False) -> Key:
        # Some topologies benefit if the node with the most dependencies
        # is used as first choice, others benefit from the opposite.
        candidates = leaf_nodes
        skey: Callable = sort_key

        preferred_candidates = set()
        if runnable_hull:
            skey = lambda k: (num_needed[k], sort_key(k))
            preferred_candidates = runnable_hull & candidates
        if not preferred_candidates and reachable_hull:
            skey = lambda k: (num_needed[k], sort_key(k))
            preferred_candidates = reachable_hull & candidates

        if reachable_hull or runnable_hull:
            # We can't reach a leaf node directly, but we still have nodes
            # with results in memory, these notes can inform our path towards
            # a new preferred leaf node.
            if not preferred_candidates:
                for r in processed_roots:
                    leafs_connected_to_loaded_roots.update(leafs_connected[r])
                processed_roots.clear()

                leafs_connected_to_loaded_roots.intersection_update(candidates)
                preferred_candidates = leafs_connected_to_loaded_roots
            candidates = preferred_candidates
        else:
            # We reach a new and independent branch, so throw away previous branch
            leafs_connected_to_loaded_roots.clear()

        if longest_path and not runnable_hull and not reachable_hull:
            return leaf_nodes_sorted.pop()
        else:
            # FIXME: This can be very expensive
            return min(candidates, key=skey)


Review comment (suggestion):
with https://github.com/dask/dask/pull/11305 `runnable_hull & candidates` will always be empty, i.e. this clause can go away and the next if can be simplified

```suggestion
        if reachable_hull:
```

Code after:
                        else:
                            if (
                                len(dependencies[current]) > 1  # type: ignore
                                and num_needed[current] <= 1
                            ):
                                for k in path:
                                    add_to_result(k)
                            else:
                                known_runnable_paths[current] = [path]
                                runnable_hull.discard(current)
                        break

    # Pick strategy
    # Note: We're trying to be smart here by picking a strategy on how to
    # determine the critical path. This is not always clear and we may want to
    # consider just calculating both orderings and picking the one with less
    # pressure. The only concern to this would be performance but at time of
    # writing, the most expensive part of ordering is the prep work (mostly
    # connected roots + sort_key) which can be reused for multiple orderings.

    def get_target(longest_path: bool = False) -> Key:
        # Some topologies benefit if the node with the most dependencies
        # is used as first choice, others benefit from the opposite.
        candidates = leaf_nodes
        skey: Callable = sort_key
        if reachable_hull:
            skey = lambda k: (num_needed[k], sort_key(k))

        all_leafs_accessible = len(leafs_connected_to_loaded_roots) >= len(candidates)
        if reachable_hull and not all_leafs_accessible:
            # Avoid this branch if we can already access all leafs. Just pick
            # one of them without the expensive selection process in this case.

            candidates = reachable_hull & candidates
            if not candidates:
                # We can't reach a leaf node directly, but we still have nodes
                # with results in memory, these notes can inform our path towards
                # a new preferred leaf node.
                for r in processed_roots:
                    leafs_connected_to_loaded_roots.update(leafs_connected[r])
                processed_roots.clear()

                leafs_connected_to_loaded_roots.intersection_update(leaf_nodes)
                candidates = leafs_connected_to_loaded_roots
            else:
                leafs_connected_to_loaded_roots.update(candidates)

        elif not reachable_hull:
            # We reach a new and independent branch, so throw away previous branch
            leafs_connected_to_loaded_roots.clear()