V8 Project
v8::internal::StringHasher Class Reference

#include <objects.h>

+ Inheritance diagram for v8::internal::StringHasher:
+ Collaboration diagram for v8::internal::StringHasher:

Public Member Functions

 StringHasher (int length, uint32_t seed)
 
 INLINE (static uint32_t AddCharacterCore(uint32_t running_hash, uint16_t c))
 
 INLINE (static uint32_t GetHashCore(uint32_t running_hash))
 

Static Public Member Functions

template<typename schar >
static uint32_t HashSequentialString (const schar *chars, int length, uint32_t seed)
 
static uint32_t ComputeUtf8Hash (Vector< const char > chars, uint32_t seed, int *utf16_length_out)
 
static uint32_t MakeArrayIndexHash (uint32_t value, int length)
 

Static Public Attributes

static const int kZeroHash = 27
 

Protected Member Functions

uint32_t GetHashField ()
 
bool has_trivial_hash ()
 
template<typename Char >
void AddCharacters (const Char *chars, int len)
 

Private Member Functions

void AddCharacter (uint16_t c)
 
bool UpdateIndex (uint16_t c)
 
 DISALLOW_COPY_AND_ASSIGN (StringHasher)
 

Private Attributes

int length_
 
uint32_t raw_running_hash_
 
uint32_t array_index_
 
bool is_array_index_
 
bool is_first_char_
 

Detailed Description

Definition at line 8341 of file objects.h.

Constructor & Destructor Documentation

◆ StringHasher()

v8::internal::StringHasher::StringHasher ( int  length,
uint32_t  seed 
)
inlineexplicit

Definition at line 6478 of file objects-inl.h.

6479  : length_(length),
6480  raw_running_hash_(seed),
6481  array_index_(0),
6483  is_first_char_(true) {
6484  DCHECK(FLAG_randomize_hashes || raw_running_hash_ == 0);
6485 }
static const int kMaxArrayIndexSize
Definition: objects.h:8807
#define DCHECK(condition)
Definition: logging.h:205

References DCHECK, and raw_running_hash_.

Member Function Documentation

◆ AddCharacter()

void v8::internal::StringHasher::AddCharacter ( uint16_t  c)
inlineprivate

Definition at line 6512 of file objects-inl.h.

6512  {
6513  // Use the Jenkins one-at-a-time hash function to update the hash
6514  // for the given character.
6515  raw_running_hash_ = AddCharacterCore(raw_running_hash_, c);
6516 }

References raw_running_hash_.

Referenced by AddCharacters(), and ComputeUtf8Hash().

+ Here is the caller graph for this function:

◆ AddCharacters()

template<typename Char >
void v8::internal::StringHasher::AddCharacters ( const Char *  chars,
int  len 
)
inlineprotected

Definition at line 6543 of file objects-inl.h.

6543  {
6544  DCHECK(sizeof(Char) == 1 || sizeof(Char) == 2);
6545  int i = 0;
6546  if (is_array_index_) {
6547  for (; i < length; i++) {
6548  AddCharacter(chars[i]);
6549  if (!UpdateIndex(chars[i])) {
6550  i++;
6551  break;
6552  }
6553  }
6554  }
6555  for (; i < length; i++) {
6557  AddCharacter(chars[i]);
6558  }
6559 }
void AddCharacter(uint16_t c)
Definition: objects-inl.h:6512
bool UpdateIndex(uint16_t c)
Definition: objects-inl.h:6519

References AddCharacter(), DCHECK, is_array_index_, and UpdateIndex().

Referenced by HashSequentialString(), v8::internal::IteratingStringHasher::VisitOneByteString(), and v8::internal::IteratingStringHasher::VisitTwoByteString().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ComputeUtf8Hash()

uint32_t v8::internal::StringHasher::ComputeUtf8Hash ( Vector< const char >  chars,
uint32_t  seed,
int utf16_length_out 
)
static

Definition at line 8956 of file objects.cc.

