Skip to content

Commit

Permalink
JavaMirrors: avoid the use of List flatten and map
Browse files Browse the repository at this point in the history
In the JavaMirrors trait, in the function "mkMethodMirror",
the code was creating several unnecessary lists.

- We replace a call to `List.flatten.length` with a call to the
  `sumSize` method from the collections utils.
- We replace a `map` followed by an `exists` by merging the
  function of the `map` into the `exists`.
- We replace a call to `flatten` followed by an `exists`
  by a call to `mexists`.

This should, in total, prevent the creation of 5*N allocations,
with N being the sum of the length of all the tparams.
  • Loading branch information
diesalbla committed Mar 26, 2019
1 parent fff4dc0 commit bd7c8bb
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/reflect/scala/reflect/runtime/JavaMirrors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -326,21 +326,20 @@ private[scala] trait JavaMirrors extends internal.SymbolTable with api.JavaUnive
// that's because we want to have decent performance
// therefore we move special cases into separate subclasses
// rather than have them on a hot path them in a unified implementation of the `apply` method
private def mkMethodMirror[T: ClassTag](receiver: T, symbol: MethodSymbol): MethodMirror = {
def existsParam(pred: Type => Boolean) = symbol.paramss.flatten.map(_.info).exists(pred)
if (isBytecodelessMethod(symbol)) new BytecodelessMethodMirror(receiver, symbol)
else if (existsParam(isByNameParam) || existsParam(isValueClassParam)) new JavaTransformingMethodMirror(receiver, symbol)
else {
symbol.paramss.flatten.length match {
private def mkMethodMirror[T: ClassTag](receiver: T, symbol: MethodSymbol): MethodMirror =
if (isBytecodelessMethod(symbol))
new BytecodelessMethodMirror(receiver, symbol)
else if (mexists(symbol.paramss)(p => isByNameParam(p.info) || isValueClassParam(p.info)))
new JavaTransformingMethodMirror(receiver, symbol)
else
sumSize(symbol.paramss, 0) match {
case 0 => new JavaVanillaMethodMirror0(receiver, symbol)
case 1 => new JavaVanillaMethodMirror1(receiver, symbol)
case 2 => new JavaVanillaMethodMirror2(receiver, symbol)
case 3 => new JavaVanillaMethodMirror3(receiver, symbol)
case 4 => new JavaVanillaMethodMirror4(receiver, symbol)
case _ => new JavaVanillaMethodMirror(receiver, symbol)
}
}
}

private abstract class JavaMethodMirror(val symbol: MethodSymbol, protected val ret: DerivedValueClassMetadata) extends MethodMirror {
lazy val jmeth = ensureAccessible(methodToJava(symbol))
Expand Down

0 comments on commit bd7c8bb

Please sign in to comment.