Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(132)

Unified Diff: lib/path.dart

Issue 1473563003: Cache the current working directory. (Closed) Base URL: git@github.com:dart-lang/path@master
Patch Set: Code review changes Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « benchmark/benchmark.dart ('k') | lib/src/context.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/path.dart
diff --git a/lib/path.dart b/lib/path.dart
index af9efe54e849ded0200f1f45b469c32ac3c35bed..93fe67e4125020bfaaa446d4ec1217620c56e0e1 100644
--- a/lib/path.dart
+++ b/lib/path.dart
@@ -79,17 +79,36 @@ Style get style => context.style;
/// In the browser, this means the current URL, without the last file segment.
String get current {
var uri = Uri.base;
+
+ // Converting the base URI to a file path is pretty slow, and the base URI
+ // rarely changes in practice, so we cache the result here.
+ if (uri == _currentUriBase) return _current;
+ _currentUriBase = uri;
+
if (Style.platform == Style.url) {
- return uri.resolve('.').toString();
+ _current = uri.resolve('.').toString();
Lasse Reichstein Nielsen 2015/12/01 06:52:58 That is likely to be slower than necessary. Instea
Lasse Reichstein Nielsen 2015/12/01 06:53:54 (That does assume that Uri.base has no query or fr
nweiz 2015/12/01 20:50:46 Given that this is cached now, I think it's probab
+ return _current;
} else {
var path = uri.toFilePath();
// Remove trailing '/' or '\'.
- int lastIndex = path.length - 1;
+ var lastIndex = path.length - 1;
assert(path[lastIndex] == '/' || path[lastIndex] == '\\');
- return path.substring(0, lastIndex);
+ _current = path.substring(0, lastIndex);
+ return _current;
}
}
+/// The last value returned by [Uri.base].
+///
+/// This is used to cache the current working directory.
+Uri _currentUriBase;
+
+/// The last known value of the current working directory.
+///
+/// This is cached because [current] is called frequently but rarely actually
+/// changes.
+String _current;
+
/// Gets the path separator for the current platform. This is `\` on Windows
/// and `/` on other platforms (including the browser).
String get separator => context.separator;
« no previous file with comments | « benchmark/benchmark.dart ('k') | lib/src/context.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698