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

Unified Diff: src/objects-inl.h

Issue 6525: Calculate string hash during flattening. (Closed)
Patch Set: Created 12 years, 2 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
« src/objects.cc ('K') | « src/objects.cc ('k') | src/runtime.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/objects-inl.h
diff --git a/src/objects-inl.h b/src/objects-inl.h
index decaf392f6d999002e9d9db5eaaf22f587e100de..ffc6f66e8f4e71166feed7d6f47fa31cac38c05e 100644
--- a/src/objects-inl.h
+++ b/src/objects-inl.h
@@ -2088,6 +2088,65 @@ uint32_t String::Hash() {
}
+StringHasher::StringHasher(int length)
+ : length_(length),
+ raw_running_hash_(0),
+ array_index_(0),
+ is_array_index_(0 < length_ && length_ <= String::kMaxArrayIndexSize),
+ is_first_char_(true),
+ is_valid_(true) { }
+
+
+bool StringHasher::has_trivial_hash() {
+ return length_ > String::kMaxMediumStringSize;
+}
+
+
+void StringHasher::AddCharacter(uc32 c) {
+ // Note: the Jenkins one-at-a-time hash function
+ raw_running_hash_ += c;
+ raw_running_hash_ += (raw_running_hash_ << 10);
+ raw_running_hash_ ^= (raw_running_hash_ >> 6);
+ // Incremental array index computation
+ if (is_array_index_) {
+ if (c < '0' || c > '9') {
+ is_array_index_ = false;
+ } else {
+ int d = c - '0';
+ if (is_first_char_) {
+ is_first_char_ = false;
+ if (c == '0' && length_ > 1) {
+ is_array_index_ = false;
+ return;
+ }
+ }
+ if (array_index_ > 429496729U - ((d > 5) ? 1 : 0)) {
Erik Corry 2008/10/07 10:04:34 (d + 2) >> 3 will probably avoid an unpredictable
+ is_array_index_ = false;
+ } else {
+ array_index_ = array_index_ * 10 + d;
+ }
+ }
+ }
+}
+
+
+void StringHasher::AddCharacterNoIndex(uc32 c) {
+ ASSERT(!is_array_index());
+ raw_running_hash_ += c;
+ raw_running_hash_ += (raw_running_hash_ << 10);
+ raw_running_hash_ ^= (raw_running_hash_ >> 6);
+}
+
+
+uint32_t StringHasher::GetHash() {
+ uint32_t result = raw_running_hash_;
+ result += (result << 3);
+ result ^= (result >> 11);
+ result += (result << 15);
+ return result;
+}
+
+
bool String::AsArrayIndex(uint32_t* index) {
uint32_t field = length_field();
if ((field & kHashComputedMask) && !(field & kIsArrayIndexMask)) return false;
« src/objects.cc ('K') | « src/objects.cc ('k') | src/runtime.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698