Anti-pattern: Passing the same large argument by value repeatedly harms performance#

Avoid passing the same large argument by value to multiple tasks. Use ray.put() and pass by reference instead.

When you pass an argument larger than 100 KB by value to a task, Ray implicitly stores the argument in the object store. Before the task runs, the worker process fetches the argument from the caller’s object store to the local object store. If you pass the same large argument to multiple tasks, Ray stores multiple copies of the argument in the object store, because Ray doesn’t deduplicate them.

Instead of passing the large argument by value to multiple tasks, call ray.put() once to store the argument in the object store and get an object ref. Then pass that object ref to the tasks. All tasks use the same copy of the argument, which is faster and uses less object store memory.

Code example#

Anti-pattern:

import ray
import numpy as np

ray.init()


@ray.remote
def func(large_arg, i):
    return len(large_arg) + i


large_arg = np.zeros(1024 * 1024)

# 10 copies of large_arg are stored in the object store.
outputs = ray.get([func.remote(large_arg, i) for i in range(10)])

Better approach:

# 1 copy of large_arg is stored in the object store.
large_arg_ref = ray.put(large_arg)
outputs = ray.get([func.remote(large_arg_ref, i) for i in range(10)])