elixir icon indicating copy to clipboard operation
elixir copied to clipboard

Cache body of defmodule when possible

Open josevalim opened this issue 3 years ago • 16 comments

In most recompilations, the body module did not change, so we can cache its .beam file and speed up compilation time.

josevalim avatar Dec 25 '22 16:12 josevalim

We cannot cache the defmodule bits but we can cache parsing. Not sure if beneficial.

josevalim avatar Dec 27 '22 14:12 josevalim

I have written a script that measures if this optimization can be beneficial. Please save the script below to the root of your Mix project (non-umbrella for now) as bench.exs and then run it as elixir bench.exs and post the results here with some specs about your machine and operating system:

files = Path.wildcard("lib/**/*.{ex,exs}")
contents = Enum.map(files, &File.read!/1)

IO.puts("Caching...")
File.rm_rf("tmp/cache")
File.mkdir_p!("tmp/cache")

caches =
  for file <- files do
    binary = file |> File.read!() |> Code.string_to_quoted!() |> :erlang.term_to_binary()
    out = Path.join("tmp/cache", file)
    File.mkdir_p!(Path.dirname(out))
    File.write!(out, binary)
    out
  end

IO.puts("Warming up...")
Enum.each(contents, &Code.string_to_quoted!/1)

IO.puts("Found #{length(files)} files")

{time, _} = :timer.tc(fn -> Enum.each(contents, &Code.string_to_quoted!/1) end)
IO.puts("Parsed in #{time}μs")

binaries = Enum.map(caches, &File.read!/1)

{time, _} = :timer.tc(fn -> Enum.each(binaries, &:erlang.binary_to_term/1) end)
IO.puts("Deserialized in #{time}μs")

{time, _} =
  :timer.tc(fn -> Enum.each(files, &(&1 |> File.read!() |> Code.string_to_quoted!())) end)

IO.puts("Read and parsed in #{time}μs")

{time, _} =
  :timer.tc(fn ->
    for file <- files do
      out = Path.join("tmp/cache", file)
      {:ok, binary} = File.read(out)
      :erlang.binary_to_term(binary)
    end
  end)

IO.puts("Cache hit plus deserialization in #{time}μs")

{time, _} =
  :timer.tc(fn ->
    for file <- files do
      out = Path.join("tmp/cachea", file)
      {:error, _} = File.read(out)
      file |> File.read!() |> Code.string_to_quoted!()
    end
  end)

IO.puts("Cache miss plus parsing in #{time}μs")

josevalim avatar Jan 09 '23 14:01 josevalim

Here are my results for Livebook, running on MacStudio M1 Pro:

Found 176 files
Parsed in 56863μs
Deserialized in 10757μs
Read and parsed in 69956μs
Cache hit plus deserialization in 29595μs
Cache miss plus parsing in 75123μs

josevalim avatar Jan 09 '23 14:01 josevalim

MacBook Pro 14-inch (M1 Max), 2021

Found 586 files
Parsed in 196870μs
Deserialized in 21731μs
Read and parsed in 150610μs
Cache hit plus deserialization in 90258μs
Cache miss plus parsing in 229501μs
Found 416 files
Parsed in 97152μs
Deserialized in 11109μs
Read and parsed in 122771μs
Cache hit plus deserialization in 45304μs
Cache miss plus parsing in 151885μs

neilberkman avatar Jan 09 '23 14:01 neilberkman

Some of my projects running in a Macbook Pro M1 Pro:

Caching...
Warming up...
Found 337 files
Parsed in 83330μs
Deserialized in 10619μs
Read and parsed in 275896μs
Cache hit plus deserialization in 101165μs
Cache miss plus parsing in 144946μs
Caching...
Warming up...
Found 143 files
Parsed in 7176μs
Deserialized in 1950μs
Read and parsed in 26621μs
Cache hit plus deserialization in 15607μs
Cache miss plus parsing in 35856μs

squiter avatar Jan 09 '23 14:01 squiter

Distributor ID:	Ubuntu
Description:	Pop!_OS 22.04 LTS
Release:	22.04
Codename:	jammy
CPU: AMD Ryzen 9 5900X 12-Core Processor
Memory: 64GB
Caching...
Warming up...
Found 284 files
Parsed in 49455μs
Deserialized in 13017μs
Read and parsed in 47431μs
Cache hit plus deserialization in 21498μs
Cache miss plus parsing in 58969μs

entone avatar Jan 09 '23 14:01 entone

Here are my results for CubDB on a 2020 MacBook Pro (2 GHz Quad-Core Intel Core i5):

