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

Unified Diff: pkg/analyzer/lib/src/util/fast_uri.dart

Issue 2284483002: Detect the VM's Uri hashCode computation algorithm and use it in FastUri. (Closed)
Patch Set: Created 4 years, 4 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: pkg/analyzer/lib/src/util/fast_uri.dart
diff --git a/pkg/analyzer/lib/src/util/fast_uri.dart b/pkg/analyzer/lib/src/util/fast_uri.dart
index 3e1d733c1c7ec5345dd9dae0015c742157f0a9ac..6827d4f915f7c1a43f7a119950afb6b595265103 100644
--- a/pkg/analyzer/lib/src/util/fast_uri.dart
+++ b/pkg/analyzer/lib/src/util/fast_uri.dart
@@ -19,6 +19,8 @@ class FastUri implements Uri {
static int _currentCacheLength = 0;
static int _currentCacheGeneration = 0;
+ static bool _hashUsingText = _shouldComputeHashCodeUsingText();
+
final int _cacheGeneration;
final String _text;
final String _scheme;
@@ -30,6 +32,11 @@ class FastUri implements Uri {
*/
final int _lastSlashIndex;
+ /**
+ * The cached hash code.
+ */
+ int _hashCode;
+
Uri _cachedFallbackUri;
FastUri._(this._cacheGeneration, this._text, this._scheme,
@@ -57,7 +64,11 @@ class FastUri implements Uri {
bool get hasFragment => false;
@override
- int get hashCode => _text.hashCode;
+ int get hashCode {
+ return _hashCode ??= _hashUsingText
+ ? _computeHashUsingText(this)
+ : _computeHashUsingCombine(this);
+ }
@override
bool get hasPort => false;
@@ -217,6 +228,38 @@ class FastUri implements Uri {
return uri;
}
+ /**
+ * This implementation was used before 'fast-URI' in Dart VM.
+ */
+ static int _computeHashUsingCombine(FastUri uri) {
+ // This code is copied from the standard Uri implementation.
+ // It is important that Uri and FastUri generate compatible hashCodes
+ // because Uri and FastUri may be used as keys in the same map.
+ int combine(part, current) {
+ // The sum is truncated to 30 bits to make sure it fits into a Smi.
+ return (current * 31 + part.hashCode) & 0x3FFFFFFF;
+ }
+
+ return combine(
+ uri.scheme,
+ combine(
+ uri.userInfo,
+ combine(
+ uri.host,
+ combine(
+ uri.port,
+ combine(uri.path,
+ combine(uri.query, combine(uri.fragment, 1)))))));
+ }
+
+ /**
+ * This implementation should be used with 'fast-URI' in Dart VM.
+ * https://github.com/dart-lang/sdk/commit/afbbbb97cfcd86a64d0ba5dcfe1ab758954adaf4
+ */
+ static int _computeHashUsingText(FastUri uri) {
+ return uri._text.hashCode;
+ }
+
static bool _isAlphabetic(int char) {
return char >= 'A'.codeUnitAt(0) && char <= 'Z'.codeUnitAt(0) ||
char >= 'a'.codeUnitAt(0) && char <= 'z'.codeUnitAt(0);
@@ -267,4 +310,13 @@ class FastUri implements Uri {
return new FastUri._(_currentCacheGeneration, text, scheme,
hasEmptyAuthority, path, lastSlashIndex);
}
+
+ /**
+ * Determine whether VM has the text based hash code computation in [Uri],
+ * or the old combine style.
+ */
+ static bool _shouldComputeHashCodeUsingText() {
+ String text = 'package:foo/foo.dart';
+ return Uri.parse(text).hashCode == text.hashCode;
+ }
}
« 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