Why do I need a special annotation?

Reading Function2 source code I noticed that @unspecialized was added recently (in scala 2.10) . What are the reasons for this and how does this affect compilation? Why do we need this for the * tupled , compose function and some other methods?

+4
source share
1 answer

I would say that it is safe to assume that it disables specialization for the target method. A good reason to disable specialization is to prevent bytecode from running in. Specializing each method indiscriminately is a bad idea, because each specialization is basically a separate copy of the same method, and the size of the bytecode is growing quite quickly. Therefore, I assume that the Function2 specialization was generally considered equal to increasing the size of the bytecode, with the exception of tupled and compose , which were not important enough to justify the increase in add-on. This is a delicate balance between code size and execution speed, the idea is to get the most bugs for dollars.

Also, as a fun illustration of how problematic the code bloat caused by the specification can be, see this recipe for a scala bomb:

Scala bomb? (e.g. lightning)

+2
source

Source: https://habr.com/ru/post/1488160/


All Articles