transitions.h 16.2 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6
#ifndef V8_OBJECTS_TRANSITIONS_H_
#define V8_OBJECTS_TRANSITIONS_H_
7

8
#include "src/common/checks.h"
9
#include "src/execution/isolate.h"
10
#include "src/objects/descriptor-array.h"
11
#include "src/objects/elements-kind.h"
12
#include "src/objects/map.h"
13
#include "src/objects/maybe-object.h"
14
#include "src/objects/name.h"
15
#include "src/objects/objects.h"
16

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

20 21 22
namespace v8 {
namespace internal {

23 24 25 26
namespace third_party_heap {
class Impl;
}

27 28 29
// Find all transitions with given name and calls the callback.
using ForEachTransitionCallback = std::function<void(Map)>;

30 31 32 33 34 35 36 37
// TransitionsAccessor is a helper class to encapsulate access to the various
// ways a Map can store transitions to other maps in its respective field at
// Map::kTransitionsOrPrototypeInfo.
// It caches state information internally, which becomes stale when a Map's
// transitions storage changes or when a GC cycle clears dead transitions;
// so while a TransitionsAccessor instance can be used for several read-only
// operations in a row (provided no GC happens between them), it must be
// discarded and recreated after "Insert" and "UpdateHandler" operations.
38
//
39 40 41 42 43 44 45 46
// Internal details: a Map's field either holds an in-place weak reference to a
// transition target, or a StoreIC handler for a transitioning store (which in
// turn points to its target map), or a TransitionArray for several target maps
// and/or handlers as well as prototype and ElementsKind transitions.  Property
// details (and in case of inline target storage, the key) are retrieved from
// the target map's descriptor array.  Stored transitions are weak in the GC
// sense: both single transitions stored inline and TransitionArray fields are
// cleared when the map they refer to is not otherwise reachable.
47
class V8_EXPORT_PRIVATE TransitionsAccessor {
48
 public:
49 50 51
  // {concurrent_access} signals that the TransitionsAccessor will only be used
  // in background threads. It acquires a reader lock for critical paths, as
  // well as blocking the accessor from modifying the TransitionsArray.
52
  inline TransitionsAccessor(Isolate* isolate, Map map,
53
                             bool concurrent_access = false);
54

55
  // Insert a new transition into |map|'s transition array, extending it
56 57 58
  // as necessary. This can trigger GC.
  static void Insert(Isolate* isolate, Handle<Map> map, Handle<Name> name,
                     Handle<Map> target, SimpleTransitionFlag flag);
59

60
  Map SearchTransition(Name name, PropertyKind kind,
61
                       PropertyAttributes attributes);
62 63 64
  static inline MaybeHandle<Map> SearchTransition(
      Isolate* isolate, Handle<Map> map, Name name, PropertyKind kind,
      PropertyAttributes attributes);
65

66
  Map SearchSpecial(Symbol name);
67 68 69
  static inline MaybeHandle<Map> SearchSpecial(Isolate* isolate,
                                               Handle<Map> map, Symbol name);

70 71
  // Returns true for non-property transitions like elements kind, or
  // or frozen/sealed transitions.
72
  static bool IsSpecialTransition(ReadOnlyRoots roots, Name name);
73

74 75 76 77 78 79 80
  enum RequestedLocation { kAnyLocation, kFieldOnly };
  MaybeHandle<Map> FindTransitionToDataProperty(
      Handle<Name> name, RequestedLocation requested_location = kAnyLocation);

