Skip to content

Commit

Permalink
Remove allocations of Options also for Product1 extractors.
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkDimius committed Dec 2, 2014
1 parent b8d14a1 commit 2d22839
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
49 changes: 25 additions & 24 deletions src/compiler/scala/tools/nsc/backend/jvm/BackendInterface.scala
Expand Up @@ -14,6 +14,7 @@ import scala.tools.asm
trait BackendInterface extends BackendInterfaceDefinitions{
type Flags = Long

type Constant >: Null <: AnyRef
type Symbol >: Null <: AnyRef
type Type >: Null <: AnyRef
type Annotation >: Null <: AnyRef
Expand Down Expand Up @@ -46,10 +47,10 @@ trait BackendInterface extends BackendInterfaceDefinitions{
type Name >: Null <: AnyRef
type Position
type CompilationUnit <: AnyRef
type Bind >: Null <: Tree
type New >: Null <: Tree
type Bind >: Null <: Tree
type New >: Null <: Tree
type ApplyDynamic >: Null <: Tree
type Super >: Null <: Tree
type Super >: Null <: Tree


implicit val TypeDefTag: ClassTag[TypeDef]
Expand Down Expand Up @@ -84,8 +85,6 @@ trait BackendInterface extends BackendInterfaceDefinitions{
implicit val SuperTag: ClassTag[Super]
implicit val ConstantClassTag: ClassTag[Constant]


type Constant
type ConstantTag = Int

val UnitTag: ConstantTag
Expand Down Expand Up @@ -218,7 +217,7 @@ trait BackendInterface extends BackendInterfaceDefinitions{
val Bind: BindDeconstructor
val ClassDef: ClassDefDeconstructor

trait DeconstructorCommon[T >: Null <: Tree] {
trait DeconstructorCommon[T >: Null <: AnyRef] {
var field: T = null
def get: this.type = this
def isEmpty: Boolean = field eq null
Expand All @@ -229,6 +228,17 @@ trait BackendInterface extends BackendInterfaceDefinitions{
}
}

trait Deconstructor1Common[T >: Null <: AnyRef, R]{
var field: T = _
def get: R
def isEmpty: Boolean = field eq null
def isDefined = !isEmpty
def unapply(s: T): this.type ={
field = s
this
}
}

trait ClassDefDeconstructor extends DeconstructorCommon[ClassDef] {
def _1: Modifiers
def _2: Name
Expand Down Expand Up @@ -262,38 +272,30 @@ trait BackendInterface extends BackendInterfaceDefinitions{
def _6: Tree
}

// todo do something with product1
trait ThisDeconstructor{
def unapply(s: This): Option[Name]
trait ThisDeconstructor extends Deconstructor1Common[This, Name]{
def apply(s: Symbol): Tree
}

trait IdentDeconstructor{
def unapply(s: Ident): Option[Name]
trait IdentDeconstructor extends Deconstructor1Common[Ident, Name]{
}

trait ReturnDeconstructor {
def unapply(s: Return): Option[Tree]
trait ReturnDeconstructor extends Deconstructor1Common[Return, Tree]{
}

trait ThrownException{
trait ThrownException {
def unapply(a: Annotation): Option[Symbol]
}

trait ThrowDeconstructor{
def unapply(s: Throw): Option[Tree]
trait ThrowDeconstructor extends Deconstructor1Common[Throw, Tree]{
}

trait ConstantDeconstructor{
def unapply(a: Constant): Option[Any]
trait ConstantDeconstructor extends Deconstructor1Common[Constant, Any]{
}

trait NewDeconstructor{
def unapply(s: New): Option[Type]
trait NewDeconstructor extends Deconstructor1Common[New, Type]{
}

trait AlternativeDeconstructor {
def unapply(s: Alternative): Option[List[Tree]]
trait AlternativeDeconstructor extends Deconstructor1Common[Alternative, List[Tree]]{
}

trait BlockDeconstructor extends DeconstructorCommon[Block]{
Expand All @@ -312,8 +314,7 @@ trait BackendInterface extends BackendInterfaceDefinitions{
def _2: List[Tree]
}

trait LiteralDeconstructor{
def unapply(a: Literal): Option[Constant]
trait LiteralDeconstructor extends Deconstructor1Common[Literal, Constant]{
}

trait AssignDeconstructor extends DeconstructorCommon[Assign]{
Expand Down
Expand Up @@ -186,40 +186,39 @@ class ScalacBackendInterface[G <: Global](val global: G) extends BackendInterfac
def _2: List[Tree] = field.args
}

// todo: this product1s should also eventually become name-based pattn matching
object Literal extends LiteralDeconstructor {
def unapply(a: Literal): Option[Constant] = Some(a.value)
def get = field.value
}

object Throw extends ThrowDeconstructor {
def unapply(s: Throw): Option[Tree] = Some(s.expr)
def get = field.expr
}

object New extends NewDeconstructor {
def unapply(s: New): Option[Type] = Some(s.tpt.tpe)
def get = field.tpt.tpe
}

object This extends ThisDeconstructor {
def unapply(s: This): Option[Name] = Some(s.qual)
def get = field.qual
def apply(s: global.Symbol): This = global.This(s.name.toTypeName) setSymbol s
}

object Return extends ReturnDeconstructor {
def unapply(s: Return): Option[Tree] = Some(s.expr)
def get = field.expr
}

object Ident extends IdentDeconstructor {
def unapply(s: Ident): Option[Name] = Some(s.name)
def get = field.name
}

object Alternative extends AlternativeDeconstructor {
def unapply(s: Alternative): Option[List[Tree]] = Some(s.trees)
def get = field.trees
}
object Constant extends ConstantDeconstructor {
def unapply(a: Constant): Option[Any] = Some(a.value)
def get = field.value
}
object ThrownException extends ThrownException {
def unapply(a: Annotation): Option[Symbol] = None // todo
def unapply(a: Annotation): Option[Symbol] = global.ThrownException.unapply(a)
}

object Try extends TryDeconstructor {
Expand Down

1 comment on commit 2d22839

@scala-jenkins
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Job pr-scala failed for 2d22839 Took 97 min. (ping @DarkDimius) (results):


To retry exactly this commit, comment PLS REBUILD/pr-scala@2d22839237a236a1656f9dc373982d745b6f5855 on PR 4136.
NOTE: New commits are rebuilt automatically as they appear. A forced rebuild is only necessary for transient failures.

Please sign in to comment.