Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit d127ab9

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Get rid of places that allocate lots of _Closure instances.
One huge source of them was checking for @VisibleForTesting, @Protected, etc. I extracted it into _InvalidAccessVerifier and refactored for performance and clarity. We probably get 3-4% speed up on my Flutter benchmark. R=brianwilkerson@google.com Change-Id: I6d0dce9a10c41595bc2245d45dafad7ae2f8b837 Reviewed-on: https://dart-review.googlesource.com/c/79478 Commit-Queue: Konstantin Shcheglov <scheglov@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
1 parent 5cff16b commit d127ab9

2 files changed

Lines changed: 324 additions & 260 deletions

File tree

pkg/analyzer/lib/src/dart/element/element.dart

Lines changed: 123 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2579,16 +2579,40 @@ abstract class ElementImpl implements Element {
25792579
}
25802580

25812581
@override
2582-
bool get hasAlwaysThrows =>
2583-
metadata.any((ElementAnnotation annotation) => annotation.isAlwaysThrows);
2582+
bool get hasAlwaysThrows {
2583+
var metadata = this.metadata;
2584+
for (var i = 0; i < metadata.length; i++) {
2585+
var annotation = metadata[i];
2586+
if (annotation.isAlwaysThrows) {
2587+
return true;
2588+
}
2589+
}
2590+
return false;
2591+
}
25842592

25852593
@override
2586-
bool get hasDeprecated =>
2587-
metadata.any((ElementAnnotation annotation) => annotation.isDeprecated);
2594+
bool get hasDeprecated {
2595+
var metadata = this.metadata;
2596+
for (var i = 0; i < metadata.length; i++) {
2597+
var annotation = metadata[i];
2598+
if (annotation.isDeprecated) {
2599+
return true;
2600+
}
2601+
}
2602+
return false;
2603+
}
25882604

25892605
@override
2590-
bool get hasFactory =>
2591-
metadata.any((ElementAnnotation annotation) => annotation.isFactory);
2606+
bool get hasFactory {
2607+
var metadata = this.metadata;
2608+
for (var i = 0; i < metadata.length; i++) {
2609+
var annotation = metadata[i];
2610+
if (annotation.isFactory) {
2611+
return true;
2612+
}
2613+
}
2614+
return false;
2615+
}
25922616