  MaybeHandle<Map> FindTransitionToField(Handle<Name> name) {
    return FindTransitionToDataProperty(name, kFieldOnly);
  }
81

82 83 84 85 86 87 88 89
  // Find all transitions with given name and calls the callback.
  // Neither GCs nor operations requiring Isolate::full_transition_array_access
  // lock are allowed inside the callback.
  // If any of the GC- or lock-requiring processing is necessary, it has to be
  // done outside of the callback.
  void ForEachTransitionTo(Name name, const ForEachTransitionCallback& callback,
                           DisallowGarbageCollection* no_gc);

90 91
  inline Handle<String> ExpectedTransitionKey();
  inline Handle<Map> ExpectedTransitionTarget();
92

93
  int NumberOfTransitions();
94 95 96
  // The size of transition arrays are limited so they do not end up in large
  // object space. Otherwise ClearNonLiveReferences would leak memory while
  // applying in-place right trimming.
97
  static const int kMaxNumberOfTransitions = 1024 + 512;
98
  inline Name GetKey(int transition_number);
99
  inline Map GetTarget(int transition_number);
100
  static inline PropertyDetails GetTargetDetails(Name name, Map target);
101

102 103
  static bool CanHaveMoreTransitions(Isolate* isolate, Handle<Map> map);

104
  static bool IsMatchingMap(Map target, Name name, PropertyKind kind,
105
                            PropertyAttributes attributes);
106

107 108 109 110
  bool HasIntegrityLevelTransitionTo(
      Map to, Symbol* out_symbol = nullptr,
      PropertyAttributes* out_integrity_level = nullptr);

111
  // ===== ITERATION =====
112
  using TraverseCallback = std::function<void(Map)>;
113

114 115
  // Traverse the transition tree in preorder.
  void TraverseTransitionTree(const TraverseCallback& callback) {
116
    // Make sure that we do not allocate in the callback.
117
    DisallowGarbageCollection no_gc;
118 119
    base::SharedMutexGuardIf<base::kShared> scope(
        isolate_->full_transition_array_access(), concurrent_access_);
120
    TraverseTransitionTreeInternal(callback, &no_gc);
121
  }
122 123 124 125 126 127 128 129 130

  // ===== PROTOTYPE TRANSITIONS =====
  // When you set the prototype of an object using the __proto__ accessor you
  // need a new map for the object (the prototype is stored in the map).  In
  // order not to multiply maps unnecessarily we store these as transitions in
  // the original map.  That way we can transition to the same map if the same
  // prototype is set, rather than creating a new map every time.  The
  // transitions are in the form of a map where the keys are prototype objects
  // and the values are the maps they transition to.
131 132 133 134 135 136
  // PutPrototypeTransition can trigger GC.
  static void PutPrototypeTransition(Isolate* isolate, Handle<Map>,
                                     Handle<Object> prototype,
                                     Handle<Map> target_map);
  static Handle<Map> GetPrototypeTransition(Isolate* isolate, Handle<Map> map,
                                            Handle<Object> prototype);
137

138 139 140 141 142
  // During the first-time Map::Update and Map::TryUpdate, the migration target
  // map could be cached in the raw_transitions slot of the old map that is
  // deprecated from the map transition tree. The next time old map is updated,
  // we will check this cache slot as a shortcut to get the migration target
  // map.
143 144
  static void SetMigrationTarget(Isolate* isolate, Handle<Map> map,
                                 Map migration_target);
145 146
  Map GetMigrationTarget();

147 148
#if DEBUG || OBJECT_PRINT
  void PrintTransitions(std::ostream& os);
149
  static void PrintOneTransition(std::ostream& os, Name key, Map target);
150 151
  void PrintTransitionTree();
  void PrintTransitionTree(std::ostream& os, int level,
152
                           DisallowGarbageCollection* no_gc);
153 154
#endif
#if DEBUG
155 156 157
  static void CheckNewTransitionsAreConsistent(Isolate* isolate,
                                               Handle<Map> map,
                                               Object transitions);
158
  bool IsConsistentWithBackPointers();
159
  bool IsSortedNoDuplicates();
160
#endif
161

162 163 164 165 166
 protected:
  // Allow tests to use inheritance to access internals.
  enum Encoding {
    kPrototypeInfo,
    kUninitialized,
167
    kMigrationTarget,
168
    kWeakRef,
169 170 171
    kFullTransitionArray,
  };

172
  inline Encoding encoding() { return encoding_; }
173

174 175
  inline int Capacity();

176 177
  inline TransitionArray transitions();

178 179
  DISALLOW_GARBAGE_COLLECTION(no_gc_)

180 181
 private:
  friend class MarkCompactCollector;  // For HasSimpleTransitionTo.
182
  friend class third_party_heap::Impl;
183
  friend class TransitionArray;
184

185 186
  static inline Encoding GetEncoding(Isolate* isolate,
                                     MaybeObject raw_transitions);
187 188
  static inline Encoding GetEncoding(Isolate* isolate, TransitionArray array);
  static inline Encoding GetEncoding(Isolate* isolate, Handle<Map> map);
189

190 191 192 193
  static inline TransitionArray GetTransitionArray(Isolate* isolate,
                                                   MaybeObject raw_transitions);
  static inline TransitionArray GetTransitionArray(Isolate* isolate,
                                                   Handle<Map> map);
194

195
  static inline Map GetSimpleTransition(Isolate* isolate, Handle<Map> map);
196
  static inline Name GetSimpleTransitionKey(Map transition);
197
  inline PropertyDetails GetSimpleTargetDetails(Map transition);
198

199
  static inline Map GetTargetFromRaw(MaybeObject raw);
200

201 202 203 204 205
  static void EnsureHasFullTransitionArray(Isolate* isolate, Handle<Map> map);
  static void SetPrototypeTransitions(Isolate* isolate, Handle<Map> map,
                                      Handle<WeakFixedArray> proto_transitions);
  static WeakFixedArray GetPrototypeTransitions(Isolate* isolate,
                                                Handle<Map> map);
206

207 208 209 210 211
  static inline void ReplaceTransitions(Isolate* isolate, Handle<Map> map,
                                        MaybeObject new_transitions);
  static inline void ReplaceTransitions(
      Isolate* isolate, Handle<Map> map,
      Handle<TransitionArray> new_transitions);
212

213
  bool HasSimpleTransitionTo(Map map);
214

215
  inline Map GetTargetMapFromWeakRef();
216

217
  void TraverseTransitionTreeInternal(const TraverseCallback& callback,
218
                                      DisallowGarbageCollection* no_gc);
219

220
  Isolate* isolate_;
221
  Map map_;
222
  MaybeObject raw_transitions_;
223
  Encoding encoding_;
224
  bool concurrent_access_;
225 226 227 228 229 230 231 232 233

