transitions.cc 19.6 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
#include "src/transitions.h"
6

7
#include "src/objects-inl.h"
8 9
#include "src/transitions-inl.h"
#include "src/utils.h"
10 11 12 13 14

namespace v8 {
namespace internal {


15 16 17 18 19 20 21 22 23
// static
void TransitionArray::Insert(Handle<Map> map, Handle<Name> name,
                             Handle<Map> target, SimpleTransitionFlag flag) {
  Isolate* isolate = map->GetIsolate();
  target->SetBackPointer(*map);

  // If the map doesn't have any transitions at all yet, install the new one.
  if (CanStoreSimpleTransition(map->raw_transitions())) {
    if (flag == SIMPLE_PROPERTY_TRANSITION) {
24
      Handle<WeakCell> cell = Map::WeakCellForMap(target);
25 26 27 28 29 30
      ReplaceTransitions(map, *cell);
      return;
    }
    // If the flag requires a full TransitionArray, allocate one.
    Handle<TransitionArray> result = Allocate(isolate, 0, 1);
    ReplaceTransitions(map, *result);
31
  }
32

33 34 35 36 37 38 39
  bool is_special_transition = flag == SPECIAL_TRANSITION;
  // If the map has a simple transition, check if it should be overwritten.
  if (IsSimpleTransition(map->raw_transitions())) {
    Map* old_target = GetSimpleTransition(map->raw_transitions());
    Name* key = GetSimpleTransitionKey(old_target);
    PropertyDetails old_details = GetSimpleTargetDetails(old_target);
    PropertyDetails new_details = is_special_transition
40
                                      ? PropertyDetails::Empty()
41 42 43 44
                                      : GetTargetDetails(*name, *target);
    if (flag == SIMPLE_PROPERTY_TRANSITION && key->Equals(*name) &&
        old_details.kind() == new_details.kind() &&
        old_details.attributes() == new_details.attributes()) {
45
      Handle<WeakCell> cell = Map::WeakCellForMap(target);
46 47 48 49 50 51 52 53
      ReplaceTransitions(map, *cell);
      return;
    }
    // Otherwise allocate a full TransitionArray with slack for a new entry.
    Handle<TransitionArray> result = Allocate(isolate, 1, 1);
    // Re-read existing data; the allocation might have caused it to be cleared.
    if (IsSimpleTransition(map->raw_transitions())) {
      old_target = GetSimpleTransition(map->raw_transitions());
54 55
      result->NoIncrementalWriteBarrierSet(
          0, GetSimpleTransitionKey(old_target), old_target);
56 57 58 59
    } else {
      result->SetNumberOfTransitions(0);
    }
    ReplaceTransitions(map, *result);
60 61
  }

62 63
  // At this point, we know that the map has a full TransitionArray.
  DCHECK(IsFullTransitionArray(map->raw_transitions()));
64

65 66 67
  int number_of_transitions = 0;
  int new_nof = 0;
  int insertion_index = kNotFound;
68 69
  DCHECK_EQ(is_special_transition, IsSpecialTransition(*name));
  PropertyDetails details = is_special_transition
70
                                ? PropertyDetails::Empty()
71
                                : GetTargetDetails(*name, *target);
72

73
  {
74
    DisallowHeapAllocation no_gc;
75 76 77
    TransitionArray* array = TransitionArray::cast(map->raw_transitions());
    number_of_transitions = array->number_of_transitions();
    new_nof = number_of_transitions;
78

79 80 81 82 83 84
    int index =
        is_special_transition
            ? array->SearchSpecial(Symbol::cast(*name), &insertion_index)
            : array->Search(details.kind(), *name, details.attributes(),
                            &insertion_index);
    // If an existing entry was found, overwrite it and return.
85
    if (index != kNotFound) {
86
      array->SetTarget(index, *target);
87
      return;
88 89
    }

90 91 92 93 94 95 96 97 98
    ++new_nof;
    CHECK(new_nof <= kMaxNumberOfTransitions);
    DCHECK(insertion_index >= 0 && insertion_index <= number_of_transitions);

    // If there is enough capacity, insert new entry into the existing array.
    if (new_nof <= Capacity(array)) {
      array->SetNumberOfTransitions(new_nof);
      for (index = number_of_transitions; index > insertion_index; --index) {
        array->SetKey(index, array->GetKey(index - 1));
99
        array->SetTarget(index, array->GetTarget(index - 1));
100 101
      }
      array->SetKey(index, *name);
102
      array->SetTarget(index, *target);
103 104
      SLOW_DCHECK(array->IsSortedNoDuplicates());
      return;
105 106
    }
  }
107

108
  // We're gonna need a bigger TransitionArray.
verwaest's avatar
verwaest committed
109 110 111
  Handle<TransitionArray> result = Allocate(
      map->GetIsolate(), new_nof,
      Map::SlackForArraySize(number_of_transitions, kMaxNumberOfTransitions));
112

113
  // The map's transition array may have shrunk during the allocation above as
114 115
  // it was weakly traversed, though it is guaranteed not to disappear. Trim the
  // result copy if needed, and recompute variables.
116
  DCHECK(IsFullTransitionArray(map->raw_transitions()));
117
  DisallowHeapAllocation no_gc;
118
  TransitionArray* array = TransitionArray::cast(map->raw_transitions());
119
  if (array->number_of_transitions() != number_of_transitions) {
120
    DCHECK(array->number_of_transitions() < number_of_transitions);
121 122

    number_of_transitions = array->number_of_transitions();
123
    new_nof = number_of_transitions;
124

125
    insertion_index = kNotFound;
126 127 128 129 130
    int index =
        is_special_transition
            ? array->SearchSpecial(Symbol::cast(*name), &insertion_index)
            : array->Search(details.kind(), *name, details.attributes(),
                            &insertion_index);
131 132 133 134 135 136
    if (index == kNotFound) {
      ++new_nof;
    } else {
      insertion_index = index;
    }
    DCHECK(insertion_index >= 0 && insertion_index <= number_of_transitions);
137

138 139
    result->Shrink(ToKeyIndex(new_nof));
    result->SetNumberOfTransitions(new_nof);
140 141 142 143
  }

  if (array->HasPrototypeTransitions()) {
    result->SetPrototypeTransitions(array->GetPrototypeTransitions());
144 145
  }

146 147
  DCHECK_NE(kNotFound, insertion_index);
  for (int i = 0; i < insertion_index; ++i) {
148
    result->NoIncrementalWriteBarrierCopyFrom(array, i, i);
149
  }
150
  result->NoIncrementalWriteBarrierSet(insertion_index, *name, *target);
151
  for (int i = insertion_index; i < number_of_transitions; ++i) {
152
    result->NoIncrementalWriteBarrierCopyFrom(array, i, i + 1);
153 154
  }

155
  SLOW_DCHECK(result->IsSortedNoDuplicates());
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
  ReplaceTransitions(map, *result);
}


// static
Map* TransitionArray::SearchTransition(Map* map, PropertyKind kind, Name* name,
                                       PropertyAttributes attributes) {
  Object* raw_transitions = map->raw_transitions();
  if (IsSimpleTransition(raw_transitions)) {
    Map* target = GetSimpleTransition(raw_transitions);
    Name* key = GetSimpleTransitionKey(target);
    if (!key->Equals(name)) return NULL;
    PropertyDetails details = GetSimpleTargetDetails(target);
    if (details.attributes() != attributes) return NULL;
    if (details.kind() != kind) return NULL;
    return target;
  }
  if (IsFullTransitionArray(raw_transitions)) {
    TransitionArray* transitions = TransitionArray::cast(raw_transitions);
    int transition = transitions->Search(kind, name, attributes);
    if (transition == kNotFound) return NULL;
    return transitions->GetTarget(transition);
  }
  return NULL;
180 181 182
}


183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
// static
Map* TransitionArray::SearchSpecial(Map* map, Symbol* name) {
  Object* raw_transitions = map->raw_transitions();
  if (IsFullTransitionArray(raw_transitions)) {
    TransitionArray* transitions = TransitionArray::cast(raw_transitions);
    int transition = transitions->SearchSpecial(name);
    if (transition == kNotFound) return NULL;
    return transitions->GetTarget(transition);
  }
  return NULL;
}


// static
Handle<Map> TransitionArray::FindTransitionToField(Handle<Map> map,
                                                   Handle<Name> name) {
  DisallowHeapAllocation no_gc;
  Map* target = SearchTransition(*map, kData, *name, NONE);
  if (target == NULL) return Handle<Map>::null();
  PropertyDetails details = target->GetLastDescriptorDetails();
  DCHECK_EQ(NONE, details.attributes());
  if (details.type() != DATA) return Handle<Map>::null();
  return Handle<Map>(target);
}


// static
Handle<String> TransitionArray::ExpectedTransitionKey(Handle<Map> map) {
  DisallowHeapAllocation no_gc;
  Object* raw_transition = map->raw_transitions();
  if (!IsSimpleTransition(raw_transition)) return Handle<String>::null();
  Map* target = GetSimpleTransition(raw_transition);
  PropertyDetails details = GetSimpleTargetDetails(target);
  if (details.type() != DATA) return Handle<String>::null();
  if (details.attributes() != NONE) return Handle<String>::null();
  Name* name = GetSimpleTransitionKey(target);
  if (!name->IsString()) return Handle<String>::null();
  return Handle<String>(String::cast(name));
}


// static
bool TransitionArray::CanHaveMoreTransitions(Handle<Map> map) {
226
  if (map->is_dictionary_map()) return false;
227 228 229 230 231 232 233 234 235 236
  Object* raw_transitions = map->raw_transitions();
  if (IsFullTransitionArray(raw_transitions)) {
    TransitionArray* transitions = TransitionArray::cast(raw_transitions);
    return transitions->number_of_transitions() < kMaxNumberOfTransitions;
  }
  return true;
}


// static
237 238 239
void TransitionArray::PutPrototypeTransition(Handle<Map> map,
                                             Handle<Object> prototype,
                                             Handle<Map> target_map) {
240 241 242
  DCHECK(HeapObject::cast(*prototype)->map()->IsMap());
  // Don't cache prototype transition if this map is either shared, or a map of
  // a prototype.
243 244
  if (map->is_prototype_map()) return;
  if (map->is_dictionary_map() || !FLAG_cache_prototype_transitions) return;
245 246 247

  const int header = kProtoTransitionHeaderSize;

248 249
  Handle<WeakCell> target_cell = Map::WeakCellForMap(target_map);

250 251 252 253 254 255 256
  Handle<FixedArray> cache(GetPrototypeTransitions(*map));
  int capacity = cache->length() - header;
  int transitions = NumberOfPrototypeTransitions(*cache) + 1;

  if (transitions > capacity) {
    // Grow array by factor 2 up to MaxCachedPrototypeTransitions.
    int new_capacity = Min(kMaxCachedPrototypeTransitions, transitions * 2);
257
    if (new_capacity == capacity) return;
258
    int grow_by = new_capacity - capacity;
259

260 261
    Isolate* isolate = map->GetIsolate();
    cache = isolate->factory()->CopyFixedArrayAndGrow(cache, grow_by);
262 263 264 265 266 267 268 269 270 271 272 273 274
    if (capacity < 0) {
      // There was no prototype transitions array before, so the size
      // couldn't be copied. Initialize it explicitly.
      SetNumberOfPrototypeTransitions(*cache, 0);
    }

    SetPrototypeTransitions(map, cache);
  }

  // Reload number of transitions as GC might shrink them.
  int last = NumberOfPrototypeTransitions(*cache);
  int entry = header + last;

275
  cache->set(entry, *target_cell);
276 277 278 279 280 281 282 283 284 285 286
  SetNumberOfPrototypeTransitions(*cache, last + 1);
}


// static
Handle<Map> TransitionArray::GetPrototypeTransition(Handle<Map> map,
                                                    Handle<Object> prototype) {
  DisallowHeapAllocation no_gc;
  FixedArray* cache = GetPrototypeTransitions(*map);
  int number_of_transitions = NumberOfPrototypeTransitions(cache);
  for (int i = 0; i < number_of_transitions; i++) {
287 288 289 290 291 292
    WeakCell* target_cell =
        WeakCell::cast(cache->get(kProtoTransitionHeaderSize + i));
    if (!target_cell->cleared() &&
        Map::cast(target_cell->value())->prototype() == *prototype) {
      return handle(Map::cast(target_cell->value()));
    }
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
  }
  return Handle<Map>();
}


// static
FixedArray* TransitionArray::GetPrototypeTransitions(Map* map) {
  Object* raw_transitions = map->raw_transitions();
  Heap* heap = map->GetHeap();
  if (!IsFullTransitionArray(raw_transitions)) {
    return heap->empty_fixed_array();
  }
  TransitionArray* transitions = TransitionArray::cast(raw_transitions);
  if (!transitions->HasPrototypeTransitions()) {
    return heap->empty_fixed_array();
  }
  return transitions->GetPrototypeTransitions();
}


// static
void TransitionArray::SetNumberOfPrototypeTransitions(
    FixedArray* proto_transitions, int value) {
  DCHECK(proto_transitions->length() != 0);
  proto_transitions->set(kProtoTransitionNumberOfEntriesOffset,
                         Smi::FromInt(value));
}


// static
int TransitionArray::NumberOfTransitions(Object* raw_transitions) {
  if (CanStoreSimpleTransition(raw_transitions)) return 0;
  if (IsSimpleTransition(raw_transitions)) return 1;
326 327
  // Prototype maps don't have transitions.
  if (raw_transitions->IsPrototypeInfo()) return 0;
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
  DCHECK(IsFullTransitionArray(raw_transitions));
  return TransitionArray::cast(raw_transitions)->number_of_transitions();
}


// static
int TransitionArray::Capacity(Object* raw_transitions) {
  if (!IsFullTransitionArray(raw_transitions)) return 1;
  TransitionArray* t = TransitionArray::cast(raw_transitions);
  if (t->length() <= kFirstIndex) return 0;
  return (t->length() - kFirstIndex) / kTransitionSize;
}


// Private static helper functions.

Handle<TransitionArray> TransitionArray::Allocate(Isolate* isolate,
                                                  int number_of_transitions,
                                                  int slack) {
  Handle<FixedArray> array = isolate->factory()->NewFixedArray(
      LengthFor(number_of_transitions + slack));
  array->set(kPrototypeTransitionsIndex, Smi::FromInt(0));
  array->set(kTransitionLengthIndex, Smi::FromInt(number_of_transitions));
  return Handle<TransitionArray>::cast(array);
}


355 356 357 358 359 360
void TransitionArray::NoIncrementalWriteBarrierCopyFrom(TransitionArray* origin,
                                                        int origin_transition,
                                                        int target_transition) {
  NoIncrementalWriteBarrierSet(target_transition,
                               origin->GetKey(origin_transition),
                               origin->GetTarget(origin_transition));
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
}


static void ZapTransitionArray(TransitionArray* transitions) {
  MemsetPointer(transitions->data_start(),
                transitions->GetHeap()->the_hole_value(),
                transitions->length());
}


void TransitionArray::ReplaceTransitions(Handle<Map> map,
                                         Object* new_transitions) {
  Object* raw_transitions = map->raw_transitions();
  if (IsFullTransitionArray(raw_transitions)) {
    TransitionArray* old_transitions = TransitionArray::cast(raw_transitions);
#ifdef DEBUG
    CheckNewTransitionsAreConsistent(map, old_transitions, new_transitions);
    DCHECK(old_transitions != new_transitions);
#endif
    // Transition arrays are not shared. When one is replaced, it should not
    // keep referenced objects alive, so we zap it.
    // When there is another reference to the array somewhere (e.g. a handle),
    // not zapping turns from a waste of memory into a source of crashes.
    ZapTransitionArray(old_transitions);
  }
  map->set_raw_transitions(new_transitions);
}


static void ZapPrototypeTransitions(Object* raw_transitions) {
  DCHECK(TransitionArray::IsFullTransitionArray(raw_transitions));
  TransitionArray* transitions = TransitionArray::cast(raw_transitions);
  if (!transitions->HasPrototypeTransitions()) return;
  FixedArray* proto_transitions = transitions->GetPrototypeTransitions();
  MemsetPointer(proto_transitions->data_start(),
                proto_transitions->GetHeap()->the_hole_value(),
                proto_transitions->length());
}


void TransitionArray::SetPrototypeTransitions(
    Handle<Map> map, Handle<FixedArray> proto_transitions) {
  EnsureHasFullTransitionArray(map);
  if (Heap::ShouldZapGarbage()) {
    Object* raw_transitions = map->raw_transitions();
    DCHECK(raw_transitions != *proto_transitions);
    ZapPrototypeTransitions(raw_transitions);
  }
  TransitionArray* transitions = TransitionArray::cast(map->raw_transitions());
  transitions->SetPrototypeTransitions(*proto_transitions);
}


void TransitionArray::EnsureHasFullTransitionArray(Handle<Map> map) {
  Object* raw_transitions = map->raw_transitions();
  if (IsFullTransitionArray(raw_transitions)) return;
  int nof = IsSimpleTransition(raw_transitions) ? 1 : 0;
  Handle<TransitionArray> result = Allocate(map->GetIsolate(), nof);
  DisallowHeapAllocation no_gc;
  // Reload pointer after the allocation that just happened.
  raw_transitions = map->raw_transitions();
  int new_nof = IsSimpleTransition(raw_transitions) ? 1 : 0;
  if (new_nof != nof) {
    DCHECK(new_nof == 0);
    result->Shrink(ToKeyIndex(0));
    result->SetNumberOfTransitions(0);
  } else if (nof == 1) {
428 429 430
    Map* target = GetSimpleTransition(raw_transitions);
    Name* key = GetSimpleTransitionKey(target);
    result->NoIncrementalWriteBarrierSet(0, key, target);
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
  }
  ReplaceTransitions(map, *result);
}


void TransitionArray::TraverseTransitionTreeInternal(Map* map,
                                                     TraverseCallback callback,
                                                     void* data) {
  Object* raw_transitions = map->raw_transitions();
  if (IsFullTransitionArray(raw_transitions)) {
    TransitionArray* transitions = TransitionArray::cast(raw_transitions);
    if (transitions->HasPrototypeTransitions()) {
      FixedArray* proto_trans = transitions->GetPrototypeTransitions();
      for (int i = 0; i < NumberOfPrototypeTransitions(proto_trans); ++i) {
        int index = TransitionArray::kProtoTransitionHeaderSize + i;
446 447 448
        WeakCell* cell = WeakCell::cast(proto_trans->get(index));
        TraverseTransitionTreeInternal(Map::cast(cell->value()), callback,
                                       data);
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
      }
    }
    for (int i = 0; i < transitions->number_of_transitions(); ++i) {
      TraverseTransitionTreeInternal(transitions->GetTarget(i), callback, data);
    }
  } else if (IsSimpleTransition(raw_transitions)) {
    TraverseTransitionTreeInternal(GetSimpleTransition(raw_transitions),
                                   callback, data);
  }
  callback(map, data);
}


#ifdef DEBUG
void TransitionArray::CheckNewTransitionsAreConsistent(
    Handle<Map> map, TransitionArray* old_transitions, Object* transitions) {
  // This function only handles full transition arrays.
  DCHECK(IsFullTransitionArray(transitions));
  TransitionArray* new_transitions = TransitionArray::cast(transitions);
  for (int i = 0; i < old_transitions->number_of_transitions(); i++) {
    Map* target = old_transitions->GetTarget(i);
    if (target->instance_descriptors() == map->instance_descriptors()) {
      Name* key = old_transitions->GetKey(i);
      int new_target_index;
      if (TransitionArray::IsSpecialTransition(key)) {
        new_target_index = new_transitions->SearchSpecial(Symbol::cast(key));
      } else {
        PropertyDetails details =
            TransitionArray::GetTargetDetails(key, target);
        new_target_index =
            new_transitions->Search(details.kind(), key, details.attributes());
      }
      DCHECK_NE(TransitionArray::kNotFound, new_target_index);
      DCHECK_EQ(target, new_transitions->GetTarget(new_target_index));
    }
  }
}
#endif


// Private non-static helper functions (operating on full transition arrays).

491
int TransitionArray::SearchDetails(int transition, PropertyKind kind,
492 493 494 495 496 497 498 499 500 501
                                   PropertyAttributes attributes,
                                   int* out_insertion_index) {
  int nof_transitions = number_of_transitions();
  DCHECK(transition < nof_transitions);
  Name* key = GetKey(transition);
  for (; transition < nof_transitions && GetKey(transition) == key;
       transition++) {
    Map* target = GetTarget(transition);
    PropertyDetails target_details = GetTargetDetails(key, target);

502
    int cmp = CompareDetails(kind, attributes, target_details.kind(),
503 504 505 506 507 508 509 510 511 512 513 514
                             target_details.attributes());
    if (cmp == 0) {
      return transition;
    } else if (cmp < 0) {
      break;
    }
  }
  if (out_insertion_index != NULL) *out_insertion_index = transition;
  return kNotFound;
}


515
int TransitionArray::Search(PropertyKind kind, Name* name,
516 517 518 519 520 521
                            PropertyAttributes attributes,
                            int* out_insertion_index) {
  int transition = SearchName(name, out_insertion_index);
  if (transition == kNotFound) {
    return kNotFound;
  }
522
  return SearchDetails(transition, kind, attributes, out_insertion_index);
523
}
524 525
}  // namespace internal
}  // namespace v8