Bug 720753 - hoist ToSourceCache from JSCompartment into JSRuntime (r=cdleary)

This commit is contained in:
Luke Wagner 2012-05-03 09:12:48 +02:00
parent 79d0b6f69c
commit 5db288b202
5 changed files with 51 additions and 26 deletions

View File

@ -190,6 +190,20 @@ struct ConservativeGCData
}
};
class ToSourceCache
{
typedef HashMap<JSFunction *,
JSString *,
DefaultHasher<JSFunction *>,
SystemAllocPolicy> Map;
Map *map_;
public:
ToSourceCache() : map_(NULL) {}
JSString *lookup(JSFunction *fun);
void put(JSFunction *fun, JSString *);
void purge();
};
class NativeIterCache
{
static const size_t SIZE = size_t(1) << 8;
@ -740,6 +754,7 @@ struct JSRuntime : js::RuntimeFriendFields
js::PropertyCache propertyCache;
js::NewObjectCache newObjectCache;
js::NativeIterCache nativeIterCache;
js::ToSourceCache toSourceCache;
/* State used by jsdtoa.cpp. */
DtoaState *dtoaState;

View File

@ -551,8 +551,6 @@ JSCompartment::purge()
listHeadp = &script->evalHashLink();
}
}
toSourceCache.destroyIfConstructed();
}
void

View File

@ -58,11 +58,6 @@
namespace js {
typedef HashMap<JSFunction *,
JSString *,
DefaultHasher<JSFunction *>,
SystemAllocPolicy> ToSourceCache;
/* Defined in jsapi.cpp */
extern Class dummy_class;
@ -272,9 +267,6 @@ struct JSCompartment
unsigned debugModeBits; // see debugMode() below
public:
typedef js::Maybe<js::ToSourceCache> LazyToSourceCache;
LazyToSourceCache toSourceCache;
JSCompartment(JSRuntime *rt);
~JSCompartment();

View File

@ -552,6 +552,36 @@ JS_FRIEND_DATA(Class) js::FunctionClass = {
fun_trace
};
JSString *
ToSourceCache::lookup(JSFunction *fun)
{
if (!map_)
return NULL;
if (Map::Ptr p = map_->lookup(fun))
return p->value;
return NULL;
}
void
ToSourceCache::put(JSFunction *fun, JSString *str)
{
if (!map_) {
map_ = OffTheBooks::new_<Map>();
if (!map_)
return;
map_->init();
}
(void) map_->put(fun, str);
}
void
ToSourceCache::purge()
{
Foreground::delete_(map_);
map_ = NULL;
}
JSString *
fun_toStringHelper(JSContext *cx, JSObject *obj, unsigned indent)
{
@ -569,28 +599,17 @@ fun_toStringHelper(JSContext *cx, JSObject *obj, unsigned indent)
if (!fun)
return NULL;
if (!indent && !cx->compartment->toSourceCache.empty()) {
ToSourceCache::Ptr p = cx->compartment->toSourceCache.ref().lookup(fun);
if (p)
return p->value;
if (!indent) {
if (JSString *str = cx->runtime->toSourceCache.lookup(fun))
return str;
}
JSString *str = JS_DecompileFunction(cx, fun, indent);
if (!str)
return NULL;
if (!indent) {
Maybe<ToSourceCache> &lazy = cx->compartment->toSourceCache;
if (lazy.empty()) {
lazy.construct();
if (!lazy.ref().init())
return NULL;
}
if (!lazy.ref().put(fun, str))
return NULL;
}
if (!indent)
cx->runtime->toSourceCache.put(fun, str);
return str;
}

View File

@ -2892,6 +2892,7 @@ PurgeRuntime(JSTracer *trc)
rt->propertyCache.purge(rt);
rt->newObjectCache.purge();
rt->nativeIterCache.purge();
rt->toSourceCache.purge();
for (ContextIter acx(rt); !acx.done(); acx.next())
acx->purge();