  DISALLOW_IMPLICIT_CONSTRUCTORS(TransitionsAccessor);
};

// TransitionArrays are fixed arrays used to hold map transitions for property,
// constant, and element changes.
// The TransitionArray class exposes a very low-level interface. Most clients
// should use TransitionsAccessors.
// TransitionArrays have the following format:
234
// [0] Link to next TransitionArray (for weak handling support) (strong ref)
235
// [1] Smi(0) or WeakFixedArray of prototype transitions (strong ref)
236
// [2] Number of transitions (can be zero after trimming)
237 238
// [3] First transition key (strong ref)
// [4] First transition target (weak ref)
239 240
// ...
// [3 + number of transitions * kTransitionSize]: start of slack
241
class TransitionArray : public WeakFixedArray {
242
 public:
243
  DECL_CAST(TransitionArray)
244

245
  inline WeakFixedArray GetPrototypeTransitions();
246
  inline bool HasPrototypeTransitions();
247

248
  // Accessors for fetching instance transition at transition number.
249 250
  inline void SetKey(int transition_number, Name value);
  inline Name GetKey(int transition_number);
251
  inline HeapObjectSlot GetKeySlot(int transition_number);
252

253
  inline Map GetTarget(int transition_number);
254 255
  inline void SetRawTarget(int transition_number, MaybeObject target);
  inline MaybeObject GetRawTarget(int transition_number);
256
  inline HeapObjectSlot GetTargetSlot(int transition_number);
257
  inline bool GetTargetIfExists(int transition_number, Isolate* isolate,
258
                                Map* target);
259

260
  // Required for templatized Search interface.
261
  inline Name GetKey(InternalIndex index);
262 263
  static constexpr int kNotFound = -1;

264
  inline Name GetSortedKey(int transition_number);
265
  int GetSortedKeyIndex(int transition_number) { return transition_number; }
266
  inline int number_of_entries() const;
267
#ifdef DEBUG
268
  V8_EXPORT_PRIVATE bool IsSortedNoDuplicates();
269
#endif
270

271
  V8_EXPORT_PRIVATE void Sort();
272

273
  void PrintInternal(std::ostream& os);
274

275
  DECL_PRINTER(TransitionArray)
276
  DECL_VERIFIER(TransitionArray)
277

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
  // Layout for full transition arrays.
  static const int kPrototypeTransitionsIndex = 0;
  static const int kTransitionLengthIndex = 1;
  static const int kFirstIndex = 2;

  // Layout of map transition entries in full transition arrays.
  static const int kEntryKeyIndex = 0;
  static const int kEntryTargetIndex = 1;
  static const int kEntrySize = 2;

  // Conversion from transition number to array indices.
  static int ToKeyIndex(int transition_number) {
    return kFirstIndex + (transition_number * kEntrySize) + kEntryKeyIndex;
  }

