Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve generated code for derived instances #3189

Merged
merged 5 commits into from
Nov 26, 2024
Merged

Conversation

lukaszcz
Copy link
Collaborator

@lukaszcz lukaszcz commented Nov 22, 2024

  • Closes Improve the inlining optimization to handle derived instances well #3186
  • Changes the generated code for derived Eq instances to define a local recursive function with a let instead of a lambda. This is necessary, because otherwise an indirection is generated on each recursive call -- instance resolution inserts the instance identifier, which then is matched on and the function for the recursive call extracted instead of being called directly. This cannot be (easily) optimized in Core because, to avoid looping in the compiler, in general one cannot do inlining of recursive functions.
  • Minor improvements to the inlining optimization and the main optimization phase: do inlining before specialization and more liberal rules for heuristic case value inlining.

@lukaszcz lukaszcz added this to the 0.6.9 milestone Nov 22, 2024
@lukaszcz lukaszcz self-assigned this Nov 22, 2024
@lukaszcz lukaszcz force-pushed the better-inline-case branch 3 times, most recently from 4992e43 to d4305a7 Compare November 25, 2024 13:38
@lukaszcz lukaszcz marked this pull request as ready for review November 25, 2024 13:38
@lukaszcz lukaszcz mentioned this pull request Nov 25, 2024
@janmasrovira
Copy link
Collaborator

janmasrovira commented Nov 26, 2024

I've used the code below to benchmark the potential gains of replacing the lambda with a recursive function defined in a let. I've used the main branch (f916c0a) to benchmark this, so the changes in the inlining phase implemented in this pr do not affect this.

module main;

import Stdlib.Prelude open;

type UNat :=
  | uzero
  | usuc UNat;

instance
eq-lambda : Eq UNat :=
  mkEq
    λ{
      uzero uzero := true
      | (usuc n) (usuc m) := Eq.eq n m
      | _ _ := false
    };

eq-let : Eq UNat :=
  let
    eq : UNat -> UNat -> Bool
      | uzero uzero := true
      | (usuc n) (usuc m) := eq n m
      | _ _ := false;
  in mkEq eq;

r : Nat -> UNat
  | zero := uzero
  | (suc n) := usuc (r n);

instance
fromnat : FromNatural UNat := mkFromNatural r;

n : UNat := fromNat 10000000;

main1 : Bool := Eq.eq {{eq-lambda}} n n;

main2 : Bool := Eq.eq {{eq-let}} n n;

main : Bool := ...;

I've used the native backend.

juvix compile native main.juvix --output lambda  # with main = main1
juvix compile native main.juvix --output let     # with main = main2

I've run hyperfine --warmup 1 './let' './lambda':

Benchmark 1: ./let
  Time (mean ± σ):     256.5 ms ±  11.3 ms    [User: 46.1 ms, System: 208.0 ms]
  Range (min … max):   245.5 ms … 282.7 ms    10 runs

Benchmark 2: ./lambda
  Time (mean ± σ):     368.6 ms ±   6.1 ms    [User: 71.6 ms, System: 293.1 ms]
  Range (min … max):   360.3 ms … 378.7 ms    10 runs

Summary
  ./let ran
    1.44 ± 0.07 times faster than ./lambda

@janmasrovira janmasrovira merged commit 63b52f9 into main Nov 26, 2024
4 checks passed
@janmasrovira janmasrovira deleted the better-inline-case branch November 26, 2024 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve the inlining optimization to handle derived instances well
2 participants