Chromium Code Reviews| 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; |