Skip to content

Commit 7374863

Browse files
rakudramacommit-bot@chromium.org
authored andcommitted
[dart2js] Canonicalize empty collections in FunctionType
This reduces the number of _CompactLinkedHashSet objects by ~15% and the number of _GrowableList objects by ~10% in Phase1. Change-Id: I88ebe1b1c842977214985e23a1f3672ad43bdfdc Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/155826 Reviewed-by: Joshua Litt <[email protected]> Commit-Queue: Stephen Adams <[email protected]>
1 parent b494309 commit 7374863

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

pkg/compiler/lib/src/elements/types.dart

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -651,25 +651,50 @@ class FunctionType extends DartType {
651651

652652
final List<FunctionTypeVariable> typeVariables;
653653

654-
FunctionType._(
654+
FunctionType._allocate(
655655
this.returnType,
656656
this.parameterTypes,
657657
this.optionalParameterTypes,
658658
this.namedParameters,
659659
this.requiredNamedParameters,
660660
this.namedParameterTypes,
661661
this.typeVariables) {
662-
assert(returnType != null, "Invalid return type in $this.");
663-
assert(!parameterTypes.contains(null), "Invalid parameter types in $this.");
662+
assert(returnType != null, 'Invalid return type in $this.');
663+
assert(!parameterTypes.contains(null), 'Invalid parameter types in $this.');
664664
assert(!optionalParameterTypes.contains(null),
665-
"Invalid optional parameter types in $this.");
665+
'Invalid optional parameter types in $this.');
666666
assert(
667-
!namedParameters.contains(null), "Invalid named parameters in $this.");
667+
!namedParameters.contains(null), 'Invalid named parameters in $this.');
668668
assert(!requiredNamedParameters.contains(null),
669-
"Invalid required named parameters in $this.");
669+
'Invalid required named parameters in $this.');
670670
assert(!namedParameterTypes.contains(null),
671-
"Invalid named parameter types in $this.");
672-
assert(!typeVariables.contains(null), "Invalid type variables in $this.");
671+
'Invalid named parameter types in $this.');
672+
assert(!typeVariables.contains(null), 'Invalid type variables in $this.');
673+
}
674+
675+
factory FunctionType._(
676+
DartType returnType,
677+
List<DartType> parameterTypes,
678+
List<DartType> optionalParameterTypes,
679+
List<String> namedParameters,
680+
Set<String> requiredNamedParameters,
681+
List<DartType> namedParameterTypes,
682+
List<FunctionTypeVariable> typeVariables) {
683+
// Canonicalize empty collections to constants to save storage.
684+
if (parameterTypes.isEmpty) parameterTypes = const [];
685+
if (optionalParameterTypes.isEmpty) optionalParameterTypes = const [];
686+
if (namedParameterTypes.isEmpty) namedParameterTypes = const [];
687+
if (requiredNamedParameters.isEmpty) requiredNamedParameters = const {};
688+
if (typeVariables.isEmpty) typeVariables = const [];
689+
690+
return FunctionType._allocate(
691+
returnType,
692+
parameterTypes,
693+
optionalParameterTypes,
694+
namedParameters,
695+
requiredNamedParameters,
696+
namedParameterTypes,
697+
typeVariables);
673698
}
674699

675700
factory FunctionType._readFromDataSource(

0 commit comments

Comments
 (0)