Skip to content

Commit 88db201

Browse files
jensjohaCommit Queue
authored andcommitted
[analyzer] Replace pathContext.relative inside LocatedGlob.matches
Comparing a before and an after aot-compiled analysis server, running each 25 times, via `pkg/front_end/tool/benchmarker.dart`: ``` out/ReleaseX64/dart pkg/front_end/tool/benchmarker.dart \ --iterations=25 \ --snapshot=pkg/analysis_server/bin/server.aot.1 \ --snapshot=pkg/analysis_server/bin/server.aot.2 \ --arguments="--disable-file-byte-store" \ --arguments="--train-using" \ --arguments="pkg/front_end/lib/" ``` (where `server.aot.1` is an aot-compile without this CL and `server.aot.2` is an aot-compile with this CL) gets this result: ``` msec task-clock:u: -5.2977% +/- 0.4313% (-466.26 +/- 37.96) (8801.11 -> 8334.86) page-faults:u: -2.5748% +/- 0.1648% (-4094.76 +/- 262.07) (159032.16 -> 154937.40) cycles:u: -5.5212% +/- 0.4440% (-2034156211.44 +/- 163567376.21) (36842412971.36 -> 34808256759.92) instructions:u: -8.2456% +/- 0.0556% (-4125031082.64 +/- 27819990.07) (50026884750.04 -> 45901853667.40) branch-misses:u: -4.2637% +/- 2.0984% (-5839704.08 +/- 2873998.26) (136961902.80 -> 131122198.72) seconds time elapsed: -5.2895% +/- 0.4311% (-0.47 +/- 0.04) (8.82 -> 8.35) seconds user: -5.5473% +/- 0.5002% (-0.47 +/- 0.04) (8.46 -> 7.99) Scavenge( new space) goes from 152 to 134 MarkSweep( promotion) goes from 15 to 14 Notice combined GC time goes from 2804 ms to 2707 ms (notice only 1 run each). ``` Change-Id: Ib5070b3c037b30f96bb91b3536d85b85ebe0c30c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/427442 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Jens Johansen <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 929d058 commit 88db201

File tree

4 files changed

+92
-6
lines changed

4 files changed

+92
-6
lines changed

pkg/analyzer/api.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4538,6 +4538,7 @@ package:analyzer/file_system/file_system.dart:
45384538
ready (getter: Future<void>)
45394539
FolderExtension (extension on Folder):
45404540
withAncestors (getter: Iterable<Folder>)
4541+
relativeIfContains (method: String? Function(String))
45414542
package:analyzer/file_system/memory_file_system.dart:
45424543
MemoryResourceProvider (class extends Object implements ResourceProvider):
45434544
new (constructor: MemoryResourceProvider Function({Context? context, Duration? delayWatcherInitialization}))

pkg/analyzer/lib/file_system/file_system.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,31 @@ extension FolderExtension on Folder {
279279
current = current.parent;
280280
}
281281
}
282+
283+
/// Return a relative path if the [path] references a resource in this folder,
284+
/// or `null` if it doesn't.
285+
///
286+
/// The [path] must be absolute and normalized.
287+
String? relativeIfContains(String path) {
288+
// Given that paths are absolute and normalized the below is equivalent,
289+
// but much faster, than something like
290+
// ```
291+
// if (parent.contains(path)) {
292+
// return provider.pathContext.relative(path, from: this.path);
293+
// }
294+
// return null;
295+
// ```
296+
String parent = this.path;
297+
if (path.length <= parent.length) return null;
298+
int separator = path.codeUnitAt(parent.length);
299+
var style = provider.pathContext.style;
300+
if (!style.isSeparator(separator)) return null;
301+
for (var i = 0; i < parent.length; i++) {
302+
if (parent.codeUnitAt(i) != path.codeUnitAt(i) &&
303+
!style.codeUnitsEqual(parent.codeUnitAt(i), path.codeUnitAt(i))) {
304+
return null;
305+
}
306+
}
307+
return path.substring(parent.length + 1);
308+
}
282309
}

pkg/analyzer/lib/src/dart/analysis/context_root.dart

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,8 @@ class LocatedGlob {
183183
LocatedGlob(this.parent, this.glob);
184184

185185
bool matches(String path) {
186-
if (parent.contains(path)) {
187-
var pathContext = parent.provider.pathContext;
188-
var relativePath = pathContext.relative(path, from: parent.path);
189-
return glob.matches(relativePath);
190-
}
191-
return false;
186+
var relativePath = parent.relativeIfContains(path);
187+
if (relativePath == null) return false;
188+
return glob.matches(relativePath);
192189
}
193190
}

pkg/analyzer/test/file_system/file_system_test_support.dart

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,67 @@ mixin FolderTestMixin implements FileSystemTestSupport {
989989
}
990990
}
991991

992+
test_relative_if_contains_immediateChild() {
993+
Folder folder = getFolder(exists: false);
994+
expect(
995+
folder.relativeIfContains(join(defaultFolderPath, 'aaa.txt')),
996+
'aaa.txt',
997+
);
998+
}
999+
1000+
test_relative_if_contains_nestedChild() {
1001+
Folder folder = getFolder(exists: false);
1002+
expect(
1003+
folder.relativeIfContains(join(defaultFolderPath, 'aaa', 'bbb.txt')),
1004+
join('aaa', 'bbb.txt'),
1005+
);
1006+
}
1007+
1008+
test_relative_if_contains_os_specificic() {
1009+
Folder folder = getFolder(exists: false);
1010+
// On non-Windows path casing does matter.
1011+
String? expectOnCaseDifferenceImmediateChild;
1012+
String? expectOnCaseDifferenceNestedChild;
1013+
if (provider.pathContext.style.name == path.Style.windows.name) {
1014+
// On Windows path casing doesn't matter.
1015+
expectOnCaseDifferenceImmediateChild = 'aaa.txt';
1016+
expectOnCaseDifferenceNestedChild = join('aaa', 'bbb.txt');
1017+
}
1018+
1019+
var caseChangedFolder = defaultFolderPath.toUpperCase();
1020+
1021+
// Immediate child.
1022+
expect(
1023+
folder.relativeIfContains(join(caseChangedFolder, 'aaa.txt')),
1024+
expectOnCaseDifferenceImmediateChild,
1025+
);
1026+
1027+
// Nested child.
1028+
expect(
1029+
folder.relativeIfContains(join(caseChangedFolder, 'aaa', 'bbb.txt')),
1030+
expectOnCaseDifferenceNestedChild,
1031+
);
1032+
1033+
// Self (on Windows).
1034+
expect(folder.relativeIfContains(caseChangedFolder), isNull);
1035+
1036+
// Unrelated.
1037+
expect(
1038+
folder.relativeIfContains(join(tempPath.toUpperCase(), 'baz.txt')),
1039+
isNull,
1040+
);
1041+
}
1042+
1043+
test_relative_if_contains_self() {
1044+
Folder folder = getFolder(exists: false);
1045+
expect(folder.relativeIfContains(defaultFolderPath), isNull);
1046+
}
1047+
1048+
test_relative_if_contains_unrelated() {
1049+
Folder folder = getFolder(exists: false);
1050+
expect(folder.relativeIfContains(join(tempPath, 'baz.txt')), isNull);
1051+
}
1052+
9921053
test_resolveSymbolicLinksSync_links_existing() {
9931054
if (!hasSymbolicLinkSupport) return;
9941055

0 commit comments

Comments
 (0)