Noboxing plugin

2,872 views
2,833 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,872
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Noboxing plugin

  1. 1. No boxing here! A Scala compiler plugin walk-through Iulian Dragos Scala Solutions 1 Thursday, October 7, 2010
  2. 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. 3. How does this code perform? Boxing Implicit conversions Specialization 3 Thursday, October 7, 2010
  4. 4. Run the code in the profiler Instrument scala.runtime.BoxesRuntime 4 Thursday, October 7, 2010
  5. 5. Why not have the compiler tell us? 5 Thursday, October 7, 2010
  6. 6. Parser Type Checker Erasure Code Gen 6 Thursday, October 7, 2010
  7. 7. Parser Create the AST Type Checker Erasure Code Gen 6 Thursday, October 7, 2010
  8. 8. Parser Create the AST Type Checker Add symbols and types Erasure Code Gen 6 Thursday, October 7, 2010
  9. 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. 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. 11. Plugins Implement a Plugin interface name, description runsAfter distributed as jar files enabled by -Xplugin 7 Thursday, October 7, 2010
  12. 12. Live coding 8 Thursday, October 7, 2010
  13. 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. 14. Traverser/Transformer library classes for AST inspection/modifcation override the default traverse/transform(tree) 10 Thursday, October 7, 2010
  15. 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. 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. 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

×