8958  {
8959  int vector_length = chars.length();
8960  // Handle some edge cases
8961  if (vector_length <= 1) {
8962  DCHECK(vector_length == 0 ||
8963  static_cast<uint8_t>(chars.start()[0]) <=
8965  *utf16_length_out = vector_length;
8966  return HashSequentialString(chars.start(), vector_length, seed);
8967  }
8968  // Start with a fake length which won't affect computation.
8969  // It will be updated later.
8971  unsigned remaining = static_cast<unsigned>(vector_length);
8972  const uint8_t* stream = reinterpret_cast<const uint8_t*>(chars.start());
8973  int utf16_length = 0;
8974  bool is_index = true;
8975  DCHECK(hasher.is_array_index_);
8976  while (remaining > 0) {
8977  unsigned consumed = 0;
8978  uint32_t c = unibrow::Utf8::ValueOf(stream, remaining, &consumed);
8979  DCHECK(consumed > 0 && consumed <= remaining);
8980  stream += consumed;
8981  remaining -= consumed;
8982  bool is_two_characters = c > unibrow::Utf16::kMaxNonSurrogateCharCode;
8983  utf16_length += is_two_characters ? 2 : 1;
8984  // No need to keep hashing. But we do need to calculate utf16_length.
8985  if (utf16_length > String::kMaxHashCalcLength) continue;
8986  if (is_two_characters) {
8989  hasher.AddCharacter(c1);
8990  hasher.AddCharacter(c2);
8991  if (is_index) is_index = hasher.UpdateIndex(c1);
8992  if (is_index) is_index = hasher.UpdateIndex(c2);
8993  } else {
8994  hasher.AddCharacter(c);
8995  if (is_index) is_index = hasher.UpdateIndex(c);
8996  }
8997  }
8998  *utf16_length_out = static_cast<int>(utf16_length);
8999  // Must set length here so that hash computation is correct.
9000  hasher.length_ = utf16_length;
9001  return hasher.GetHashField();
9002 }
static uint16_t LeadSurrogate(uint32_t char_code)
Definition: unicode.h:108
static const uchar kMaxNonSurrogateCharCode
Definition: unicode.h:98
static uint16_t TrailSurrogate(uint32_t char_code)
Definition: unicode.h:111
static const unsigned kMaxOneByteChar
Definition: unicode.h:141
static uchar ValueOf(const byte *str, unsigned length, unsigned *cursor)
Definition: unicode-inl.h:129
static uint32_t HashSequentialString(const schar *chars, int length, uint32_t seed)
Definition: objects-inl.h:6563
StringHasher(int length, uint32_t seed)
Definition: objects-inl.h:6478
static const int kMaxHashCalcLength
Definition: objects.h:8824
T * start() const
Definition: vector.h:47
int length() const
Definition: vector.h:41
unsigned short uint16_t
Definition: unicode.cc:23

References AddCharacter(), DCHECK, GetHashField(), HashSequentialString(), is_array_index_, v8::internal::String::kMaxArrayIndexSize, v8::internal::String::kMaxHashCalcLength, unibrow::Utf16::kMaxNonSurrogateCharCode, unibrow::Utf8::kMaxOneByteChar, unibrow::Utf16::LeadSurrogate(), v8::internal::Vector< T >::length(), length_, v8::internal::Vector< T >::start(), unibrow::Utf16::TrailSurrogate(), UpdateIndex(), and unibrow::Utf8::ValueOf().

Referenced by v8::internal::Utf8StringKey::Hash().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ DISALLOW_COPY_AND_ASSIGN()

v8::internal::StringHasher::DISALLOW_COPY_AND_ASSIGN ( StringHasher  )
private

◆ GetHashField()

uint32_t v8::internal::StringHasher::GetHashField ( )
protected

Definition at line 8943 of file objects.cc.

8943  {
8945  if (is_array_index_) {
8947  }
8948  return (GetHashCore(raw_running_hash_) << String::kHashShift) |
8950  } else {
8952  }
8953 }
static const int kHashShift
Definition: objects.h:8499
static const int kIsNotArrayIndexMask
Definition: objects.h:8495
static uint32_t MakeArrayIndexHash(uint32_t value, int length)
Definition: objects.cc:8925

References array_index_, is_array_index_, v8::internal::Name::kHashShift, v8::internal::Name::kIsNotArrayIndexMask, v8::internal::String::kMaxHashCalcLength, length_, MakeArrayIndexHash(), and raw_running_hash_.

Referenced by ComputeUtf8Hash(), v8::internal::IteratingStringHasher::Hash(), and HashSequentialString().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ has_trivial_hash()

bool v8::internal::StringHasher::has_trivial_hash ( )
inlineprotected

Definition at line 6488 of file objects-inl.h.

6488  {
6490 }

References v8::internal::String::kMaxHashCalcLength, and length_.

Referenced by v8::internal::IteratingStringHasher::Hash(), and HashSequentialString().

+ Here is the caller graph for this function:

◆ HashSequentialString()

template<typename schar >
uint32_t v8::internal::StringHasher::HashSequentialString ( const schar *  chars,
int  length,
uint32_t  seed 
)
inlinestatic

Definition at line 6563 of file objects-inl.h.

