1// Copyright 2016 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_OBJECTS_LOOKUP_CACHE_H_
6#define V8_OBJECTS_LOOKUP_CACHE_H_
7
8#include "src/objects/map.h"
9#include "src/objects/name.h"
10#include "src/objects/objects.h"
11
12namespace v8 {
13namespace internal {
14
15// Cache for mapping (map, property name) into descriptor index.
16// The cache contains both positive and negative results.
17// Descriptor index equals kNotFound means the property is absent.
18// Cleared at startup and prior to any gc.
19class DescriptorLookupCache {
20 public:
21  DescriptorLookupCache(const DescriptorLookupCache&) = delete;
22  DescriptorLookupCache& operator=(const DescriptorLookupCache&) = delete;
23  // Lookup descriptor index for (map, name).
24  // If absent, kAbsent is returned.
25  inline int Lookup(Map source, Name name);
26
27  // Update an element in the cache.
28  inline void Update(Map source, Name name, int result);
29
30  // Clear the cache.
31  void Clear();
32
33  static const int kAbsent = -2;
34
35 private:
36  DescriptorLookupCache() {
37    for (int i = 0; i < kLength; ++i) {
38      keys_[i].source = Map();
39      keys_[i].name = Name();
40      results_[i] = kAbsent;
41    }
42  }
43
44  static inline int Hash(Map source, Name name);
45
46  static const int kLength = 64;
47  struct Key {
48    Map source;
49    Name name;
50  };
51
52  Key keys_[kLength];
53  int results_[kLength];
54
55  friend class Isolate;
56};
57
58}  // namespace internal
59}  // namespace v8
60
61#endif  // V8_OBJECTS_LOOKUP_CACHE_H_
62