Caching...
Warming up...
Found 14 files
Parsed in 13667μs
Deserialized in 3315μs
Read and parsed in 16754μs
Cache hit plus deserialization in 4895μs
Cache miss plus parsing in 20313μs

lucaong avatar Jan 09 '23 15:01 lucaong

MacBook Pro with an M1 Pro with macOS 12.6

Found 567 files
Parsed in 88386μs
Deserialized in 15817μs
Read and parsed in 147066μs
Cache hit plus deserialization in 123187μs
Cache miss plus parsing in 195406μs
Found 660 files
Parsed in 169228μs
Deserialized in 20565μs
Read and parsed in 399328μs
Cache hit plus deserialization in 179763μs
Cache miss plus parsing in 431854μs

ypconstante avatar Jan 09 '23 15:01 ypconstante

MacBook Pro M1 Pro with macOS Ventura 3.0.1

Caching...
Warming up...
Found 144 files
Parsed in 9801μs
Deserialized in 2678μs
Read and parsed in 28224μs
Cache hit plus deserialization in 12728μs
Cache miss plus parsing in 33615μs
Caching...
Warming up...
Found 64 files
Parsed in 6312μs
Deserialized in 1229μs
Read and parsed in 23204μs
Cache hit plus deserialization in 8616μs
Cache miss plus parsing in 17986μs

krns avatar Jan 09 '23 15:01 krns

2018 Lenovo Notebook (1.8 GHz Quad-Core Intel Core i7) with Microsoft Windows 11

Found 60 files
Parsed in 64921μs
Deserialized in 7680μs
Read and parsed in 86016μs
Cache hit plus deserialization in 31641μs
Cache miss plus parsing in 113868μs
Found 146 files
Parsed in 37171μs
Deserialized in 10035μs
Read and parsed in 65331μs
Cache hit plus deserialization in 32563μs
Cache miss plus parsing in 86528μs

cw789 avatar Jan 09 '23 15:01 cw789

2019 MacBook Pro (2,6 GHz 6-Core Intel Core i7)

Found 446 files
Parsed in 71539μs
Deserialized in 15179μs
Read and parsed in 70861μs
Cache hit plus deserialization in 43437μs
Cache miss plus parsing in 77311μs

hzeus avatar Jan 09 '23 15:01 hzeus

A reasonably large Phoenix project, on a 2019 MacBook Pro (2.6 GHz 6-Core Intel Core i7):

Caching...
Warming up...
Found 1683 files
Parsed in 436158μs
Deserialized in 60060μs
Read and parsed in 471458μs
Cache hit plus deserialization in 366630μs
Cache miss plus parsing in 907296μs

kerryb avatar Jan 09 '23 15:01 kerryb

2017 MacBook Pro (2.9 GHz 4-core Intel Core i7):

Caching...
Warming up...
Found 1436 files
Parsed in 2670215μs
Deserialized in 214961μs
Read and parsed in 2330537μs
Cache hit plus deserialization in 819989μs
Cache miss plus parsing in 3527866μs

voltone avatar Jan 09 '23 15:01 voltone

AMD Ryzen Threadripper 1920X (24) @ 3.500GHz / Linux 5.15.86-1-lts

for plausible:

Caching...
Warming up...
Found 154 files
Parsed in 69677μs
Deserialized in 20120μs
Read and parsed in 74430μs
Cache hit plus deserialization in 33578μs
Cache miss plus parsing in 96952μs

and for livebook:

Caching...
Warming up...
Found 178 files
Parsed in 161536μs
Deserialized in 30697μs
Read and parsed in 129997μs
Cache hit plus deserialization in 64808μs
Cache miss plus parsing in 186107μs

and for a larger project:

Caching...
Warming up...
Found 987 files
Parsed in 576813μs
Deserialized in 108900μs
Read and parsed in 445281μs
Cache hit plus deserialization in 342555μs
Cache miss plus parsing in 1015095μs

aerosol avatar Jan 09 '23 15:01 aerosol

11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz / on Ubuntu 22.04.1 LTS / Linux 5.15.0-57-generic

Caching...
Warming up...
Found 392 files
Parsed in 111635μs
Deserialized in 23521μs
Read and parsed in 96376μs
Cache hit plus deserialization in 62927μs
Cache miss plus parsing in 132254μs

maennchen avatar Jan 09 '23 15:01 maennchen

Thanks everyone! We have enough data for now.

It seems doing such optimization would make it 30% more expensive on a miss and 66% faster on a hit. But overall the times are small. Projects with a thousand of files take 500ms to parse them, which is happening in parallel and only a small cost of the compilation, so at the moment I don't believe this is worth exploring. :)

josevalim avatar Jan 09 '23 15:01 josevalim