string-hasher-inl.h 5.11 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_STRING_HASHER_INL_H_
#define V8_STRING_HASHER_INL_H_

8 9
#include "src/string-hasher.h"

10
#include "src/char-predicates-inl.h"
11
#include "src/objects.h"
12
#include "src/objects/string-inl.h"
13
#include "src/utils-inl.h"
14 15 16 17

namespace v8 {
namespace internal {

Yang Guo's avatar
Yang Guo committed
18
StringHasher::StringHasher(int length, uint64_t seed)
19
    : length_(length),
Yang Guo's avatar
Yang Guo committed
20
      raw_running_hash_(static_cast<uint32_t>(seed)),
21
      array_index_(0),
22
      is_array_index_(IsInRange(length, 1, String::kMaxArrayIndexSize)) {
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
  DCHECK(FLAG_randomize_hashes || raw_running_hash_ == 0);
}

bool StringHasher::has_trivial_hash() {
  return length_ > String::kMaxHashCalcLength;
}

uint32_t StringHasher::AddCharacterCore(uint32_t running_hash, uint16_t c) {
  running_hash += c;
  running_hash += (running_hash << 10);
  running_hash ^= (running_hash >> 6);
  return running_hash;
}

uint32_t StringHasher::GetHashCore(uint32_t running_hash) {
  running_hash += (running_hash << 3);
  running_hash ^= (running_hash >> 11);
  running_hash += (running_hash << 15);
41 42 43
  int32_t hash = static_cast<int32_t>(running_hash & String::kHashBitMask);
  int32_t mask = (hash - 1) >> 31;
  return running_hash | (kZeroHash & mask);
44 45
}

46
template <typename Char>
47
uint32_t StringHasher::ComputeRunningHash(uint32_t running_hash,
48 49 50 51 52
                                          const Char* chars, int length) {
  DCHECK_LE(0, length);
  DCHECK_IMPLIES(0 < length, chars != nullptr);
  const Char* end = &chars[length];
  while (chars != end) {
53 54 55 56 57 58 59 60 61 62 63 64 65
    running_hash = AddCharacterCore(running_hash, *chars++);
  }
  return running_hash;
}

void StringHasher::AddCharacter(uint16_t c) {
  // Use the Jenkins one-at-a-time hash function to update the hash
  // for the given character.
  raw_running_hash_ = AddCharacterCore(raw_running_hash_, c);
}

bool StringHasher::UpdateIndex(uint16_t c) {
  DCHECK(is_array_index_);
66
  if (!TryAddIndexChar(&array_index_, c)) {
67 68 69
    is_array_index_ = false;
    return false;
  }
70 71
  is_array_index_ = array_index_ != 0 || length_ == 1;
  return is_array_index_;
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
}

template <typename Char>
inline void StringHasher::AddCharacters(const Char* chars, int length) {
  DCHECK(sizeof(Char) == 1 || sizeof(Char) == 2);
  int i = 0;
  if (is_array_index_) {
    for (; i < length; i++) {
      AddCharacter(chars[i]);
      if (!UpdateIndex(chars[i])) {
        i++;
        break;
      }
    }
  }
87 88
  raw_running_hash_ =
      ComputeRunningHash(raw_running_hash_, &chars[i], length - i);
89 90 91 92
}

template <typename schar>
uint32_t StringHasher::HashSequentialString(const schar* chars, int length,
Yang Guo's avatar
Yang Guo committed
93
                                            uint64_t seed) {
94
#ifdef DEBUG
95 96
  StringHasher hasher(length, seed);
  if (!hasher.has_trivial_hash()) hasher.AddCharacters(chars, length);
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
  uint32_t expected = hasher.GetHashField();
#endif

  // Check whether the string is a valid array index. In that case, compute the
  // array index hash. It'll fall through to compute a regular string hash from
  // the start if it turns out that the string isn't a valid array index.
  if (IsInRange(length, 1, String::kMaxArrayIndexSize)) {
    if (IsDecimalDigit(chars[0]) && (length == 1 || chars[0] != '0')) {
      uint32_t index = chars[0] - '0';
      int i = 1;
      do {
        if (i == length) {
          uint32_t result = MakeArrayIndexHash(index, length);
          DCHECK_EQ(expected, result);
          return result;
        }
      } while (TryAddIndexChar(&index, chars[i++]));
    }
  } else if (length > String::kMaxHashCalcLength) {
    // String hash of a large string is simply the length.
    uint32_t result =
        (length << String::kHashShift) | String::kIsNotArrayIndexMask;
    DCHECK_EQ(result, expected);
    return result;
  }

  // Non-array-index hash.
  uint32_t hash =
      ComputeRunningHash(static_cast<uint32_t>(seed), chars, length);

  uint32_t result =
      (GetHashCore(hash) << String::kHashShift) | String::kIsNotArrayIndexMask;
  DCHECK_EQ(result, expected);
  return result;
131 132
}

Yang Guo's avatar
Yang Guo committed
133
IteratingStringHasher::IteratingStringHasher(int len, uint64_t seed)
134 135
    : StringHasher(len, seed) {}

136
uint32_t IteratingStringHasher::Hash(String string, uint64_t seed) {
137 138 139
  IteratingStringHasher hasher(string->length(), seed);
  // Nothing to do.
  if (hasher.has_trivial_hash()) return hasher.GetHashField();
140 141
  ConsString cons_string = String::VisitFlat(&hasher, string);
  if (cons_string.is_null()) return hasher.GetHashField();
142 143 144 145 146 147 148 149 150 151 152 153 154 155
  hasher.VisitConsString(cons_string);
  return hasher.GetHashField();
}

void IteratingStringHasher::VisitOneByteString(const uint8_t* chars,
                                               int length) {
  AddCharacters(chars, length);
}

void IteratingStringHasher::VisitTwoByteString(const uint16_t* chars,
                                               int length) {
  AddCharacters(chars, length);
}

156 157 158 159 160
std::size_t SeededStringHasher::operator()(const char* name) const {
  return StringHasher::HashSequentialString(
      name, static_cast<int>(strlen(name)), hashseed_);
}

161 162 163 164
}  // namespace internal
}  // namespace v8

#endif  // V8_STRING_HASHER_INL_H_