Benchmarking Fibonacci. E-Graphs memoize computation.

using Metatheory
using Test

function fib end

fibo = @theory x y n begin
  x::Int + y::Int => x + y
  fib(n::Int) => (n < 2 ? n : :(fib($(n - 1)) + fib($(n - 2))))
end

params = SaturationParams(timeout = 60)
SaturationParams(60, 0x0000000000000000, 5000, 15000, nothing, Metatheory.EGraphs.var"#30#32"(), Metatheory.EGraphs.Schedulers.BackoffScheduler, (), false, true)

We run the saturation twice to see a result that does not include compilation time.

g = EGraph(:(fib(10)))
saturate!(g, fibo, params)
SaturationReport
=================
	Stop Reason: saturated
	Iterations: 16
	EGraph Size: 15 eclasses, 54 nodes
 ────────────────────────────────────────────────────────────────────
                            Time                    Allocations      
                   ───────────────────────   ────────────────────────
 Tot / % measured:      398ms /  12.4%           24.5MiB /   7.9%    

 Section   ncalls     time    %tot     avg     alloc    %tot      avg
 ────────────────────────────────────────────────────────────────────
 Search        16   31.7ms   64.5%  1.98ms   1.32MiB   68.4%  84.3KiB
   2           16   31.5ms   64.2%  1.97ms   1.27MiB   66.0%  81.4KiB
   1           16    129μs    0.3%  8.08μs   43.2KiB    2.2%  2.70KiB
 Apply         16   17.2ms   35.1%  1.08ms    553KiB   28.1%  34.6KiB
 Rebuild       16    234μs    0.5%  14.6μs   69.4KiB    3.5%  4.34KiB
 ────────────────────────────────────────────────────────────────────

That's fast!

z = EGraph(:(fib(10)))
saturate!(z, fibo, params)
SaturationReport
=================
	Stop Reason: saturated
	Iterations: 16
	EGraph Size: 15 eclasses, 54 nodes
 ────────────────────────────────────────────────────────────────────
                            Time                    Allocations      
                   ───────────────────────   ────────────────────────
 Tot / % measured:     1.46ms /  84.6%            575KiB /  87.3%    

 Section   ncalls     time    %tot     avg     alloc    %tot      avg
 ────────────────────────────────────────────────────────────────────
 Apply         16    819μs   66.3%  51.2μs    332KiB   66.2%  20.7KiB
 Rebuild       16    209μs   16.9%  13.1μs   69.4KiB   13.8%  4.34KiB
 Search        16    208μs   16.8%  13.0μs    100KiB   20.0%  6.27KiB
   2           16   97.3μs    7.9%  6.08μs   53.0KiB   10.6%  3.31KiB
   1           16   96.7μs    7.8%  6.04μs   43.2KiB    8.6%  2.70KiB
 ────────────────────────────────────────────────────────────────────

We can test that the result is correct.

@testset "Fibonacci" begin
  @test 55 == extract!(g, astsize)
end
Test.DefaultTestSet("Fibonacci", Any[], 1, false, false, true, 1.699627293090282e9, 1.699627293124442e9, false)

This page was generated using Literate.jl.