Obviously, performance is an issue for me, and hence I try avoid the usual pitfalls, like for loops and inadvertent boxing/unboxing in places where it matters. Still, I sometimes run in situations where I feel myself longing for something like C++ templates. I am well aware of the @specialized annotation but it is not good enough:
- It only works for primitive types.
- AFAIK, the number of specializations grows exponentially with the number of specialized type parameters. However, in my practice, I only need specializations for certain parameter combinations.
- Nearly nothing is specialized in the Scala library (I guess to avoid code bloat), so when you use containers in specialized code, there will be a lot of boxing and unboxing, and the code will run way slower than non-specialized code that uses boxed values from the start.
- In the case of libraries, not the user decides what to specialize but the provider.
So what about on-demand specialization? What if we allow for @specialized annotations in new statements? For example:
val intList = new List[@specialized Int]
Of course, overuse of this feature would result in code bloat, so it should be used only where performance matters.
So what do you think? Is my idea viable? Has this been tried before? Would it be easy to implement or would it take a lot of time and effort?