Skip to content

Commit fac7b19

Browse files
jensjohaCommit Queue
authored andcommitted
[analyzer] [_fe_analyzer_shared] Add Stack.popNonNullableNewList; use in popTypedList2
When analyzing the CFE with StableAnalysis in AOT with GC disabled: * This CL reduces the combined cost of `popTypedList2` from ~501 million instructions to ~150 million instructions according to `valgrind --tool=callgrind`. * Total savings, via `valgrind --tool=callgrind`, is reported as ~359 million instructions. * Total savings, via `perf stat`, is reported as ~367 million instructions (see also below). A benchmark run with 5 runs each gives this (but note that the normal gc runs should mostly be ignored): With normal GC: ``` page-faults:u: 2.5544% +/- 0.0884% (4895.60 +/- 169.43) (191651.60 -> 196547.20) instructions:u: -1.7106% +/- 0.0039% (-990609348.00 +/- 2286578.79) (57909229892.40 -> 56918620544.40) branch-misses:u: -4.0211% +/- 2.2322% (-7537436.00 +/- 4184151.25) (187447442.80 -> 179910006.80) maxRssKbytes: -1.1530% +/- 0.0148% (-7169.60 +/- 91.81) (621801.60 -> 614632.00) maxRssBytes: -1.1530% +/- 0.0148% (-7341670.40 +/- 94008.95) (636724838.40 -> 629383168.00) Comparing GC data: Scavenge( new space) goes from 171 to 169 MarkSweep( promotion) goes from 17 to 16 MarkSweep( old space) goes from 0 to 1 Notice combined GC time goes from 3636 ms to 3507 ms (notice only 1 run each). ``` ``` With GC disabled: page-faults:u: -1.7322% +/- 0.0001% (-21490.20 +/- 1.49) (1240629.60 -> 1219139.40) instructions:u: -0.9185% +/- 0.0021% (-367119249.20 +/- 829664.80) (39970048410.80 -> 39602929161.60) maxRssKbytes: -1.7226% +/- 0.0012% (-85733.60 +/- 60.60) (4977076.00 -> 4891342.40) maxRssBytes: -1.7226% +/- 0.0012% (-87791206.40 +/- 62052.71) (5096525824.00 -> 5008734617.60) ``` Change-Id: I85eae091cc45d5fb2820fbd263d022619c9875f6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/490122 Reviewed-by: Johnni Winther <johnniwinther@google.com> Commit-Queue: Jens Johansen <jensj@google.com>
1 parent 9d969a1 commit fac7b19

2 files changed

Lines changed: 28 additions & 7 deletions

File tree

pkg/_fe_analyzer_shared/lib/src/parser/stack_listener.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,9 @@ abstract class Stack {
499499
/// Returns `null` if a [ParserRecovery] value is found, or [list] otherwise.
500500
List<T>? popNonNullableList<T>(int count, List<T> list);
501501

502+
/// Pops [count] elements from the stack and puts it into the returned list.
503+
List<T> popNonNullableNewList<T>(int count);
504+
502505
void push(Object value);
503506

504507
/// Will return `null` instead of [NullValue].
@@ -628,6 +631,22 @@ class StackImpl implements Stack {
628631
return isParserRecovery ? null : list;
629632
}
630633

634+
@override
635+
List<T> popNonNullableNewList<T>(int count) {
636+
assert(arrayLength >= count);
637+
final List<Object?> array = this.array;
638+
final int length = arrayLength;
639+
final int startIndex = length - count;
640+
List<T> result = new List.generate(count, ((index) {
641+
int arrayIndex = startIndex + index;
642+
final Object? value = array[arrayIndex];
643+
array[arrayIndex] = null;
644+
return value as T;
645+
}));
646+
arrayLength -= count;
647+
return result;
648+
}
649+
631650
@override
632651
List<Object?> get values {
633652
final int length = arrayLength;
@@ -707,6 +726,14 @@ class DebugStack implements Stack {
707726
return result;
708727
}
709728

729+
@override
730+
List<T> popNonNullableNewList<T>(int count) {
731+
List<T> result = realStack.popNonNullableNewList(count);
732+
latestStacktraces.length = count;
733+
stackTraceStack.popList(count, latestStacktraces, /* nullValue = */ null);
734+
return result;
735+
}
736+
710737
@override
711738
void push(Object value) {
712739
realStack.push(value);

pkg/analyzer/lib/src/fasta/ast_builder.dart

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5817,14 +5817,8 @@ class AstBuilder extends StackListener {
58175817
return tailList.nonNulls.toList();
58185818
}
58195819

5820-
// TODO(scheglov): This is probably not optimal.
58215820
List<T> popTypedList2<T>(int count) {
5822-
var result = <T>[];
5823-
for (var i = 0; i < count; i++) {
5824-
var element = stack.pop(null) as T;
5825-
result.add(element);
5826-
}
5827-
return result.reversed.toList();
5821+
return stack.popNonNullableNewList<T>(count);
58285822
}
58295823

58305824
void reportErrorIfNullableType(Token? questionMark) {

0 commit comments

Comments
 (0)