cat_ethos
09/01/2025, 5:26 PMbaml-sam
09/01/2025, 5:28 PMbaml-sam
09/01/2025, 5:29 PMbaml-sam
09/01/2025, 5:30 PMcat_ethos
09/01/2025, 5:36 PMcat_ethos
09/01/2025, 5:37 PMcat_ethos
09/01/2025, 5:38 PMcat_ethos
09/01/2025, 5:39 PMbaml-sam
09/01/2025, 5:42 PMbaml-sam
09/01/2025, 5:46 PMcall_function_sync would need to become tokio::spawn(call_function.await) under the hood, and then it would need to pass the result to Elixirbaml-sam
09/01/2025, 5:47 PMemil
09/01/2025, 5:55 PMcat_ethos
09/01/2025, 6:22 PMemil
09/01/2025, 7:02 PMtest "concurrent calls" do
string = """
Twinkle, twinkle, little star,
How I wonder what you are!
Up above the world so high,
Like a diamond in the sky.
Twinkle, twinkle, little star,
How I wonder what you are!
When the blazing sun is gone,
When he nothing shines upon,
Then you show your little light,
Twinkle, twinkle, all the night.
Twinkle, twinkle, little star,
How I wonder what you are!
"""
# Time single call
{single_time, {:ok, _}} =
:timer.tc(fn ->
BamlElixirTest.Echo.call(%{message: "1 #{DateTime.utc_now()} #{string}"})
end)
# Time parallel calls
{parallel_time, _} =
:timer.tc(fn ->
jobs =
1..5
|> Enum.map(fn _ ->
Task.async(fn ->
BamlElixirTest.Echo.call(%{message: "2 #{DateTime.utc_now()} #{string}"})
end)
end)
Task.await_many(jobs, :infinity)
end)
# Time parallel calls 2x
{parallel_time_2x, _} =
:timer.tc(fn ->
jobs =
1..10
|> Enum.map(fn _ ->
Task.async(fn ->
BamlElixirTest.Echo.call(%{message: "3 #{DateTime.utc_now()} #{string}"})
end)
end)
Task.await_many(jobs, :infinity)
end)
IO.inspect(single_time / 1_000_000, label: "single_time")
# => 2.565963
IO.inspect(parallel_time / 1_000_000, label: "parallel_time")
# => 3.510661
IO.inspect(parallel_time_2x / 1_000_000, label: "parallel_time_2x")
# => 3.085659
endbaml-sam
09/02/2025, 9:41 PMcall_function_sync will block the caller until it returns, so if the above works then Task.async is doing something to force the Elixir caller to yield the current execution context somehowbaml-sam
09/02/2025, 9:42 PMtokio::spawn strategy I described above is how we do it in each FFI's bridge, to be able to hand off from Rust's async to the host language's async handlingemil
09/06/2025, 8:30 AMbaml-sam
09/06/2025, 6:12 PMcat_ethos
09/09/2025, 5:55 PMemil
09/11/2025, 8:06 AM