code: def test_apply_recursive_caching(shared_ray_instance):
      @ray.remote
      def f(input):
          return input
  
      input = InputNode()
      f_node = f.bind(input)
  
      a, b = input, f_node
      for _ in range(10):
          a, b = f.bind(a, b), f.bind(a, b)
  
      counter = 0
      original_apply_recursive = DAGNode.apply_recursive
  
      def _apply_recursive_with_counter(self, fn):
          nonlocal counter
          counter += 1
          return original_apply_recursive(self, fn)
  
      DAGNode.apply_recursive = _apply_recursive_with_counter
  
      a.apply_recursive(lambda node: node)
  
      # Prior to #40337; count was 2559
      assert counter == 40
      DAGNode.apply_recursive = original_apply_recursive
  
file_path: ray/python/ray/dag/tests/test_input_node.py
imports: import pytest
  from ray.dag.dag_node import DAGNode
  from ray.dag.input_node import InputNode
  from typing import Any, TypeVar
  import ray
label_desc: Use image node_id 12 for calling a global function with example usage: test_apply_recursive_caching(shared_ray_instance) and returns: input, original_apply_recursive