Skip to content

Commit

Permalink
refactor: use Task.async_stream in parallel compiler (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
polvalente committed Dec 14, 2020
1 parent edf9877 commit b8bf33f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/fika/compiler/type_checker/parallel_type_checker.ex
Expand Up @@ -52,11 +52,21 @@ defmodule Fika.Compiler.TypeChecker.ParallelTypeChecker do
def handle_continue(:start, state) do
pid = self()

Enum.each(state.unchecked_functions, fn {signature, function} ->
Task.start_link(fn ->
state.unchecked_functions
|> Task.async_stream(
fn {signature, function} ->
result = TypeChecker.check(function, %{type_checker_pid: pid})
__MODULE__.post_result(pid, signature, result)
end)
end,
max_concurrency: Enum.count(state.unchecked_functions)
)
|> Enum.each(fn
{:ok, _} ->
:ok

error ->
Logger.error("Error while running ParallelTypeChecker: #{inspect(error)}")
raise CompileError, description: "failed to run ParallelTypeChecker"
end)

{:noreply, state}
Expand Down
27 changes: 27 additions & 0 deletions test/fika/compiler/module_compiler_test.exs
Expand Up @@ -24,6 +24,33 @@ defmodule Fika.Compiler.ModuleCompilerTest do
File.rm!(temp_file)
end

test "can compile a file with many functions" do
tmp_dir = System.tmp_dir!()
temp_file = Path.join(tmp_dir, "foo.fi")

# Show that we're not limited to the number of online schedulers
n = System.schedulers_online() * 2

str =
1..n
|> Enum.map(fn number ->
"""
fn foo_#{number} : String do
"Hello world"
end
"""
end)
|> Enum.join("\n")

File.write!(temp_file, str)

File.cd!(tmp_dir, fn ->
assert {:ok, "foo", "foo.fi", _binary} = ModuleCompiler.compile("foo")
end)

File.rm!(temp_file)
end

test "returns error when file doesn't exist" do
module = Path.join(System.tmp_dir!(), "foo") |> String.to_atom()

Expand Down

0 comments on commit b8bf33f

Please sign in to comment.