Skip to content

Commit

Permalink
Optimize lf.concurrent_execute.
Browse files Browse the repository at this point in the history
When `executor` is empty and `max_worker` is 1, we could avoid the overhead of creating a short-lived thread pool.

PiperOrigin-RevId: 615501183
  • Loading branch information
daiyip authored and langfun authors committed Mar 13, 2024
1 parent 48de120 commit 32b354e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions langfun/core/concurrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ def concurrent_execute(
exponential_backoff=exponential_backoff,
)

# NOTE(daiyip): when executor is not specified and max_worker is 1,
# we don't need to create a executor pool. Instead, the inputs will be
# processed by the user function in sequence within the current thread.
if executor is None and max_workers == 1:
return [func(i) for i in parallel_inputs]

shutdown_after_finish = executor is None
executor = _executor_pool.executor_from(executor, max_workers=max_workers)

Expand Down
10 changes: 10 additions & 0 deletions langfun/core/concurrent_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ def fun(a):
with component.context(y=2):
self.assertEqual(concurrent.concurrent_execute(fun, [A(1), A(2)]), [2, 4])

def test_concurrent_execute_with_a_single_worker(self):
def fun(a):
return a.x * a.y

with component.context(y=2):
self.assertEqual(
concurrent.concurrent_execute(fun, [A(1), A(2)], max_workers=1),
[2, 4],
)

def test_concurrent_execute_with_external_executor(self):
def fun(a):
return a.x * a.y
Expand Down

0 comments on commit 32b354e

Please sign in to comment.