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/. */
7 #include "vm/StencilCache.h"
9 #include "frontend/CompilationStencil.h"
10 #include "js/experimental/JSStencil.h"
11 #include "vm/MutexIDs.h"
13 js::StencilCache::StencilCache()
14 : cache(js::mutexid::StencilCache
), enabled(false) {}
16 js::StencilCache::AccessKey
js::StencilCache::isSourceCached(
19 return cache
.noAccess();
22 AccessKey
lock(cache
.lock());
24 // As we checked the flag before taking the lock, we have to check again to
25 // avoid races on the cache manipulation.
26 return cache
.noAccess();
28 if (!lock
->watched
.has(src
)) {
29 // If the source does not have any cached function, and we do not expect to
30 // cache any delazification in the future, then skip any cache handling.
31 return cache
.noAccess();
36 bool js::StencilCache::startCaching(RefPtr
<ScriptSource
>&& src
) {
37 auto guard
= cache
.lock();
38 if (!guard
->watched
.putNew(std::move(src
))) {
45 js::frontend::CompilationStencil
* js::StencilCache::lookup(
46 AccessKey
& guard
, const StencilContext
& key
) {
47 auto ptr
= guard
->functions
.lookup(key
);
52 return ptr
->value().get();
55 bool js::StencilCache::putNew(AccessKey
& guard
, const StencilContext
& key
,
56 js::frontend::CompilationStencil
* value
) {
57 return guard
->functions
.putNew(key
, value
);
60 // Important: This function should not be called within a scope checking for
61 // isSourceCached, as this would cause a dead-lock.
62 void js::StencilCache::clearAndDisable() {
63 auto guard
= cache
.lock();
64 guard
->functions
.clearAndCompact();
65 guard
->watched
.clearAndCompact();
69 /* static */ js::DelazificationCache
js::DelazificationCache::singleton
;