DevoxxFR 2024 Reproducible Builds with Apache Maven
Noboxing plugin
1. No boxing here!
A Scala compiler plugin walk-through
Iulian Dragos
Scala Solutions
1
Thursday, October 7, 2010
2. def mult(m: Matrix[Int], n: Matrix[Int]) = {
val p = new Matrix[Int](m.rows, n.cols)
for (i <- 0 until m.rows)
for (j <- 0 until n.cols) {
var sum = 0
for (k <- 0 until n.rows)
sum += m(i, k) * n(k, j)
p(i, j) = sum
}
p
}
2
Thursday, October 7, 2010
3. How does this code perform?
Boxing
Implicit conversions
Specialization
3
Thursday, October 7, 2010
4. Run the code in the profiler
Instrument
scala.runtime.BoxesRuntime
4
Thursday, October 7, 2010
5. Why not have the compiler
tell us?
5
Thursday, October 7, 2010
6. Parser
Type Checker
Erasure
Code Gen
6
Thursday, October 7, 2010
7. Parser Create the AST
Type Checker
Erasure
Code Gen
6
Thursday, October 7, 2010
8. Parser Create the AST
Type Checker Add symbols and types
Erasure
Code Gen
6
Thursday, October 7, 2010
9. Parser Create the AST
Type Checker Add symbols and types
Erasure Remove generics and add boxing
Code Gen
6
Thursday, October 7, 2010
10. Parser Create the AST
Type Checker Add symbols and types
Erasure Remove generics and add boxing
@noboxing Issue warnings no box/unbox
Code Gen
6
Thursday, October 7, 2010
11. Plugins
Implement a Plugin interface
name, description
runsAfter
distributed as jar files
enabled by -Xplugin
7
Thursday, October 7, 2010
13. Scala AST
Definitions
PackageDef, ValDef, DefDef, ClassDef, TypeDef
Terms
New, If, Match, Return, Ident, This, Apply
Boxing is just a method call (Int.box)
9
Thursday, October 7, 2010
14. Traverser/Transformer
library classes for AST inspection/modifcation
override the default traverse/transform(tree)
10
Thursday, October 7, 2010
15. Symbols & Types
Each definition has a unique Symbol
references involve symbols (never names or trees)
Each Symbol has a Type
Each Tree has a Type
Some Trees have Symbols
11
Thursday, October 7, 2010
16. Definitions
definitions.
getClass returns the symbol of a fully-qualified name
getMember - member of a class/object/package
isBox/isUnbox - checks if a symbol is a boxing op
common symbols (PredefModule, ArrayClass, etc)
12
Thursday, October 7, 2010
17. More info
Scala-lang.org
http://www.scala-lang.org/node/140 (writing
compiler plugins)
http://www.scala-lang.org/node/598 (compiler
internals videos)
Source code on github
http://github.com/dragos/noboxing-plugin
13
Thursday, October 7, 2010