Miniboxing is a Scala compiler transformation that improves the performance of generics for primitive numeric types. Unlike specialization, miniboxing reduces the amount of generated bytecode without sacrificing execution speed, therefore being capable of transforming the Scala collections hierachy. Miniboxing is currently implemented as a Scala compiler plugin which you can easily try on your project.
Miniboxing makes a trade-off between the bytecode size and performance: it produces 4x-100x less bytecode than naively duplicating and adapting the generic bytecode for every primitive type. Still, its overhead is less than 10% compared to manually specialized code. Benchmarks show miniboxing speeds up generic collections by factors of 1.5x to 22x. The following benchmark shows the miniboxing plugin at work on an array buffer:
genericis the generic code
miniboxingis the code generated by our plugin
specializationis the code generated by the
@specializedannotation in Scala
monomorphicis the code specialized by hand
Why not try it yourself?
Using miniboxing is as easy as adding an annotation:
class C[@miniboxed T](val t: T)
Please keep in mind that the miniboxing plugin is a beta release, and not a feature-complete, perfect drop-in for your project.
We are doing our best to make miniboxing a stable transformation, with nightly builds and hundreds of test cases running every night. Yet, there are bugs we haven't fixed yet, so don't be surprised if the plugin fails on some programs. But please do file bugs for such failures, so we can fix them asap!