25932617
@override
25942618
int get hashCode {
@@ -2601,89 +2625,132 @@ abstract class ElementImpl implements Element {
26012625
}
26022626

26032627
@override
2604-
bool get hasIsTest =>
2605-
metadata.any((ElementAnnotation annotation) => annotation.isIsTest);
2606-
2607-
@override
2608-
bool get hasIsTestGroup =>
2609-
metadata.any((ElementAnnotation annotation) => annotation.isIsTestGroup);
2610-
2611-
@override
2612-
bool get hasJS =>
2613-
metadata.any((ElementAnnotation annotation) => annotation.isJS);
2614-
2615-
@override
2616-
bool get hasOverride =>
2617-
metadata.any((ElementAnnotation annotation) => annotation.isOverride);
2618-
2619-
@override
2620-
bool get hasProtected =>
2621-
metadata.any((ElementAnnotation annotation) => annotation.isProtected);
2622-
2623-
@override
2624-
bool get hasRequired =>
2625-
metadata.any((ElementAnnotation annotation) => annotation.isRequired);
2628+
bool get hasIsTest {
2629+
var metadata = this.metadata;
2630+
for (var i = 0; i < metadata.length; i++) {
2631+
var annotation = metadata[i];
2632+
if (annotation.isIsTest) {
2633+
return true;
2634+
}
2635+
}
2636+
return false;
2637+
}
26262638

26272639
@override
2628-
bool get hasSealed =>
2629-
metadata.any((ElementAnnotation annotation) => annotation.isSealed);
2640+
bool get hasIsTestGroup {
2641+
var metadata = this.metadata;
2642+
for (var i = 0; i < metadata.length; i++) {
2643+
var annotation = metadata[i];
2644+
if (annotation.isIsTestGroup) {
2645+
return true;
2646+
}
2647+
}
2648+
return false;
2649+
}
26302650

26312651
@override
2632-
bool get hasVisibleForTemplate => metadata
2633-
.any((ElementAnnotation annotation) => annotation.isVisibleForTemplate);
2652+
bool get hasJS {
2653+
var metadata = this.metadata;
2654+
for (var i = 0; i < metadata.length; i++) {
2655+
var annotation = metadata[i];
2656+
if (annotation.isJS) {
2657+
return true;
2658+
}
2659+
}
2660+
return false;
2661+
}
26342662

26352663
@override
2636-
bool get hasVisibleForTesting => metadata
2637-
.any((ElementAnnotation annotation) => annotation.isVisibleForTesting);
2638-
2639-
/// Return an identifier that uniquely identifies this element among the
2640-
/// children of this element's parent.
2641-
String get identifier => name;
2664+
bool get hasOverride {
2665+
var metadata = this.metadata;
2666+
for (var i = 0; i < metadata.length; i++) {
2667+
var annotation = metadata[i];
2668+
if (annotation.isOverride) {
2669+
return true;
2670+
}
2671+
}
2672+
return false;
2673+
}
26422674

26432675
@override
2644-
bool get isAlwaysThrows =>
2645-
metadata.any((ElementAnnotation annotation) => annotation.isAlwaysThrows);
2676+
bool get hasProtected {
2677+
var metadata = this.metadata;
2678+
for (var i = 0; i < metadata.length; i++) {
2679+
var annotation = metadata[i];
2680+
if (annotation.isProtected) {
2681+
return true;
2682+
}
2683+
}
2684+
return false;
2685+
}
26462686

26472687
@override
2648-
bool get isDeprecated {
2649-
for (ElementAnnotation annotation in metadata) {
2650-
if (annotation.isDeprecated) {
2688+
bool get hasRequired {
2689+
var metadata = this.metadata;
2690+
for (var i = 0; i < metadata.length; i++) {
2691+
var annotation = metadata[i];
2692+
if (annotation.isRequired) {
26512693
return true;
26522694
}
26532695
}
26542696
return false;
26552697
}
26562698

26572699
@override
2658-
bool get isFactory {
2659-
for (ElementAnnotation annotation in metadata) {
2660-
if (annotation.isFactory) {
2700+
bool get hasSealed {
2701+
var metadata = this.metadata;
2702+
for (var i = 0; i < metadata.length; i++) {
2703+
var annotation = metadata[i];
2704+
if (annotation.isSealed) {
26612705
return true;
26622706
}
26632707
}
26642708
return false;
26652709
}
26662710

26672711
@override
2668-
bool get isJS {
2669-
for (ElementAnnotation annotation in metadata) {
2670-
if (annotation.isJS) {
2712+
bool get hasVisibleForTemplate {
2713+
var metadata = this.metadata;
2714+
for (var i = 0; i < metadata.length; i++) {
2715+
var annotation = metadata[i];
2716+
if (annotation.isVisibleForTemplate) {
26712717
return true;
26722718
}
26732719
}
26742720
return false;
26752721
}
26762722

26772723
@override
2678-
bool get isOverride {
2679-
for (ElementAnnotation annotation in metadata) {
2680-
if (annotation.isOverride) {
2724+
bool get hasVisibleForTesting {
2725+
var metadata = this.metadata;
2726+
for (var i = 0; i < metadata.length; i++) {
2727+
var annotation = metadata[i];
2728+
if (annotation.isVisibleForTesting) {
26812729
return true;
26822730
}
26832731
}
26842732
return false;
26852733
}
26862734

2735+
/// Return an identifier that uniquely identifies this element among the
2736+
/// children of this element's parent.
2737+
String get identifier => name;
2738+
2739+
@override
2740+
bool get isAlwaysThrows => hasAlwaysThrows;
2741+
2742+
@override
2743+
bool get isDeprecated => hasDeprecated;
2744+
2745+
@override
2746+
bool get isFactory => hasFactory;
2747+
2748+
@override
2749+
bool get isJS => hasJS;
2750+
2751+
@override
2752+
bool get isOverride => hasOverride;
2753+
26872754
@override
26882755
bool get isPrivate {
26892756
String name = displayName;
@@ -2694,27 +2761,13 @@ abstract class ElementImpl implements Element {
26942761
}
26952762

26962763
@override
2697-
bool get isProtected {
2698-
for (ElementAnnotation annotation in metadata) {
2699-
if (annotation.isProtected) {
2700-
return true;
2701-
}
2702-
}
2703-
return false;
2704-
}
2764+
bool get isProtected => hasProtected;
27052765

27062766
@override
27072767
bool get isPublic => !isPrivate;
27082768

27092769
@override
2710-
bool get isRequired {
2711-
for (ElementAnnotation annotation in metadata) {
2712-
if (annotation.isRequired) {
2713-
return true;
2714-
}
2715-
}
2716-
return false;
2717-
}
2770+
bool get isRequired => hasRequired;
27182771

27192772
/// Return `true` if this element is resynthesized from a summary.
27202773
bool get isResynthesized => enclosingUnit?.resynthesizerContext != null;
@@ -2727,12 +2780,8 @@ abstract class ElementImpl implements Element {
27272780
setModifier(Modifier.SYNTHETIC, isSynthetic);
27282781
}
27292782

2730-
bool get isVisibleForTemplate => metadata
2731-
.any((ElementAnnotation annotation) => annotation.isVisibleForTemplate);
2732-
27332783
@override
2734-
bool get isVisibleForTesting => metadata
2735-
.any((ElementAnnotation annotation) => annotation.isVisibleForTesting);
2784+
bool get isVisibleForTesting => hasVisibleForTesting;
27362785

27372786
@override
27382787
LibraryElement get library =>
@@ -2968,7 +3017,7 @@ abstract class ElementImpl implements Element {
29683017

29693018
/// If the given [type] is a generic function type, then the element
29703019
/// associated with the type is implicitly a child of this element and should
2971-
/// be visted by the given [visitor].
3020+
/// be visited by the given [visitor].
29723021
void _safelyVisitPossibleChild(DartType type, ElementVisitor visitor) {
29733022
Element element = type?.element;
29743023
if (element is GenericFunctionTypeElementImpl &&

0 commit comments

Comments
 (0)