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

specialization can put expensive computations in loops #646

Closed
digama0 opened this issue Aug 27, 2021 · 0 comments
Closed

specialization can put expensive computations in loops #646

digama0 opened this issue Aug 27, 2021 · 0 comments

Comments

@digama0
Copy link
Collaborator

digama0 commented Aug 27, 2021

def build (n : Nat) : Array Unit := do
  let mut out := #[]
  for _ in [0:n] do
    out := out.push ()
  out

@[noinline] def size : IO Nat := pure 50000

def bench (f : ∀ {α : Type}, α → IO Unit := fun _ => ()) : IO Unit := do
  let n ← size
  let arr := build n
  timeit "time" $
    for _ in [:30] do
      f $ #[1, 2, 3, 4].map fun ty => arr[ty]

#eval bench -- 2.5 s

Looking at the trace, it appears that the evaluation of let arr := build n is lowered into the function, as if it were written:

def bench (f : ∀ {α : Type}, α → IO Unit := fun _ => ()) : IO Unit := do
  let n ← size
  timeit "time" $
    for _ in [:30] do
      f $ #[1, 2, 3, 4].map fun ty => (build n)[ty]

which causes the large array to be constructed 120 times.

ChrisHughes24 pushed a commit to ChrisHughes24/lean4 that referenced this issue Dec 2, 2022
mathlib3 sha: fd47bdf09e90f553519c712378e651975fe8c829

Co-authored-by: Jireh Loreaux <loreaujy@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant