# Idea: scope- (rather than type-) centric composable optimizations

**URL:** <https://discourse.julialang.org/t/idea-scope-rather-than-type-centric-composable-optimizations/40118>\
**Category:** Internals & Design\
**Created:** [May 25, 2020, 11:38am UTC](https://discourse.julialang.org/t/idea-scope-rather-than-type-centric-composable-optimizations/40118 "2020-05-25T11:38:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hessammehr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hessammehr/32/3847_2.png) [@hessammehr](https://discourse.julialang.org/u/hessammehr)\
**Post date:** [May 25, 2020, 11:38am UTC](https://discourse.julialang.org/t/idea-scope-rather-than-type-centric-composable-optimizations/40118/1 "2020-05-25T11:38:12Z")

</div>

I was inspired by [this paper](http://www.cs.uccs.edu/~qyi/papers/LCPC15.pdf) and the following observations to sketch out a model of compositional optimizations that I thought I would share with the community.

- Simple interpretation can be faster than Julia’s per-function JIT for non-hot code paths.
- Julia has done a lot of work to make compilation transparent, but it’s still hard to control/compose directly how optimizations are applied.
- Optimizations are very much type-centric, i.e. dictated by types, rather than a combination of types and algorith/control flow.

I think the idea of using a lazy computation graph combined with scopes to declare and compose, which stops short of using a GUI to interactively apply high-level optimizations as in the paper, might be a viable long-term evolution path for Julia that:

- Addresses unnecessary compilation and the time to first plot issue, since unadorned code can simply be interpreted with Python-like performance.
- Prevents Julia turning into yet another opaque JIT like V8 that seems to be the current direction with interpretation + heuristic JIT application
- Allows domain knowledge to dictate what optimizations are applied at the top.
- Allows new optimizations to be packaged as libraries.
- Optimizations can be written in Julia itself as pattern matches applied to the computation graph.
- Does not intermix the mathematical/algorithmic description of programs versus their implementation using lower-level control flow primitives (for loops, etc.).
- Is more deliberate and avoids pathological cases of having to dig through `@code_warntype` to satisfy the compiler about type stability.

In a nutshell, I think types are a great first step towards giving developers a nice way to negotiate optimization with the compiler but they are not the whole solution.

A tiny code example showing the concept:

```julia
@with UnrollLoop(; n=16), ParallelLoop()
  z = Conv2D(x, y)
  # nested optimization scope
  @with OtherOptimization()
     ...
  end
end

```

I have written a short [blog post](https://github.com/hessammehr/hessammehr.github.io/blob/master/blog/posts/2020-05-24-lazy-interactive-optimization.md) on the subject for more context if anyone is interested and would love to hear your thoughts and alternative ideas for extending Julia’s performance and expressiveness.

---

<div class="post-metadata">

**Author:** ![datnamer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datnamer/32/3471_2.png) [@datnamer](https://discourse.julialang.org/u/datnamer)\
**Post date:** [May 25, 2020, 12:33pm UTC](https://discourse.julialang.org/t/idea-scope-rather-than-type-centric-composable-optimizations/40118/2 "2020-05-25T12:33:40Z")

</div>

This seems relevant to the compiler hooks @Keno and @staticfloat have been working on.

Also you might want to look at [GitHub - MikeInnes/Mjolnir.jl: A little less conversation, a little more abstraction](https://github.com/MikeInnes/Mjolnir.jl) which seems to do some of that as well.

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [May 26, 2020, 2:35am UTC](https://discourse.julialang.org/t/idea-scope-rather-than-type-centric-composable-optimizations/40118/3 "2020-05-26T02:35:54Z")

</div>

Related future julia features:

- The ability to set the optimization level on a per-module basis (coming on Julia 1.5) [Pull request link](https://github.com/JuliaLang/julia/pull/34896)
- An infrastructure to add custom compiler passes (coming in Julia, 1.6) [Pull request link](https://github.com/JuliaLang/julia/pull/33955)
