code-cache.h 3.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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_OBJECTS_CODE_CACHE_H_
#define V8_OBJECTS_CODE_CACHE_H_

#include "src/objects/hash-table.h"

// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"

namespace v8 {
namespace internal {

16 17 18 19 20
// The key in the code cache hash table consists of the property name and the
// code object. The actual match is on the name and the code flags. If a key
// is created using the flags and not a code object it can only be used for
// lookup not to create a new entry.
class CodeCacheHashTableKey final {
21
 public:
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  CodeCacheHashTableKey(Handle<Name> name, Code::Flags flags)
      : name_(name), flags_(flags), code_() {
    DCHECK(name_->IsUniqueName());
  }

  CodeCacheHashTableKey(Handle<Name> name, Handle<Code> code)
      : name_(name), flags_(code->flags()), code_(code) {
    DCHECK(name_->IsUniqueName());
  }

  bool IsMatch(Object* other) {
    DCHECK(other->IsFixedArray());
    FixedArray* pair = FixedArray::cast(other);
    Name* name = Name::cast(pair->get(0));
    Code::Flags flags = Code::cast(pair->get(1))->flags();
    if (flags != flags_) return false;
    DCHECK(name->IsUniqueName());
    return *name_ == name;
  }

  static uint32_t NameFlagsHashHelper(Name* name, Code::Flags flags) {
    return name->Hash() ^ flags;
  }

  uint32_t Hash() { return NameFlagsHashHelper(*name_, flags_); }

  MUST_USE_RESULT Handle<Object> AsHandle(Isolate* isolate) {
    Handle<Code> code = code_.ToHandleChecked();
    Handle<FixedArray> pair = isolate->factory()->NewFixedArray(2);
    pair->set(0, *name_);
    pair->set(1, *code);
    return pair;
  }

 private:
  Handle<Name> name_;
  Code::Flags flags_;
  // TODO(jkummerow): We should be able to get by without this.
  MaybeHandle<Code> code_;
};

class CodeCacheHashTableShape : public BaseShape<CodeCacheHashTableKey*> {
 public:
  static inline bool IsMatch(CodeCacheHashTableKey* key, Object* value) {
66 67 68
    return key->IsMatch(value);
  }

69
  static inline uint32_t Hash(Isolate* isolate, CodeCacheHashTableKey* key) {
70 71
    return key->Hash();
  }
72

73
  static inline uint32_t HashForObject(Isolate* isolate, Object* object) {
74 75 76 77
    FixedArray* pair = FixedArray::cast(object);
    Name* name = Name::cast(pair->get(0));
    Code* code = Code::cast(pair->get(1));
    return CodeCacheHashTableKey::NameFlagsHashHelper(name, code->flags());
78 79
  }

80 81
  static inline Handle<Object> AsHandle(Isolate* isolate,
                                        CodeCacheHashTableKey* key);
82 83 84 85 86 87 88 89 90 91

  static const int kPrefixSize = 0;
  // The both the key (name + flags) and value (code object) can be derived from
  // the fixed array that stores both the name and code.
  // TODO(verwaest): Don't allocate a fixed array but inline name and code.
  // Rewrite IsMatch to get table + index as input rather than just the raw key.
  static const int kEntrySize = 1;
};

class CodeCacheHashTable
92
    : public HashTable<CodeCacheHashTable, CodeCacheHashTableShape> {
93 94 95 96 97 98
 public:
  static Handle<CodeCacheHashTable> Put(Handle<CodeCacheHashTable> table,
                                        Handle<Name> name, Handle<Code> code);

  Code* Lookup(Name* name, Code::Flags flags);

99
  DECL_CAST(CodeCacheHashTable)
100 101 102 103 104 105 106 107 108 109 110

 private:
  DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCacheHashTable);
};

}  // namespace internal
}  // namespace v8

#include "src/objects/object-macros-undef.h"

#endif  // V8_OBJECTS_CODE_CACHE_H_