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

Unified Diff: sdk/lib/core/uri.dart

Issue 2119833002: Cache hashCode in Uri implementations to improve performance when used as, e.g., Map key. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 4 years, 6 months 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 | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/core/uri.dart
diff --git a/sdk/lib/core/uri.dart b/sdk/lib/core/uri.dart
index a01b6701df1498407e28a369bec6650d1e13f320..17f290d14e9be14108050c037aac48ff94348329 100644
--- a/sdk/lib/core/uri.dart
+++ b/sdk/lib/core/uri.dart
@@ -1370,6 +1370,13 @@ class _Uri implements Uri {
String _text;
/**
+ * Cache of the hashCode of [_text].
+ *
+ * Is null until computed.
+ */
+ int _hashCodeCache;
+
+ /**
* Cache the computed return value of [queryParameters].
*/
Map<String, String> _queryParameters;
@@ -2580,7 +2587,11 @@ class _Uri implements Uri {
UriData get data => (scheme == "data") ? new UriData.fromUri(this) : null;
String toString() {
- if (_text != null) return _text;
+ return _text ??= _initializeText();
+ }
+
+ String _initializeText() {
+ assert(_text == null);
StringBuffer sb = new StringBuffer();
if (scheme.isNotEmpty) sb..write(scheme)..write(":");
if (hasAuthority || path.startsWith("//") || (scheme == "file")) {
@@ -2592,8 +2603,7 @@ class _Uri implements Uri {
sb.write(path);
if (_query != null) sb..write("?")..write(_query);
if (_fragment != null) sb..write("#")..write(_fragment);
- _text = sb.toString();
- return _text;
+ return sb.toString();
}
bool operator==(other) {
@@ -2615,7 +2625,7 @@ class _Uri implements Uri {
}
int get hashCode {
- return (_text ?? toString()).hashCode;
+ return _hashCodeCache ??= toString().hashCode;
}
static List _createList() => [];
@@ -4002,6 +4012,7 @@ class _SimpleUri implements Uri {
/// To make comparisons more efficient, we cache the value, and
/// canonicalize a few known types.
String _schemeCache;
+ int _hashCodeCache;
_SimpleUri(
this._uri,
@@ -4409,7 +4420,7 @@ class _SimpleUri implements Uri {
return null;
}
- int get hashCode => _uri.hashCode;
+ int get hashCode => _hashCodeCache ??= _uri.hashCode;
bool operator==(Object other) {
if (identical(this, other)) return true;
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698