title: How does ray deal with variables of enclosing scope?
body: <p>Consider the following example:</p>
  <pre><code>import numpy as np
  import ray
  import time
  
  A = np.array([42] * 4200)   
  
  @ray.remote
  def foo1(x):
      x[5]**2
  
  @ray.remote
  def foo2(j):
      A[j]**2
  
  ray.init()
  
  #
  # some warmup for ray
  #
  
  start = time.perf_counter()
  for _ in range(1000):
      ray.get(foo1.remote(A))
  time_foo1 = time.perf_counter() - start
  
  start = time.perf_counter()
  for _ in range(1000):
      ray.get(foo2.remote(5))
  time_foo2 = time.perf_counter() - start
  
  print(time_foo1, time_foo2)
  
  ray.shutdown()
  </code></pre>
  <p>It appears that <code>time_foo2</code> is significantly smaller than <code>time_foo1</code>. My naive guess would be that ray is serializing <code>A</code> each time <code>foo1</code> is called. However, even if I manually put <code>A</code> in the object storage and pass the object reference to <code>foo1</code>, I don't see any improvement. Could somebody enlighten me what is going on here behind the scenes?</p>