Skip to content

Commit 9202990

Browse files
jensjohaCommit Queue
authored andcommitted
[kernel] Scope calculator speedup
Running the test (once) goes from ~3:33 to ~1:59 on my machine. Functionally it should be the same. Change-Id: I84844cdac6948bd5ae0238681bc83bbf6bd48308 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/433721 Commit-Queue: Jens Johansen <[email protected]> Reviewed-by: Johnni Winther <[email protected]>
1 parent fd26223 commit 9202990

File tree

1 file changed

+32
-42
lines changed

1 file changed

+32
-42
lines changed

pkg/kernel/lib/dart_scope_calculator.dart

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ class DartScopeBuilder2 extends VisitorDefault<void> with VisitorVoidMixin {
264264
final Uri _scriptUri;
265265
final int _offset;
266266
final List<DartScope2> findScopes = [];
267-
final Set<int> foundOffsets = {};
267+
int? closestLessOrEqualFoundOffset;
268268

269269
final Set<VariableDeclaration> hoistedUnwritten = {};
270270
final List<List<VariableDeclaration>> scopes = [];
@@ -532,20 +532,32 @@ class DartScopeBuilder2 extends VisitorDefault<void> with VisitorVoidMixin {
532532
scopes.last.add(node);
533533
}
534534

535+
void _updateClosestFoundOffset(int offset) {
536+
if (offset > _offset) {
537+
// This offset is larger that the one we're looking for so we'll skip.
538+
return;
539+
}
540+
if (closestLessOrEqualFoundOffset == null ||
541+
offset > closestLessOrEqualFoundOffset!) {
542+
closestLessOrEqualFoundOffset = offset;
543+
return;
544+
}
545+
}
546+
535547
void _checkOffset(TreeNode node) {
536-
if (_currentUri == _scriptUri) {
537-
foundOffsets.add(node.fileOffset);
538-
if (node.fileOffset == _offset) {
539-
addFound(node);
540-
} else {
541-
List<int>? allOffsets = node.fileOffsetsIfMultiple;
542-
if (allOffsets != null) {
543-
for (final int offset in allOffsets) {
544-
foundOffsets.add(offset);
545-
if (offset == _offset) {
546-
addFound(node);
547-
break;
548-
}
548+
if (_currentUri != _scriptUri) return;
549+
550+
_updateClosestFoundOffset(node.fileOffset);
551+
if (node.fileOffset == _offset) {
552+
addFound(node);
553+
} else {
554+
List<int>? allOffsets = node.fileOffsetsIfMultiple;
555+
if (allOffsets != null) {
556+
for (final int offset in allOffsets) {
557+
_updateClosestFoundOffset(offset);
558+
if (offset == _offset) {
559+
addFound(node);
560+
break;
549561
}
550562
}
551563
}
@@ -556,36 +568,15 @@ class DartScopeBuilder2 extends VisitorDefault<void> with VisitorVoidMixin {
556568
Library library, Uri scriptUri, Class? cls, int offset) {
557569
DartScopeBuilder2 data = _raw(library, scriptUri, cls, offset);
558570
if (data.findScopes.isEmpty) {
559-
int? closestMatchingOrSmallerOffset =
560-
_findClosestMatchingOrSmallerOffset(data, offset);
561-
if (closestMatchingOrSmallerOffset != null) {
562-
offset = closestMatchingOrSmallerOffset;
571+
int? closestLessOrEqualFoundOffset = data.closestLessOrEqualFoundOffset;
572+
if (closestLessOrEqualFoundOffset != null) {
573+
offset = closestLessOrEqualFoundOffset;
563574
data = _raw(library, scriptUri, cls, offset);
564575
}
565576
}
566577
return _findScopePick(data.findScopes, library, cls, offset);
567578
}
568579

569-
static int? _findClosestMatchingOrSmallerOffset(
570-
DartScopeBuilder2 data, int offset) {
571-
List<int> foundOffsets = data.foundOffsets.toList()..sort();
572-
if (foundOffsets.isEmpty) return null;
573-
int low = 0;
574-
int high = foundOffsets.length - 1;
575-
while (low < high) {
576-
int mid = high - ((high - low) >> 1); // Get middle, rounding up.
577-
int pivot = foundOffsets[mid];
578-
if (pivot <= offset) {
579-
low = mid;
580-
} else {
581-
high = mid - 1;
582-
}
583-
}
584-
int result = foundOffsets[low];
585-
if (result < 0) return null;
586-
return result;
587-
}
588-
589580
static DartScope _findScopePick(
590581
List<DartScope2> scopes, Library library, Class? cls, int offset) {
591582
DartScope2 scope;
@@ -622,10 +613,9 @@ class DartScopeBuilder2 extends VisitorDefault<void> with VisitorVoidMixin {
622613
Library library, Uri scriptUri, int offset) {
623614
DartScopeBuilder2 data = _rawNoClass(library, scriptUri, offset);
624615
if (data.findScopes.isEmpty) {
625-
int? closestMatchingOrSmallerOffset =
626-
_findClosestMatchingOrSmallerOffset(data, offset);
627-
if (closestMatchingOrSmallerOffset != null) {
628-
offset = closestMatchingOrSmallerOffset;
616+
int? closestLessOrEqualFoundOffset = data.closestLessOrEqualFoundOffset;
617+
if (closestLessOrEqualFoundOffset != null) {
618+
offset = closestLessOrEqualFoundOffset;
629619
data = _rawNoClass(library, scriptUri, offset);
630620
}
631621
}

0 commit comments

Comments
 (0)