| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#ifndef vm_Caches_h |
| 8 |
#define vm_Caches_h |
| 9 |
|
| 10 |
#include "mozilla/Array.h" |
| 11 |
#include "mozilla/MathAlgorithms.h" |
| 12 |
#include "mozilla/Maybe.h" |
| 13 |
#include "mozilla/MruCache.h" |
| 14 |
#include "mozilla/TemplateLib.h" |
| 15 |
#include "mozilla/UniquePtr.h" |
| 16 |
|
| 17 |
#include "frontend/ScopeBindingCache.h" |
| 18 |
#include "gc/Tracer.h" |
| 19 |
#include "js/RootingAPI.h" |
| 20 |
#include "js/TypeDecls.h" |
| 21 |
#include "vm/JSScript.h" |
| 22 |
#include "vm/Shape.h" |
| 23 |
#include "vm/StringType.h" |
| 24 |
|
| 25 |
namespace js { |
| 26 |
|
| 27 |
struct EvalCacheEntry { |
| 28 |
JSLinearString* str; |
| 29 |
JSScript* script; |
| 30 |
JSScript* callerScript; |
| 31 |
jsbytecode* pc; |
| 32 |
|
| 33 |
// We sweep this cache after a nursery collection to update entries with |
| 34 |
// string keys that have been tenured. |
| 35 |
// |
| 36 |
// The entire cache is purged on a major GC, so we don't need to sweep it |
| 37 |
// then. |
| 38 |
bool traceWeak(JSTracer* trc) { |
| 39 |
MOZ_ASSERT(trc->kind() == JS::TracerKind::MinorSweeping); |
| 40 |
return TraceManuallyBarrieredWeakEdge(trc, &str, "EvalCacheEntry::str"); |
| 41 |
} |
| 42 |
}; |
| 43 |
|
| 44 |
struct EvalCacheLookup { |
| 45 |
JSLinearString* str = nullptr; |
| 46 |
JSScript* callerScript = nullptr; |
| 47 |
MOZ_INIT_OUTSIDE_CTOR jsbytecode* pc = nullptr; |
| 48 |
|
| 49 |
EvalCacheLookup() = default; |
| 50 |
EvalCacheLookup(JSLinearString* str, JSScript* callerScript, jsbytecode* pc) |
| 51 |
: str(str), callerScript(callerScript), pc(pc) {} |
| 52 |
|
| 53 |
void trace(JSTracer* trc); |
| 54 |
}; |
| 55 |
|
| 56 |
struct EvalCacheHashPolicy { |
| 57 |
using Lookup = EvalCacheLookup; |
| 58 |
|
| 59 |
static HashNumber hash(const Lookup& l); |
| 60 |
static bool match(const EvalCacheEntry& entry, const EvalCacheLookup& l); |
| 61 |
}; |
| 62 |
|
| 63 |
using EvalCache = |
| 64 |
GCHashSet<EvalCacheEntry, EvalCacheHashPolicy, SystemAllocPolicy>; |
| 65 |
|
| 66 |
class MegamorphicCacheEntry { |
| 67 |
// Receiver object's shape. |
| 68 |
Shape* shape_ = nullptr; |
| 69 |
|
| 70 |
// The atom or symbol property being accessed. |
| 71 |
PropertyKey key_; |
| 72 |
|
| 73 |
// Slot offset and isFixedSlot flag of the data property. |
| 74 |
TaggedSlotOffset slotOffset_; |
| 75 |
|
| 76 |
// This entry is valid iff the generation matches the cache's generation. |
| 77 |
uint16_t generation_ = 0; |
| 78 |
|
| 79 |
// This encodes the number of hops on the prototype chain to get to the holder |
| 80 |
// object, along with information about the kind of property. If the high bit |
| 81 |
// is 0, the property is a data property. If the high bit is 1 and the value |
| 82 |
// is <= MaxHopsForGetterProperty, the property is a getter. Otherwise, this |
| 83 |
// is a sentinel value indicating a missing property lookup. |
| 84 |
uint8_t hopsAndKind_ = 0; |
| 85 |
|
| 86 |
friend class MegamorphicCache; |
| 87 |
|
| 88 |
public: |
| 89 |
// A simple flag for the JIT to check which, if false, lets it know that it's |
| 90 |
// just a data property N hops up the prototype chain |
| 91 |
static constexpr uint8_t NonDataPropertyFlag = 128; |
| 92 |
|
| 93 |
static constexpr uint8_t MaxHopsForGetterProperty = 253; |
| 94 |
static constexpr uint8_t NumHopsForMissingProperty = 254; |
| 95 |
static constexpr uint8_t NumHopsForMissingOwnProperty = 255; |
| 96 |
|
| 97 |
static constexpr uint8_t MaxHopsForDataProperty = 127; |
| 98 |
static constexpr uint8_t MaxHopsForAccessorProperty = 125; |
| 99 |
|
| 100 |
void init(Shape* shape, PropertyKey key, uint16_t generation, uint8_t numHops, |
| 101 |
TaggedSlotOffset slotOffset) { |
| 102 |
shape_ = shape; |
| 103 |
key_ = key; |
| 104 |
slotOffset_ = slotOffset; |
| 105 |
generation_ = generation; |
| 106 |
hopsAndKind_ = numHops; |
| 107 |
MOZ_ASSERT(hopsAndKind_ == numHops, "numHops must fit in hopsAndKind_"); |
| 108 |
} |
| 109 |
bool isMissingProperty() const { |
| 110 |
return hopsAndKind_ == NumHopsForMissingProperty; |
| 111 |
} |
| 112 |
bool isMissingOwnProperty() const { |
| 113 |
return hopsAndKind_ == NumHopsForMissingOwnProperty; |
| 114 |
} |
| 115 |
bool isAccessorProperty() const { |
| 116 |
return hopsAndKind_ >= NonDataPropertyFlag and |
| 117 |
hopsAndKind_ <= MaxHopsForGetterProperty; |
| 118 |
} |
| 119 |
bool isDataProperty() const { return !(hopsAndKind_ & NonDataPropertyFlag); } |
| 120 |
uint16_t numHops() const { |
| 121 |
if (isDataProperty()) { |
| 122 |
return hopsAndKind_; |
| 123 |
} else { |
| 124 |
MOZ_ASSERT(isAccessorProperty()); |
| 125 |
return hopsAndKind_ & ~NonDataPropertyFlag; |
| 126 |
} |
| 127 |
} |
| 128 |
TaggedSlotOffset slotOffset() const { |
| 129 |
MOZ_ASSERT(hopsAndKind_ <= MaxHopsForGetterProperty); |
| 130 |
return slotOffset_; |
| 131 |
} |
| 132 |
|
| 133 |
static constexpr size_t offsetOfShape() { |
| 134 |
return offsetof(MegamorphicCacheEntry, shape_); |
| 135 |
} |
| 136 |
|
| 137 |
static constexpr size_t offsetOfKey() { |
| 138 |
return offsetof(MegamorphicCacheEntry, key_); |
| 139 |
} |
| 140 |
|
| 141 |
static constexpr size_t offsetOfGeneration() { |
| 142 |
return offsetof(MegamorphicCacheEntry, generation_); |
| 143 |
} |
| 144 |
|
| 145 |
static constexpr size_t offsetOfSlotOffset() { |
| 146 |
return offsetof(MegamorphicCacheEntry, slotOffset_); |
| 147 |
} |
| 148 |
|
| 149 |
static constexpr size_t offsetOfHopsAndKind() { |
| 150 |
return offsetof(MegamorphicCacheEntry, hopsAndKind_); |
| 151 |
} |
| 152 |
}; |
| 153 |
|
| 154 |
// [SMDOC] Megamorphic Property Lookup Cache (MegamorphicCache) |
| 155 |
// |
| 156 |
// MegamorphicCache is a data structure used to speed up megamorphic property |
| 157 |
// lookups from JIT code. The same cache is currently used for both GetProp and |
| 158 |
// HasProp (in, hasOwnProperty) operations. |
| 159 |
// |
| 160 |
// This is implemented as a fixed-size array of entries. Lookups are performed |
| 161 |
// based on the receiver object's Shape + PropertyKey. If found in the cache, |
| 162 |
// the result of a lookup represents either: |
| 163 |
// |
| 164 |
// * A data property on the receiver or on its proto chain (stored as number of |
| 165 |
// 'hops' up the proto chain + the slot of the data property). |
| 166 |
// |
| 167 |
// * A missing property on the receiver or its proto chain. |
| 168 |
// |
| 169 |
// * A missing property on the receiver, but it might exist on the proto chain. |
| 170 |
// This lets us optimize hasOwnProperty better. |
| 171 |
// |
| 172 |
// Collisions are handled by simply overwriting the previous entry stored in the |
| 173 |
// slot. This is sufficient to achieve a high hit rate on typical web workloads |
| 174 |
// while ensuring cache lookups are always fast and simple. |
| 175 |
// |
| 176 |
// Lookups always check the receiver object's shape (ensuring the properties and |
| 177 |
// prototype are unchanged). Because the cache also caches lookups on the proto |
| 178 |
// chain, Watchtower is used to invalidate the cache when prototype objects are |
| 179 |
// mutated. This is done by incrementing the cache's generation counter to |
| 180 |
// invalidate all entries. |
| 181 |
// |
| 182 |
// The cache is also invalidated on each major GC. |
| 183 |
class MegamorphicCache { |
| 184 |
public: |
| 185 |
using Entry = MegamorphicCacheEntry; |
| 186 |
|
| 187 |
static constexpr size_t NumEntries = 1024; |
| 188 |
static constexpr uint8_t ShapeHashShift1 = |
| 189 |
mozilla::tl::FloorLog2<alignof(Shape)>::value; |
| 190 |
static constexpr uint8_t ShapeHashShift2 = |
| 191 |
ShapeHashShift1 + mozilla::tl::FloorLog2<NumEntries>::value; |
| 192 |
|
| 193 |
static_assert(mozilla::IsPowerOfTwo(alignof(Shape)) && |
| 194 |
mozilla::IsPowerOfTwo(NumEntries), |
| 195 |
"FloorLog2 is exact because alignof(Shape) and NumEntries are " |
| 196 |
"both powers of two"); |
| 197 |
|
| 198 |
private: |
| 199 |
mozilla::Array<Entry, NumEntries> entries_; |
| 200 |
|
| 201 |
// Generation counter used to invalidate all entries. |
| 202 |
uint16_t generation_ = 0; |
| 203 |
|
| 204 |
// NOTE: this logic is mirrored in MacroAssembler::emitMegamorphicCacheLookup |
| 205 |
Entry& getEntry(Shape* shape, PropertyKey key) { |
| 206 |
static_assert(mozilla::IsPowerOfTwo(NumEntries), |
| 207 |
"NumEntries must be a power-of-two for fast modulo"); |
| 208 |
uintptr_t hash = uintptr_t(shape) >> ShapeHashShift1; |
| 209 |
hash ^= uintptr_t(shape) >> ShapeHashShift2; |
| 210 |
hash += HashAtomOrSymbolPropertyKey(key); |
| 211 |
return entries_[hash % NumEntries]; |
| 212 |
} |
| 213 |
|
| 214 |
public: |
| 215 |
void bumpGeneration() { |
| 216 |
generation_++; |
| 217 |
if (generation_ == 0) { |
| 218 |
// Generation overflowed. Invalidate the whole cache. |
| 219 |
for (size_t i = 0; i < NumEntries; i++) { |
| 220 |
entries_[i].shape_ = nullptr; |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
bool isValidForLookup(const Entry& entry, Shape* shape, PropertyKey key) { |
| 225 |
return (entry.shape_ == shape && entry.key_ == key && |
| 226 |
entry.generation_ == generation_); |
| 227 |
} |
| 228 |
bool lookup(Shape* shape, PropertyKey key, Entry** entryp) { |
| 229 |
Entry& entry = getEntry(shape, key); |
| 230 |
*entryp = &entry; |
| 231 |
return isValidForLookup(entry, shape, key); |
| 232 |
} |
| 233 |
|
| 234 |
void initEntryForMissingProperty(Entry* entry, Shape* shape, |
| 235 |
PropertyKey key) { |
| 236 |
entry->init(shape, key, generation_, Entry::NumHopsForMissingProperty, |
| 237 |
TaggedSlotOffset()); |
| 238 |
} |
| 239 |
void initEntryForMissingOwnProperty(Entry* entry, Shape* shape, |
| 240 |
PropertyKey key) { |
| 241 |
entry->init(shape, key, generation_, Entry::NumHopsForMissingOwnProperty, |
| 242 |
TaggedSlotOffset()); |
| 243 |
} |
| 244 |
void initEntryForDataProperty(Entry* entry, Shape* shape, PropertyKey key, |
| 245 |
size_t numHops, TaggedSlotOffset slotOffset) { |
| 246 |
if (numHops > Entry::MaxHopsForDataProperty) { |
| 247 |
return; |
| 248 |
} |
| 249 |
entry->init(shape, key, generation_, numHops, slotOffset); |
| 250 |
} |
| 251 |
void initEntryForAccessorProperty(Entry* entry, Shape* shape, PropertyKey key, |
| 252 |
size_t numHops, |
| 253 |
TaggedSlotOffset slotOffset) { |
| 254 |
if (numHops > Entry::MaxHopsForAccessorProperty) { |
| 255 |
return; |
| 256 |
} |
| 257 |
numHops |= MegamorphicCacheEntry::NonDataPropertyFlag; |
| 258 |
entry->init(shape, key, generation_, numHops, slotOffset); |
| 259 |
} |
| 260 |
|
| 261 |
static constexpr size_t offsetOfEntries() { |
| 262 |
return offsetof(MegamorphicCache, entries_); |
| 263 |
} |
| 264 |
|
| 265 |
static constexpr size_t offsetOfGeneration() { |
| 266 |
return offsetof(MegamorphicCache, generation_); |
| 267 |
} |
| 268 |
}; |
| 269 |
|
| 270 |
class MegamorphicSetPropCacheEntry { |
| 271 |
Shape* beforeShape_ = nullptr; |
| 272 |
Shape* afterShape_ = nullptr; |
| 273 |
|
| 274 |
// The atom or symbol property being accessed. |
| 275 |
PropertyKey key_; |
| 276 |
|
| 277 |
// Slot offset and isFixedSlot flag of the data property. |
| 278 |
TaggedSlotOffset slotOffset_; |
| 279 |
|
| 280 |
// If slots need to be grown, this is the new capacity we need. |
| 281 |
uint16_t newCapacity_ = 0; |
| 282 |
|
| 283 |
// This entry is valid iff the generation matches the cache's generation. |
| 284 |
uint16_t generation_ = 0; |
| 285 |
|
| 286 |
friend class MegamorphicSetPropCache; |
| 287 |
|
| 288 |
public: |
| 289 |
void init(Shape* beforeShape, Shape* afterShape, PropertyKey key, |
| 290 |
uint16_t generation, TaggedSlotOffset slotOffset, |
| 291 |
uint16_t newCapacity) { |
| 292 |
beforeShape_ = beforeShape; |
| 293 |
afterShape_ = afterShape; |
| 294 |
key_ = key; |
| 295 |
slotOffset_ = slotOffset; |
| 296 |
newCapacity_ = newCapacity; |
| 297 |
generation_ = generation; |
| 298 |
} |
| 299 |
TaggedSlotOffset slotOffset() const { return slotOffset_; } |
| 300 |
Shape* afterShape() const { return afterShape_; } |
| 301 |
|
| 302 |
static constexpr size_t offsetOfShape() { |
| 303 |
return offsetof(MegamorphicSetPropCacheEntry, beforeShape_); |
| 304 |
} |
| 305 |
static constexpr size_t offsetOfAfterShape() { |
| 306 |
return offsetof(MegamorphicSetPropCacheEntry, afterShape_); |
| 307 |
} |
| 308 |
|
| 309 |
static constexpr size_t offsetOfKey() { |
| 310 |
return offsetof(MegamorphicSetPropCacheEntry, key_); |
| 311 |
} |
| 312 |
|
| 313 |
static constexpr size_t offsetOfNewCapacity() { |
| 314 |
return offsetof(MegamorphicSetPropCacheEntry, newCapacity_); |
| 315 |
} |
| 316 |
|
| 317 |
static constexpr size_t offsetOfGeneration() { |
| 318 |
return offsetof(MegamorphicSetPropCacheEntry, generation_); |
| 319 |
} |
| 320 |
|
| 321 |
static constexpr size_t offsetOfSlotOffset() { |
| 322 |
return offsetof(MegamorphicSetPropCacheEntry, slotOffset_); |
| 323 |
} |
| 324 |
}; |
| 325 |
|
| 326 |
class MegamorphicSetPropCache { |
| 327 |
public: |
| 328 |
using Entry = MegamorphicSetPropCacheEntry; |
| 329 |
// We can get more hits if we increase this, but this seems to be around |
| 330 |
// the sweet spot where we are getting most of the hits we would get with |
| 331 |
// an infinitely sized cache |
| 332 |
static constexpr size_t NumEntries = 1024; |
| 333 |
static constexpr uint8_t ShapeHashShift1 = |
| 334 |
mozilla::tl::FloorLog2<alignof(Shape)>::value; |
| 335 |
static constexpr uint8_t ShapeHashShift2 = |
| 336 |
ShapeHashShift1 + mozilla::tl::FloorLog2<NumEntries>::value; |
| 337 |
|
| 338 |
static_assert(mozilla::IsPowerOfTwo(alignof(Shape)) && |
| 339 |
mozilla::IsPowerOfTwo(NumEntries), |
| 340 |
"FloorLog2 is exact because alignof(Shape) and NumEntries are " |
| 341 |
"both powers of two"); |
| 342 |
|
| 343 |
private: |
| 344 |
mozilla::Array<Entry, NumEntries> entries_; |
| 345 |
|
| 346 |
// Generation counter used to invalidate all entries. |
| 347 |
uint16_t generation_ = 0; |
| 348 |
|
| 349 |
Entry& getEntry(Shape* beforeShape, PropertyKey key) { |
| 350 |
static_assert(mozilla::IsPowerOfTwo(NumEntries), |
| 351 |
"NumEntries must be a power-of-two for fast modulo"); |
| 352 |
uintptr_t hash = uintptr_t(beforeShape) >> ShapeHashShift1; |
| 353 |
hash ^= uintptr_t(beforeShape) >> ShapeHashShift2; |
| 354 |
hash += HashAtomOrSymbolPropertyKey(key); |
| 355 |
return entries_[hash % NumEntries]; |
| 356 |
} |
| 357 |
|
| 358 |
public: |
| 359 |
void bumpGeneration() { |
| 360 |
generation_++; |
| 361 |
if (generation_ == 0) { |
| 362 |
// Generation overflowed. Invalidate the whole cache. |
| 363 |
for (size_t i = 0; i < NumEntries; i++) { |
| 364 |
entries_[i].beforeShape_ = nullptr; |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
void set(Shape* beforeShape, Shape* afterShape, PropertyKey key, |
| 369 |
TaggedSlotOffset slotOffset, uint32_t newCapacity) { |
| 370 |
uint16_t newSlots = (uint16_t)newCapacity; |
| 371 |
if (newSlots != newCapacity) { |
| 372 |
return; |
| 373 |
} |
| 374 |
Entry& entry = getEntry(beforeShape, key); |
| 375 |
entry.init(beforeShape, afterShape, key, generation_, slotOffset, newSlots); |
| 376 |
} |
| 377 |
|
| 378 |
#ifdef DEBUG |
| 379 |
bool lookup(Shape* beforeShape, PropertyKey key, Entry** entryp) { |
| 380 |
Entry& entry = getEntry(beforeShape, key); |
| 381 |
*entryp = &entry; |
| 382 |
return (entry.beforeShape_ == beforeShape && entry.key_ == key && |
| 383 |
entry.generation_ == generation_); |
| 384 |
} |
| 385 |
#endif |
| 386 |
|
| 387 |
static constexpr size_t offsetOfEntries() { |
| 388 |
return offsetof(MegamorphicSetPropCache, entries_); |
| 389 |
} |
| 390 |
|
| 391 |
static constexpr size_t offsetOfGeneration() { |
| 392 |
return offsetof(MegamorphicSetPropCache, generation_); |
| 393 |
} |
| 394 |
}; |
| 395 |
|
| 396 |
// Cache for AtomizeString, mapping JSString* or JS::Latin1Char* to the |
| 397 |
// corresponding JSAtom*. The cache has three different optimizations: |
| 398 |
// |
| 399 |
// * The two most recent lookups are cached. This has a hit rate of 30-65% on |
| 400 |
// typical web workloads. |
| 401 |
// |
| 402 |
// * MruCache is used for short JS::Latin1Char strings. |
| 403 |
// |
| 404 |
// * For longer strings, there's also a JSLinearString* => JSAtom* HashMap, |
| 405 |
// because hashing the string characters repeatedly can be slow. |
| 406 |
// This map is also used by nursery GC to de-duplicate strings to atoms. |
| 407 |
// |
| 408 |
// This cache is purged on minor and major GC. |
| 409 |
class StringToAtomCache { |
| 410 |
public: |
| 411 |
struct LastLookup { |
| 412 |
JSString* string = nullptr; |
| 413 |
JSAtom* atom = nullptr; |
| 414 |
|
| 415 |
static constexpr size_t offsetOfString() { |
| 416 |
return offsetof(LastLookup, string); |
| 417 |
} |
| 418 |
|
| 419 |
static constexpr size_t offsetOfAtom() { |
| 420 |
return offsetof(LastLookup, atom); |
| 421 |
} |
| 422 |
}; |
| 423 |
static constexpr size_t NumLastLookups = 2; |
| 424 |
|
| 425 |
struct AtomTableKey { |
| 426 |
explicit AtomTableKey(const JS::Latin1Char* str, size_t len) |
| 427 |
: string_(str), length_(len) { |
| 428 |
hash_ = mozilla::HashString(string_, length_); |
| 429 |
} |
| 430 |
|
| 431 |
const JS::Latin1Char* string_; |
| 432 |
size_t length_; |
| 433 |
uint32_t hash_; |
| 434 |
}; |
| 435 |
|
| 436 |
private: |
| 437 |
struct RopeAtomCache |
| 438 |
: public mozilla::MruCache<AtomTableKey, JSAtom*, RopeAtomCache> { |
| 439 |
static HashNumber Hash(const AtomTableKey& key) { return key.hash_; } |
| 440 |
static bool Match(const AtomTableKey& key, const JSAtom* val) { |
| 441 |
JS::AutoCheckCannotGC nogc; |
| 442 |
return val->length() == key.length_ && |
| 443 |
EqualChars(key.string_, val->latin1Chars(nogc), key.length_); |
| 444 |
} |
| 445 |
}; |
| 446 |
using Map = |
| 447 |
HashMap<JSString*, JSAtom*, PointerHasher<JSString*>, SystemAllocPolicy>; |
| 448 |
Map map_; |
| 449 |
mozilla::Array<LastLookup, NumLastLookups> lastLookups_; |
| 450 |
RopeAtomCache ropeCharCache_; |
| 451 |
|
| 452 |
public: |
| 453 |
// Don't use the HashMap for short strings. Hashing them is less expensive. |
| 454 |
// But the length needs to long enough to cover common identifiers in React. |
| 455 |
static constexpr size_t MinStringLength = 39; |
| 456 |
|
| 457 |
JSAtom* lookupInMap(JSString* s) const { |
| 458 |
MOZ_ASSERT(s->inStringToAtomCache()); |
| 459 |
MOZ_ASSERT(s->length() >= MinStringLength); |
| 460 |
|
| 461 |
auto p = map_.lookup(s); |
| 462 |
JSAtom* atom = p ? p->value() : nullptr; |
| 463 |
return atom; |
| 464 |
} |
| 465 |
|
| 466 |
MOZ_ALWAYS_INLINE JSAtom* lookup(JSString* s) const { |
| 467 |
MOZ_ASSERT(!s->isAtom()); |
| 468 |
for (const LastLookup& entry : lastLookups_) { |
| 469 |
if (entry.string == s) { |
| 470 |
return entry.atom; |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
if (!s->inStringToAtomCache()) { |
| 475 |
MOZ_ASSERT(!map_.lookup(s)); |
| 476 |
return nullptr; |
| 477 |
} |
| 478 |
|
| 479 |
return lookupInMap(s); |
| 480 |
} |
| 481 |
|
| 482 |
MOZ_ALWAYS_INLINE JSAtom* lookupWithRopeChars( |
| 483 |
const JS::Latin1Char* str, size_t len, |
| 484 |
mozilla::Maybe<AtomTableKey>& key) { |
| 485 |
MOZ_ASSERT(len < MinStringLength); |
| 486 |
key.emplace(str, len); |
| 487 |
if (auto p = ropeCharCache_.Lookup(key.value())) { |
| 488 |
return p.Data(); |
| 489 |
} |
| 490 |
return nullptr; |
| 491 |
} |
| 492 |
|
| 493 |
static constexpr size_t offsetOfLastLookups() { |
| 494 |
return offsetof(StringToAtomCache, lastLookups_); |
| 495 |
} |
| 496 |
|
| 497 |
void maybePut(JSString* s, JSAtom* atom, mozilla::Maybe<AtomTableKey>& key) { |
| 498 |
if (key.isSome()) { |
| 499 |
ropeCharCache_.Put(key.value(), atom); |
| 500 |
} |
| 501 |
|
| 502 |
for (size_t i = NumLastLookups - 1; i > 0; i--) { |
| 503 |
lastLookups_[i] = lastLookups_[i - 1]; |
| 504 |
} |
| 505 |
lastLookups_[0].string = s; |
| 506 |
lastLookups_[0].atom = atom; |
| 507 |
|
| 508 |
if (s->length() < MinStringLength) { |
| 509 |
return; |
| 510 |
} |
| 511 |
if (!map_.putNew(s, atom)) { |
| 512 |
return; |
| 513 |
} |
| 514 |
s->setInStringToAtomCache(); |
| 515 |
} |
| 516 |
|
| 517 |
void purge() { |
| 518 |
map_.clearAndCompact(); |
| 519 |
for (LastLookup& entry : lastLookups_) { |
| 520 |
entry.string = nullptr; |
| 521 |
entry.atom = nullptr; |
| 522 |
} |
| 523 |
|
| 524 |
ropeCharCache_.Clear(); |
| 525 |
} |
| 526 |
}; |
| 527 |
|
| 528 |
#ifdef MOZ_EXECUTION_TRACING |
| 529 |
|
| 530 |
// Holds a handful of caches used for tracing JS execution. These effectively |
| 531 |
// hold onto IDs which let the tracer know that it has already recorded the |
| 532 |
// entity in question. They need to be cleared on a compacting GC since they |
| 533 |
// are keyed by pointers. However the IDs must continue incrementing until |
| 534 |
// the tracer is turned off since entries containing the IDs in question may |
| 535 |
// linger in the ExecutionTracer's buffer through a GC. |
| 536 |
class TracingCaches { |
| 537 |
uint32_t shapeId_ = 0; |
| 538 |
uint32_t atomId_ = 0; |
| 539 |
using TracingPointerCache = |
| 540 |
HashMap<uintptr_t, uint32_t, DefaultHasher<uintptr_t>, SystemAllocPolicy>; |
| 541 |
TracingPointerCache shapes_; |
| 542 |
TracingPointerCache atoms_; |
| 543 |
|
| 544 |
// NOTE: this cache does not need to be cleared on compaction, but still |
| 545 |
// needs to be cleared at the end of tracing. |
| 546 |
using TracingU32Set = |
| 547 |
HashSet<uint32_t, DefaultHasher<uint32_t>, SystemAllocPolicy>; |
| 548 |
TracingU32Set scriptSourcesSeen_; |
| 549 |
|
| 550 |
public: |
| 551 |
void clearOnCompaction() { |
| 552 |
atoms_.clear(); |
| 553 |
shapes_.clear(); |
| 554 |
} |
| 555 |
|
| 556 |
void clearAll() { |
| 557 |
shapeId_ = 0; |
| 558 |
atomId_ = 0; |
| 559 |
scriptSourcesSeen_.clear(); |
| 560 |
atoms_.clear(); |
| 561 |
shapes_.clear(); |
| 562 |
} |
| 563 |
|
| 564 |
enum class GetOrPutResult { |
| 565 |
OOM, |
| 566 |
NewlyAdded, |
| 567 |
WasPresent, |
| 568 |
}; |
| 569 |
|
| 570 |
GetOrPutResult getOrPutAtom(JSAtom* atom, uint32_t* id) { |
| 571 |
TracingPointerCache::AddPtr p = |
| 572 |
atoms_.lookupForAdd(reinterpret_cast<uintptr_t>(atom)); |
| 573 |
if (p) { |
| 574 |
*id = p->value(); |
| 575 |
return GetOrPutResult::WasPresent; |
| 576 |
} |
| 577 |
*id = atomId_++; |
| 578 |
if (!atoms_.add(p, reinterpret_cast<uintptr_t>(atom), *id)) { |
| 579 |
return GetOrPutResult::OOM; |
| 580 |
} |
| 581 |
return GetOrPutResult::NewlyAdded; |
| 582 |
} |
| 583 |
|
| 584 |
GetOrPutResult getOrPutShape(Shape* shape, uint32_t* id) { |
| 585 |
TracingPointerCache::AddPtr p = |
| 586 |
shapes_.lookupForAdd(reinterpret_cast<uintptr_t>(shape)); |
| 587 |
if (p) { |
| 588 |
*id = p->value(); |
| 589 |
return GetOrPutResult::WasPresent; |
| 590 |
} |
| 591 |
*id = shapeId_++; |
| 592 |
if (!shapes_.add(p, reinterpret_cast<uintptr_t>(shape), *id)) { |
| 593 |
return GetOrPutResult::OOM; |
| 594 |
} |
| 595 |
return GetOrPutResult::NewlyAdded; |
| 596 |
} |
| 597 |
|
| 598 |
// NOTE: scriptSourceId is js::ScriptSource::id value. |
| 599 |
GetOrPutResult putScriptSourceIfMissing(uint32_t scriptSourceId) { |
| 600 |
TracingU32Set::AddPtr p = scriptSourcesSeen_.lookupForAdd(scriptSourceId); |
| 601 |
if (p) { |
| 602 |
return GetOrPutResult::WasPresent; |
| 603 |
} |
| 604 |
if (!scriptSourcesSeen_.add(p, scriptSourceId)) { |
| 605 |
return GetOrPutResult::OOM; |
| 606 |
} |
| 607 |
return GetOrPutResult::NewlyAdded; |
| 608 |
} |
| 609 |
}; |
| 610 |
|
| 611 |
#endif /* MOZ_EXECUTION_TRACING */ |
| 612 |
|
| 613 |
class RuntimeCaches { |
| 614 |
public: |
| 615 |
MegamorphicCache megamorphicCache; |
| 616 |
UniquePtr<MegamorphicSetPropCache> megamorphicSetPropCache; |
| 617 |
UncompressedSourceCache uncompressedSourceCache; |
| 618 |
EvalCache evalCache; |
| 619 |
StringToAtomCache stringToAtomCache; |
| 620 |
|
| 621 |
#ifdef MOZ_EXECUTION_TRACING |
| 622 |
TracingCaches tracingCaches; |
| 623 |
#endif |
| 624 |
|
| 625 |
// Delazification: Cache binding for runtime objects which are used during |
| 626 |
// delazification to quickly resolve NameLocation of bindings without linearly |
| 627 |
// iterating over the list of bindings. |
| 628 |
frontend::RuntimeScopeBindingCache scopeCache; |
| 629 |
|
| 630 |
void sweepAfterMinorGC(JSTracer* trc) { evalCache.traceWeak(trc); } |
| 631 |
#ifdef JSGC_HASH_TABLE_CHECKS |
| 632 |
void checkEvalCacheAfterMinorGC(); |
| 633 |
#endif |
| 634 |
|
| 635 |
void purgeForCompaction() { |
| 636 |
evalCache.clear(); |
| 637 |
stringToAtomCache.purge(); |
| 638 |
megamorphicCache.bumpGeneration(); |
| 639 |
if (megamorphicSetPropCache) { |
| 640 |
// MegamorphicSetPropCache can be null if we failed out of |
| 641 |
// JSRuntime::init. We will then try to destroy the runtime which will |
| 642 |
// do a GC and land us here. |
| 643 |
megamorphicSetPropCache->bumpGeneration(); |
| 644 |
} |
| 645 |
scopeCache.purge(); |
| 646 |
#ifdef MOZ_EXECUTION_TRACING |
| 647 |
tracingCaches.clearOnCompaction(); |
| 648 |
#endif |
| 649 |
} |
| 650 |
|
| 651 |
void purge() { |
| 652 |
purgeForCompaction(); |
| 653 |
uncompressedSourceCache.purge(); |
| 654 |
} |
| 655 |
}; |
| 656 |
|
| 657 |
} // namespace js |
| 658 |
|
| 659 |
#endif /* vm_Caches_h */ |