Skip to content

Commit

Permalink
Avoid use of WrappedArray in favour of direct Array usage
Browse files Browse the repository at this point in the history
  • Loading branch information
retronym committed Feb 22, 2018
1 parent 554f4ae commit 351ab3d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import java.util.{ Arrays, Comparator }
import scala.tools.nsc.symtab.Flags
import xsbti.api._

import scala.annotation.tailrec
import scala.tools.nsc.Global

/**
Expand Down Expand Up @@ -160,7 +161,7 @@ class ExtractAPI[GlobalType <: Global](
* Force all lazy structures. This is necessary so that we see the symbols/types at this phase and
* so that we don't hold on to compiler objects and classes
*/
def forceStructures(): Unit =
@tailrec final def forceStructures(): Unit =
if (pending.isEmpty)
structureCache.clear()
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ object APIUtil {
// that inherits a macro does not have a macro.
override def visitStructure0(structure: Structure): Unit = {
visitTypes(structure.parents)
visitDefinitions(structure.declared.toArray[Definition])
visitDefinitions(structure.declared)
}

override def visitModifiers(m: Modifiers): Unit = {
Expand Down
37 changes: 22 additions & 15 deletions internal/zinc-apiinfo/src/main/scala/xsbt/api/HashAPI.scala
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,15 @@ final class HashAPI private (

private[this] var hash: Hash = 0

@inline final def hashString(s: String): Unit = extend(stringHash(s))
@inline final def hashBoolean(b: Boolean): Unit = extend(if (b) TrueHash else FalseHash)
@inline final def hashSeq[T](s: Seq[T], hashF: T => Unit): Unit = {
final def hashString(s: String): Unit = extend(stringHash(s))
final def hashBoolean(b: Boolean): Unit = extend(if (b) TrueHash else FalseHash)
@inline final def hashArray[T <: AnyRef](s: Array[T], hashF: T => Unit): Unit = {
extend(s.length)
s foreach hashF
var i = 0
while (i < s.length) {
hashF(s(i))
i += 1
}
}
final def hashSymmetric[T](ts: TraversableOnce[T], hashF: T => Unit): Unit = {
val current = hash
Expand Down Expand Up @@ -230,12 +234,12 @@ final class HashAPI private (
hashString(id.value)
}

def hashValueParameters(valueParameters: Seq[ParameterList]) =
hashSeq(valueParameters, hashValueParameterList)
def hashValueParameters(valueParameters: Array[ParameterList]) =
hashArray(valueParameters, hashValueParameterList)
def hashValueParameterList(list: ParameterList) = {
extend(ValueParamsHash)
hashBoolean(list.isImplicit)
hashSeq(list.parameters, hashValueParameter)
hashArray(list.parameters, hashValueParameter)
}
def hashValueParameter(parameter: MethodParameter) = {
hashString(parameter.name)
Expand All @@ -259,7 +263,8 @@ final class HashAPI private (
hashType(d.tpe)
}

def hashTypeParameters(parameters: Seq[TypeParameter]) = hashSeq(parameters, hashTypeParameter)
def hashTypeParameters(parameters: Array[TypeParameter]) =
hashArray(parameters, hashTypeParameter)
def hashTypeParameter(parameter: TypeParameter): Unit = {
hashString(parameter.id)
extend(parameter.variance.ordinal)
Expand All @@ -268,20 +273,20 @@ final class HashAPI private (
hashType(parameter.upperBound)
hashAnnotations(parameter.annotations)
}
def hashAnnotations(annotations: Seq[Annotation]) = hashSeq(annotations, hashAnnotation)
def hashAnnotations(annotations: Array[Annotation]) = hashArray(annotations, hashAnnotation)
def hashAnnotation(annotation: Annotation) = {
hashType(annotation.base)
hashAnnotationArguments(annotation.arguments)
}
def hashAnnotationArguments(args: Seq[AnnotationArgument]) =
hashSeq(args, hashAnnotationArgument)
def hashAnnotationArguments(args: Array[AnnotationArgument]) =
hashArray(args, hashAnnotationArgument)
def hashAnnotationArgument(arg: AnnotationArgument): Unit = {
hashString(arg.name)
hashString(arg.value)
}

def hashTypes(ts: Seq[Type], includeDefinitions: Boolean = true) =
hashSeq(ts, (t: Type) => hashType(t, includeDefinitions))
def hashTypes(ts: Array[Type], includeDefinitions: Boolean = true) =
hashArray(ts, (t: Type) => hashType(t, includeDefinitions))
def hashType(t: Type, includeDefinitions: Boolean = true): Unit =
t match {
case s: Structure => hashStructure(s, includeDefinitions)
Expand All @@ -304,7 +309,9 @@ final class HashAPI private (
extend(SingletonHash)
hashPath(s.path)
}
def hashPath(path: Path) = hashSeq(path.components, hashPathComponent)
def hashPath(path: Path) = {
hashArray(path.components, hashPathComponent)
}
def hashPathComponent(pc: PathComponent) = pc match {
case _: This => extend(ThisPathHash)
case s: Super => hashSuperPath(s)
Expand Down Expand Up @@ -357,7 +364,7 @@ final class HashAPI private (
hashDefinitions(structure.inherited, false)
}
}
def hashParameters(parameters: Seq[TypeParameter], base: Type): Unit = {
def hashParameters(parameters: Array[TypeParameter], base: Type): Unit = {
hashTypeParameters(parameters)
hashType(base)
}
Expand Down
31 changes: 20 additions & 11 deletions internal/zinc-apiinfo/src/main/scala/xsbt/api/Visit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Visit {
visitString(p.name)
}

def visitDefinitions(ds: Seq[Definition]) = ds foreach visitDefinition
def visitDefinitions(ds: Array[_ <: Definition]) = visitArray(ds, visitDefinition)
def visitDefinition(d: Definition): Unit = {
visitString(d.name)
visitAnnotations(d.annotations)
Expand Down Expand Up @@ -86,9 +86,10 @@ class Visit {
def visitProtected(p: Protected): Unit = visitQualifier(p.qualifier)
def visitModifiers(m: Modifiers): Unit = ()

def visitValueParameters(valueParameters: Seq[ParameterList]) =
valueParameters foreach visitValueParameterList
def visitValueParameterList(list: ParameterList) = list.parameters foreach visitValueParameter
def visitValueParameters(valueParameters: Array[ParameterList]) =
visitArray(valueParameters, visitValueParameterList)
def visitValueParameterList(list: ParameterList) =
visitArray(list.parameters, visitValueParameter)
def visitValueParameter(parameter: MethodParameter) = {
visitString(parameter.name)
visitType(parameter.tpe)
Expand All @@ -107,26 +108,27 @@ class Visit {
visitType(d.tpe)
}

def visitTypeParameters(parameters: Seq[TypeParameter]) = parameters foreach visitTypeParameter
def visitTypeParameters(parameters: Array[TypeParameter]) =
visitArray(parameters, visitTypeParameter)
def visitTypeParameter(parameter: TypeParameter): Unit = {
visitTypeParameters(parameter.typeParameters)
visitType(parameter.lowerBound)
visitType(parameter.upperBound)
visitAnnotations(parameter.annotations)
}
def visitAnnotations(annotations: Seq[Annotation]) = annotations foreach visitAnnotation
def visitAnnotations(annotations: Array[Annotation]) = visitArray(annotations, visitAnnotation)
def visitAnnotation(annotation: Annotation) = {
visitType(annotation.base)
visitAnnotationArguments(annotation.arguments)
}
def visitAnnotationArguments(args: Seq[AnnotationArgument]) =
args foreach visitAnnotationArgument
def visitAnnotationArguments(args: Array[AnnotationArgument]) =
visitArray(args, visitAnnotationArgument)
def visitAnnotationArgument(arg: AnnotationArgument): Unit = {
visitString(arg.name)
visitString(arg.value)
}

def visitTypes(ts: Seq[Type]) = ts.foreach(visitType)
def visitTypes(ts: Array[Type]) = visitArray(ts, visitType)
def visitType(t: Type): Unit = {
t match {
case s: Structure => visitStructure(s)
Expand All @@ -145,7 +147,7 @@ class Visit {
def visitEmptyType(): Unit = ()
def visitParameterRef(p: ParameterRef): Unit = ()
def visitSingleton(s: Singleton): Unit = visitPath(s.path)
def visitPath(path: Path) = path.components foreach visitPathComponent
def visitPath(path: Path) = visitArray(path.components, visitPathComponent)
def visitPathComponent(pc: PathComponent) = pc match {
case t: This => visitThisPath(t)
case s: Super => visitSuperPath(s)
Expand Down Expand Up @@ -180,9 +182,16 @@ class Visit {
visitDefinitions(structure.declared)
visitDefinitions(structure.inherited)
}
def visitParameters(parameters: Seq[TypeParameter], base: Type): Unit = {
def visitParameters(parameters: Array[TypeParameter], base: Type): Unit = {
visitTypeParameters(parameters)
visitType(base)
}
def visitString(s: String): Unit = ()
@inline final def visitArray[T <: AnyRef](ts: Array[T], f: T => Unit): Unit = {
var i = 0
while (i < ts.length) {
f(ts(i))
i += 1
}
}
}

0 comments on commit 351ab3d

Please sign in to comment.