From 43b499cf9a5be7c4ff8d72dff8fe9172398ea0bb Mon Sep 17 00:00:00 2001 From: Jens Johansen Date: Wed, 26 Apr 2023 11:29:28 +0000 Subject: [PATCH] [kernel] Optimize computeThisFunctionType When compiling compile.dart: 148,192 FunctionType are created via `computeThisFunctionType`. 133,085 of them have no named parameters; 141,507 of them have no type parameters; 128,357 of them have neither. This CL makes it not create new lists when not needed (or create empty iterators and sort empty lists), and makes the lists it do create not growable. Change-Id: Ibd5fef458b8a1254aa2f1fab80d0449a2f0bfd94 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/298541 Commit-Queue: Jens Johansen Reviewed-by: Johnni Winther --- pkg/kernel/lib/ast.dart | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/pkg/kernel/lib/ast.dart b/pkg/kernel/lib/ast.dart index af351ca8487..836c34dc354 100644 --- a/pkg/kernel/lib/ast.dart +++ b/pkg/kernel/lib/ast.dart @@ -4153,15 +4153,27 @@ class FunctionNode extends TreeNode { /// [FunctionType.withoutTypeParameters]. FunctionType computeThisFunctionType(Nullability nullability) { TreeNode? parent = this.parent; - List named = - namedParameters.map(_getNamedTypeOfVariable).toList(growable: false); - named.sort(); - // We need create a copy of the list of type parameters, otherwise - // transformations like erasure don't work. - List typeParametersCopy = new List.of( - parent is Constructor - ? parent.enclosingClass.typeParameters - : typeParameters); + List named; + if (namedParameters.isEmpty) { + named = const []; + } else { + named = + namedParameters.map(_getNamedTypeOfVariable).toList(growable: false); + named.sort(); + } + + List typeParametersCopy; + List typeParametersToCopy = parent is Constructor + ? parent.enclosingClass.typeParameters + : typeParameters; + if (typeParametersToCopy.isEmpty) { + typeParametersCopy = const []; + } else { + // We need create a copy of the list of type parameters, otherwise + // transformations like erasure don't work. + typeParametersCopy = + new List.of(typeParametersToCopy, growable: false); + } return new FunctionType( positionalParameters.map(_getTypeOfVariable).toList(growable: false), returnType,