Explore profile guided specialization.
This is just an idle thought but I it seemed good to write up.
Given a function
foo :: Class a => a -> T
it's generally desirable to specialize this function for the actual types
a it's used with.
This is currently either done by hand (using pragmas or otherwise), or at use sites when we see
a monomorphic use of the function at which point we try to specialize for a specific type
It might be possible to do this guided by a profile as follows.
- We attach an identifier to dictionary objects at runtime.
- When generating code for
fooin profiling mode we call a wrapper which records:
- Which function was called
- Which dictionaries it was called with.
This tells us which functions are used with which types. Based on this information we could do a much better job at:
- Ensuring overloaded functions get unfoldings when needed.
- Ensure overloaded functions get specializations when desired.
- Avoid blowup of specialization on types which don't benefit from it.
The idea sounds simple enough. But I'm sure there are all kinds of engineering problems which would be tricky to figure out.