Skip to content

Commit fba86e4

Browse files
committed
Reduce allocations in Uncurry.transformArgs
- Use map2conserve to reuse the incoming argument list in the common case when there are no vararg or by-name translations to make - Avoid the temporary list of types in `.paramTypes` in favour of just getting the list of `params` (`Symbols`) and reading the `.info` of them
1 parent ac7a2aa commit fba86e4

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/compiler/scala/tools/nsc/transform/UnCurry.scala

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,9 @@ abstract class UnCurry extends InfoTransform
247247
}
248248
}
249249

250-
def transformArgs(pos: Position, fun: Symbol, args: List[Tree], formals: List[Type]): List[Tree] = {
250+
def transformArgs(pos: Position, fun: Symbol, args: List[Tree], params: List[Symbol]): List[Tree] = {
251251
val isJava = fun.isJavaDefined
252+
252253
def transformVarargs(varargsElemType: Type) = {
253254
def mkArrayValue(ts: List[Tree], elemtp: Type) =
254255
ArrayValue(TypeTree(elemtp), ts) setType arrayType(elemtp)
@@ -314,7 +315,7 @@ abstract class UnCurry extends InfoTransform
314315
else arrayToSequence(tree, varargsElemType, copy = isNewCollections) // existing array, make a defensive copy
315316
}
316317
else {
317-
def mkArray = mkArrayValue(args drop (formals.length - 1), varargsElemType)
318+
def mkArray = mkArrayValue(args drop (params.length - 1), varargsElemType)
318319
if (javaStyleVarArgs) mkArray
319320
else if (args.isEmpty) gen.mkNil // avoid needlessly double-wrapping an empty argument list
320321
else arrayToSequence(mkArray, varargsElemType, copy = false) // fresh array, no need to copy
@@ -328,18 +329,19 @@ abstract class UnCurry extends InfoTransform
328329
}
329330
}
330331
}
331-
args.take(formals.length - 1) :+ (suffix setType formals.last)
332+
args.take(params.length - 1) :+ (suffix setType params.last.info)
332333
}
333334

334-
val args1 = if (isVarArgTypes(formals)) transformVarargs(formals.last.typeArgs.head.widen) else args
335+
val isVarargs = isVarArgsList(params)
336+
val args1 = if (isVarargs) transformVarargs(params.last.info.typeArgs.head.widen) else args
335337

336-
map2(formals, args1) { (formal, arg) =>
337-
if (!isByNameParamType(formal)) arg
338+
map2Conserve(args1, params) { (arg, param) =>
339+
if (!isByNameParamType(param.info)) arg
338340
else if (isByNameRef(arg)) { // thunk does not need to be forced because it's a reference to a by-name arg passed to a by-name param
339341
byNameArgs += arg
340342
arg setType functionType(Nil, arg.tpe)
341343
} else {
342-
log(s"Argument '$arg' at line ${arg.pos.line} is $formal from ${fun.fullName}")
344+
log(s"Argument '$arg' at line ${arg.pos.line} is ${param.info} from ${fun.fullName}")
343345
def canUseDirectly(qual: Tree) = qual.tpe.typeSymbol.isSubClass(FunctionClass(0)) && treeInfo.isExprSafeToInline(qual)
344346
arg match {
345347
// don't add a thunk for by-name argument if argument already is an application of
@@ -482,16 +484,16 @@ abstract class UnCurry extends InfoTransform
482484
super.transform(tree)
483485

484486
case Apply(fn, args) =>
485-
// Read formals before `transform(fn)`, because UnCurry replaces T* by Seq[T] (see DesugaredParameterType).
487+
// Read the param symbols before `transform(fn)`, because UnCurry replaces T* by Seq[T] (see DesugaredParameterType).
486488
// The call to `transformArgs` below needs `formals` that still have varargs.
487-
val formals = fn.tpe.paramTypes
489+
val fnParams = fn.tpe.params
488490
val transformedFn = transform(fn)
489491
// scala/bug#6479: no need to lift in args to label jumps
490492
// scala/bug#11127: boolean && / || are emitted using jumps, the lhs stack value is consumed by the conditional jump
491493
val noReceiverOnStack = fn.symbol.isLabel || fn.symbol == currentRun.runDefinitions.Boolean_and || fn.symbol == currentRun.runDefinitions.Boolean_or
492494
val needLift = needTryLift || !noReceiverOnStack
493495
withNeedLift(needLift) {
494-
treeCopy.Apply(tree, transformedFn, transformTrees(transformArgs(tree.pos, fn.symbol, args, formals)))
496+
treeCopy.Apply(tree, transformedFn, transformTrees(transformArgs(tree.pos, fn.symbol, args, fnParams)))
495497
}
496498

497499
case Assign(_: RefTree, _) =>

0 commit comments

Comments
 (0)