stub-cache.cc 4.63 KB
Newer Older
1 2 3 4
// Copyright 2012 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.

5
#include "src/ic/stub-cache.h"
6

7
#include "src/ast/ast.h"
8
#include "src/base/bits.h"
9
#include "src/counters.h"
10
#include "src/heap/heap.h"
11
#include "src/ic/ic-inl.h"
12 13 14 15

namespace v8 {
namespace internal {

16
StubCache::StubCache(Isolate* isolate) : isolate_(isolate) {
17 18 19 20
  // Ensure the nullptr (aka Smi::kZero) which StubCache::Get() returns
  // when the entry is not found is not considered as a handler.
  DCHECK(!IC::IsHandler(nullptr));
}
21 22

void StubCache::Initialize() {
23 24
  DCHECK(base::bits::IsPowerOfTwo(kPrimaryTableSize));
  DCHECK(base::bits::IsPowerOfTwo(kSecondaryTableSize));
25 26 27
  Clear();
}

28 29 30 31 32 33 34 35 36 37 38 39 40 41
// Hash algorithm for the primary table.  This algorithm is replicated in
// assembler for every architecture.  Returns an index into the table that
// is scaled by 1 << kCacheIndexShift.
int StubCache::PrimaryOffset(Name* name, Map* map) {
  STATIC_ASSERT(kCacheIndexShift == Name::kHashShift);
  // Compute the hash of the name (use entire hash field).
  DCHECK(name->HasHashCode());
  uint32_t field = name->hash_field();
  // Using only the low bits in 64-bit mode is unlikely to increase the
  // risk of collision even if the heap is spread over an area larger than
  // 4Gb (and not at all if it isn't).
  uint32_t map_low32bits =
      static_cast<uint32_t>(reinterpret_cast<uintptr_t>(map));
  // Base the offset on a simple combination of name and map.
42
  uint32_t key = map_low32bits + field;
43 44 45 46 47 48 49 50 51 52 53 54 55 56
  return key & ((kPrimaryTableSize - 1) << kCacheIndexShift);
}

// Hash algorithm for the secondary table.  This algorithm is replicated in
// assembler for every architecture.  Returns an index into the table that
// is scaled by 1 << kCacheIndexShift.
int StubCache::SecondaryOffset(Name* name, int seed) {
  // Use the seed from the primary cache in the secondary cache.
  uint32_t name_low32bits =
      static_cast<uint32_t>(reinterpret_cast<uintptr_t>(name));
  uint32_t key = (seed - name_low32bits) + kSecondaryMagic;
  return key & ((kSecondaryTableSize - 1) << kCacheIndexShift);
}

57 58
#ifdef DEBUG
namespace {
59

60
bool CommonStubCacheChecks(StubCache* stub_cache, Name* name, Map* map,
61
                           MaybeObject* handler) {
62
  // Validate that the name and handler do not move on scavenge, and that we
63 64
  // can use identity checks instead of structural equality checks.
  DCHECK(!name->GetHeap()->InNewSpace(name));
65
  DCHECK(!name->GetHeap()->InNewSpace(handler));
66
  DCHECK(name->IsUniqueName());
67
  DCHECK(name->HasHashCode());
68
  if (handler) DCHECK(IC::IsHandler(handler));
69
  return true;
70 71
}

72 73
}  // namespace
#endif
74

75
MaybeObject* StubCache::Set(Name* name, Map* map, MaybeObject* handler) {
76
  DCHECK(CommonStubCacheChecks(this, name, map, handler));
77 78

  // Compute the primary entry.
79
  int primary_offset = PrimaryOffset(name, map);
80
  Entry* primary = entry(primary_, primary_offset);
81
  MaybeObject* old_handler = primary->value;
82 83 84

  // If the primary entry has useful data in it, we retire it to the
  // secondary cache before overwriting it.
85 86
  if (old_handler != MaybeObject::FromObject(
                         isolate_->builtins()->builtin(Builtins::kIllegal))) {
87
    Map* old_map = primary->map;
88 89
    int seed = PrimaryOffset(primary->key, old_map);
    int secondary_offset = SecondaryOffset(primary->key, seed);
90 91 92 93 94 95
    Entry* secondary = entry(secondary_, secondary_offset);
    *secondary = *primary;
  }

  // Update primary cache.
  primary->key = name;
96
  primary->value = handler;
97 98
  primary->map = map;
  isolate()->counters()->megamorphic_stub_cache_updates()->Increment();
99
  return handler;
100 101
}

102
MaybeObject* StubCache::Get(Name* name, Map* map) {
103 104
  DCHECK(CommonStubCacheChecks(this, name, map, nullptr));
  int primary_offset = PrimaryOffset(name, map);
105
  Entry* primary = entry(primary_, primary_offset);
106
  if (primary->key == name && primary->map == map) {
107 108
    return primary->value;
  }
109
  int secondary_offset = SecondaryOffset(name, primary_offset);
110
  Entry* secondary = entry(secondary_, secondary_offset);
111
  if (secondary->key == name && secondary->map == map) {
112 113
    return secondary->value;
  }
114
  return nullptr;
115 116 117 118
}


void StubCache::Clear() {
119 120
  MaybeObject* empty = MaybeObject::FromObject(
      isolate_->builtins()->builtin(Builtins::kIllegal));
121 122
  for (int i = 0; i < kPrimaryTableSize; i++) {
    primary_[i].key = isolate()->heap()->empty_string();
ishell's avatar
ishell committed
123
    primary_[i].map = nullptr;
124 125 126 127
    primary_[i].value = empty;
  }
  for (int j = 0; j < kSecondaryTableSize; j++) {
    secondary_[j].key = isolate()->heap()->empty_string();
ishell's avatar
ishell committed
128
    secondary_[j].map = nullptr;
129 130 131 132
    secondary_[j].value = empty;
  }
}

133 134
}  // namespace internal
}  // namespace v8