6565  {
6566  StringHasher hasher(length, seed);
6567  if (!hasher.has_trivial_hash()) hasher.AddCharacters(chars, length);
6568  return hasher.GetHashField();
6569 }

References AddCharacters(), GetHashField(), and has_trivial_hash().

Referenced by ComputeUtf8Hash(), v8::internal::NativeObjectsExplorer::FindOrAddGroupInfo(), v8::internal::HeapObjectsMap::GenerateId(), v8::internal::StringsStorage::GetEntry(), v8::internal::SeqOneByteSubStringKey::Hash(), v8::internal::HeapSnapshotJSONSerializer::INLINE(), and v8::internal::TwoCharHashTableKey::TwoCharHashTableKey().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ INLINE() [1/2]

v8::internal::StringHasher::INLINE ( static uint32_t   AddCharacterCoreuint32_t running_hash, uint16_t c)

◆ INLINE() [2/2]

v8::internal::StringHasher::INLINE ( static uint32_t   GetHashCoreuint32_t running_hash)

◆ MakeArrayIndexHash()

uint32_t v8::internal::StringHasher::MakeArrayIndexHash ( uint32_t  value,
int  length 
)
static

Definition at line 8925 of file objects.cc.

8925  {
8926  // For array indexes mix the length into the hash as an array index could
8927  // be zero.
8928  DCHECK(length > 0);
8932 
8934  value |= length << String::ArrayIndexLengthBits::kShift;
8935 
8936  DCHECK((value & String::kIsNotArrayIndexMask) == 0);
8938  (value & String::kContainsCachedArrayIndexMask) == 0);
8939  return value;
8940 }
static const U kShift
Definition: utils.h:204
static const unsigned int kContainsCachedArrayIndexMask
Definition: objects.h:8618
static const int kArrayIndexValueBits
Definition: objects.h:8601
static const int kMaxCachedArrayIndexLength
Definition: objects.h:8596
int TenToThe(int exponent)
Definition: utils.h:733

References DCHECK, v8::internal::String::kArrayIndexValueBits, v8::internal::String::kContainsCachedArrayIndexMask, v8::internal::Name::kIsNotArrayIndexMask, v8::internal::String::kMaxArrayIndexSize, v8::internal::String::kMaxCachedArrayIndexLength, v8::internal::BitFieldBase< T, shift, size, U >::kShift, and v8::internal::TenToThe().

Referenced by GetHashField(), and v8::internal::RUNTIME_FUNCTION().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ UpdateIndex()

bool v8::internal::StringHasher::UpdateIndex ( uint16_t  c)
inlineprivate

Definition at line 6519 of file objects-inl.h.

6519  {
6521  if (c < '0' || c > '9') {
6522  is_array_index_ = false;
6523  return false;
6524  }
6525  int d = c - '0';
6526  if (is_first_char_) {
6527  is_first_char_ = false;
6528  if (c == '0' && length_ > 1) {
6529  is_array_index_ = false;
6530  return false;
6531  }
6532  }
6533  if (array_index_ > 429496729U - ((d + 2) >> 3)) {
6534  is_array_index_ = false;
6535  return false;
6536  }
6537  array_index_ = array_index_ * 10 + d;
6538  return true;
6539 }

References array_index_, DCHECK, is_array_index_, is_first_char_, and length_.

Referenced by AddCharacters(), and ComputeUtf8Hash().

+ Here is the caller graph for this function:

Member Data Documentation

◆ array_index_

uint32_t v8::internal::StringHasher::array_index_
private

Definition at line 8388 of file objects.h.

Referenced by GetHashField(), and UpdateIndex().

◆ is_array_index_

bool v8::internal::StringHasher::is_array_index_
private

Definition at line 8389 of file objects.h.

Referenced by AddCharacters(), ComputeUtf8Hash(), GetHashField(), and UpdateIndex().

◆ is_first_char_

bool v8::internal::StringHasher::is_first_char_
private

Definition at line 8390 of file objects.h.

Referenced by UpdateIndex().

◆ kZeroHash

const int v8::internal::StringHasher::kZeroHash = 27
static

Definition at line 8363 of file objects.h.

Referenced by v8::internal::TwoCharHashTableKey::TwoCharHashTableKey().

◆ length_

int v8::internal::StringHasher::length_
private

Definition at line 8386 of file objects.h.

Referenced by ComputeUtf8Hash(), GetHashField(), has_trivial_hash(), and UpdateIndex().

◆ raw_running_hash_

uint32_t v8::internal::StringHasher::raw_running_hash_
private

Definition at line 8387 of file objects.h.

Referenced by AddCharacter(), GetHashField(), and StringHasher().


The documentation for this class was generated from the following files: