DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Implementation

Mercurial (6863f516ba38)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef vm_Caches_h
#define vm_Caches_h

#include <new>

#include "jsmath.h"

#include "frontend/SourceNotes.h"
#include "gc/Tracer.h"
#include "js/RootingAPI.h"
#include "js/TypeDecls.h"
#include "js/UniquePtr.h"
#include "vm/ArrayObject.h"
#include "vm/JSAtom.h"
#include "vm/JSObject.h"
#include "vm/JSScript.h"
#include "vm/NativeObject.h"

namespace js {

/*
 * GetSrcNote cache to avoid O(n^2) growth in finding a source note for a
 * given pc in a script. We use the script->code pointer to tag the cache,
 * instead of the script address itself, so that source notes are always found
 * by offset from the bytecode with which they were generated.
 */
struct GSNCache {
  typedef HashMap<jsbytecode*, jssrcnote*, PointerHasher<jsbytecode*>,
                  SystemAllocPolicy>
      Map;

  jsbytecode* code;
  Map map;

  GSNCache() : code(nullptr) {}

  void purge();
};

struct EvalCacheEntry {
  JSLinearString* str;
  JSScript* script;
  JSScript* callerScript;
  jsbytecode* pc;

  // We sweep this cache before a nursery collection to remove entries with
  // string keys in the nursery.
  //
  // The entire cache is purged on a major GC, so we don't need to sweep it
  // then.
  bool needsSweep() { return !str->isTenured(); }
};

struct EvalCacheLookup {
  explicit EvalCacheLookup(JSContext* cx) : str(cx), callerScript(cx) {}
  RootedLinearString str;
  RootedScript callerScript;
  MOZ_INIT_OUTSIDE_CTOR jsbytecode* pc;
};

struct EvalCacheHashPolicy {
  typedef EvalCacheLookup Lookup;

  static HashNumber hash(const Lookup& l);
  static bool match(const EvalCacheEntry& entry, const EvalCacheLookup& l);
};

typedef GCHashSet<EvalCacheEntry, EvalCacheHashPolicy, SystemAllocPolicy>
    EvalCache;

/*
 * Cache for speeding up repetitive creation of objects in the VM.
 * When an object is created which matches the criteria in the 'key' section
 * below, an entry is filled with the resulting object.
 */
class NewObjectCache {
  /* Statically asserted to be equal to sizeof(JSObject_Slots16) */
  static const unsigned MAX_OBJ_SIZE = 4 * sizeof(void*) + 16 * sizeof(Value);

  static void staticAsserts() {
    JS_STATIC_ASSERT(NewObjectCache::MAX_OBJ_SIZE == sizeof(JSObject_Slots16));
    JS_STATIC_ASSERT(gc::AllocKind::OBJECT_LAST ==
                     gc::AllocKind::OBJECT16_BACKGROUND);
  }

  struct Entry {
    /* Class of the constructed object. */
    const Class* clasp;

    /*
     * Key with one of three possible values:
     *
     * - Global for the object. The object must have a standard class for
     *   which the global's prototype can be determined, and the object's
     *   parent will be the global.
     *
     * - Prototype for the object (cannot be global). The object's parent
     *   will be the prototype's parent.
     *
     * - Type for the object. The object's parent will be the type's
     *   prototype's parent.
     */
    gc::Cell* key;

    /* Allocation kind for the constructed object. */
    gc::AllocKind kind;

    /* Number of bytes to copy from the template object. */
    uint32_t nbytes;

    /*
     * Template object to copy from, with the initial values of fields,
     * fixed slots (undefined) and private data (nullptr).
     */
    char templateObject[MAX_OBJ_SIZE];
  };

  using EntryArray = Entry[41];  // TODO: reconsider size;
  EntryArray entries;

 public:
  using EntryIndex = int;

  NewObjectCache()
      : entries{}  // zeroes out the array
  {}

  void purge() {
    new (&entries) EntryArray{};  // zeroes out the array
  }

  /* Remove any cached items keyed on moved objects. */
  void clearNurseryObjects(JSRuntime* rt);

  /*
   * Get the entry index for the given lookup, return whether there was a hit
   * on an existing entry.
   */
  inline bool lookupProto(const Class* clasp, JSObject* proto,
                          gc::AllocKind kind, EntryIndex* pentry);
  inline bool lookupGlobal(const Class* clasp, js::GlobalObject* global,
                           gc::AllocKind kind, EntryIndex* pentry);

  bool lookupGroup(js::ObjectGroup* group, gc::AllocKind kind,
                   EntryIndex* pentry) {
    return lookup(group->clasp(), group, kind, pentry);
  }

  /*
   * Return a new object from a cache hit produced by a lookup method, or
   * nullptr if returning the object could possibly trigger GC (does not
   * indicate failure).
   */
  inline NativeObject* newObjectFromHit(JSContext* cx, EntryIndex entry,
                                        js::gc::InitialHeap heap);

  /* Fill an entry after a cache miss. */
  void fillProto(EntryIndex entry, const Class* clasp, js::TaggedProto proto,
                 gc::AllocKind kind, NativeObject* obj);

  inline void fillGlobal(EntryIndex entry, const Class* clasp,
                         js::GlobalObject* global, gc::AllocKind kind,
                         NativeObject* obj);

  void fillGroup(EntryIndex entry, js::ObjectGroup* group, gc::AllocKind kind,
                 NativeObject* obj) {
    MOZ_ASSERT(obj->group() == group);
    return fill(entry, group->clasp(), group, kind, obj);
  }

  /* Invalidate any entries which might produce an object with shape/proto. */
  void invalidateEntriesForShape(JSContext* cx, HandleShape shape,
                                 HandleObject proto);

 private:
  EntryIndex makeIndex(const Class* clasp, gc::Cell* key, gc::AllocKind kind) {
    uintptr_t hash = (uintptr_t(clasp) ^ uintptr_t(key)) + size_t(kind);
    return hash % mozilla::ArrayLength(entries);
  }

  bool lookup(const Class* clasp, gc::Cell* key, gc::AllocKind kind,
              EntryIndex* pentry) {
    *pentry = makeIndex(clasp, key, kind);
    Entry* entry = &entries[*pentry];

    // N.B. Lookups with the same clasp/key but different kinds map to
    // different entries.
    return entry->clasp == clasp && entry->key == key;
  }

  void fill(EntryIndex entry_, const Class* clasp, gc::Cell* key,
            gc::AllocKind kind, NativeObject* obj) {
    MOZ_ASSERT(unsigned(entry_) < mozilla::ArrayLength(entries));
    MOZ_ASSERT(entry_ == makeIndex(clasp, key, kind));
    Entry* entry = &entries[entry_];

    MOZ_ASSERT(!obj->hasDynamicSlots());
    MOZ_ASSERT(obj->hasEmptyElements() || obj->is<ArrayObject>());

    entry->clasp = clasp;
    entry->key = key;
    entry->kind = kind;

    entry->nbytes = gc::Arena::thingSize(kind);
    js_memcpy(&entry->templateObject, obj, entry->nbytes);
  }

  static void copyCachedToObject(NativeObject* dst, NativeObject* src,
                                 gc::AllocKind kind) {
    js_memcpy(dst, src, gc::Arena::thingSize(kind));

    // Initialize with barriers
    dst->initGroup(src->group());
    dst->initShape(src->shape());
  }
};

class RuntimeCaches {
 public:
  js::GSNCache gsnCache;
  js::NewObjectCache newObjectCache;
  js::UncompressedSourceCache uncompressedSourceCache;
  js::EvalCache evalCache;

  void purgeForMinorGC(JSRuntime* rt) {
    newObjectCache.clearNurseryObjects(rt);
    evalCache.sweep();
  }

  void purgeForCompaction() {
    newObjectCache.purge();
    evalCache.clear();
  }

  void purge() {
    purgeForCompaction();
    gsnCache.purge();
    uncompressedSourceCache.purge();
  }
};

}  // namespace js

#endif /* vm_Caches_h */