# Inline caching in not object oriented languages

I've been recently studying inline caching as a technique to optimize method dispatch in object oriented languages. Basically, the idea is that one can remember what was previously dispatched and predict that things will go on in the same way.

My question is whether this technique has been used in pure functional languages (I'm a native of Scala which mixes both paradigms thus the uncertainty).

• Method dispatch is inherent to object orientation. What would you consider analogous in a non-OO language? – Gilles May 31 '17 at 18:34

Let's say you have a function h :: (A -> B) -> C (this is using Haskell syntax). h is a higher order function and particularly it takes a function of type A -> B. The definition of h will be something like:
h f = ... (f a) ...

h doesn't know what f will be so it must emit code for a call to an unknown function. Of course, if (h g) occurs in the code where g is a known function, the compiler may choose to inline h and then further optimize the inlined body of h with f now known. This is just normal inlining. We could also imagine a similar variation where we make specialized versions of h for some finite collection of functions A -> B and call the appropriate specialized version rather than inlining. (For GHC Haskell, this occurs with type classes via the SPECIALIZE pragma.) Neither of these are inline caching, though you could imagine a "static" version of inline caching that adds the appropriate guards and branches to specialized code if possible and otherwise falls back to generic code. I don't believe this is a particularly common optimization whether a language is functional or not. It's similar to rearranging branches to improve branch prediction though.