  static int ToTargetIndex(int transition_number) {
    return kFirstIndex + (transition_number * kEntrySize) + kEntryTargetIndex;
  }

297
  inline int SearchNameForTesting(Name name,
298
                                  int* out_insertion_index = nullptr);
299

300 301 302
  inline Map SearchAndGetTargetForTesting(PropertyKind kind, Name name,
                                          PropertyAttributes attributes);

303
 private:
304
  friend class Factory;
305
  friend class MarkCompactCollector;
306
  friend class third_party_heap::Impl;
307
  friend class TransitionsAccessor;
308

309
  inline void SetNumberOfTransitions(int number_of_transitions);
310

311
  inline int Capacity();
312

313 314 315 316 317 318 319
  // ===== PROTOTYPE TRANSITIONS =====
  // Cache format:
  //    0: finger - index of the first free cell in the cache
  //    1 + i: target map
  static const int kProtoTransitionHeaderSize = 1;
  static const int kMaxCachedPrototypeTransitions = 256;

320
  inline void SetPrototypeTransitions(WeakFixedArray prototype_transitions);
321

322
  static inline int NumberOfPrototypeTransitions(
323 324
      WeakFixedArray proto_transitions);
  static void SetNumberOfPrototypeTransitions(WeakFixedArray proto_transitions,
325
                                              int value);
326

327
  static const int kProtoTransitionNumberOfEntriesOffset = 0;
328
  static_assert(kProtoTransitionHeaderSize == 1);
329 330 331 332 333 334 335 336

  // Returns the fixed array length required to hold number_of_transitions
  // transitions.
  static int LengthFor(int number_of_transitions) {
    return ToKeyIndex(number_of_transitions);
  }

  // Search a  transition for a given kind, property name and attributes.
337
  int Search(PropertyKind kind, Name name, PropertyAttributes attributes,
338
             int* out_insertion_index = nullptr);
339

340 341
  V8_EXPORT_PRIVATE Map SearchAndGetTarget(PropertyKind kind, Name name,
                                           PropertyAttributes attributes);
342

343 344
  // Search a non-property transition (like elements kind, observe or frozen
  // transitions).
345 346
  inline int SearchSpecial(Symbol symbol, bool concurrent_search = false,
                           int* out_insertion_index = nullptr);
347
  // Search a first transition for a given property name.
348 349
  inline int SearchName(Name name, bool concurrent_search = false,
                        int* out_insertion_index = nullptr);
350
  int SearchDetails(int transition, PropertyKind kind,
351
                    PropertyAttributes attributes, int* out_insertion_index);
352 353
  Map SearchDetailsAndGetTarget(int transition, PropertyKind kind,
                                PropertyAttributes attributes);
354

355 356 357 358
  // Find all transitions with given name and calls the callback.
  void ForEachTransitionTo(Name name,
                           const ForEachTransitionCallback& callback);

359
  inline int number_of_transitions() const;
360

361
  static bool CompactPrototypeTransitionArray(Isolate* isolate,
362
                                              WeakFixedArray array);
363

364 365
  static Handle<WeakFixedArray> GrowPrototypeTransitionArray(
      Handle<WeakFixedArray> array, int new_capacity, Isolate* isolate);
366

367
  // Compares two tuples <key, kind, attributes>, returns -1 if
368
  // tuple1 is "less" than tuple2, 0 if tuple1 equal to tuple2 and 1 otherwise.
369 370
  static inline int CompareKeys(Name key1, uint32_t hash1, PropertyKind kind1,
                                PropertyAttributes attributes1, Name key2,
371
                                uint32_t hash2, PropertyKind kind2,
372 373 374 375
                                PropertyAttributes attributes2);

  // Compares keys, returns -1 if key1 is "less" than key2,
  // 0 if key1 equal to key2 and 1 otherwise.
376
  static inline int CompareNames(Name key1, uint32_t hash1, Name key2,
377 378 379 380
                                 uint32_t hash2);

  // Compares two details, returns -1 if details1 is "less" than details2,
  // 0 if details1 equal to details2 and 1 otherwise.
381
  static inline int CompareDetails(PropertyKind kind1,
382
                                   PropertyAttributes attributes1,
383
                                   PropertyKind kind2,
384 385
                                   PropertyAttributes attributes2);

386
  inline void Set(int transition_number, Name key, MaybeObject target);
387

388
  OBJECT_CONSTRUCTORS(TransitionArray, WeakFixedArray);
389 390
};

391 392
}  // namespace internal
}  // namespace v8
393

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

396
#endif  // V8_OBJECTS_TRANSITIONS_H_