Skip to content

Commit

Permalink
Bug 1675962 - Part 4: Add cache for CompilationAtomCache for delazifi…
Browse files Browse the repository at this point in the history
…cations. r=tcampbell

Differential Revision: https://phabricator.services.mozilla.com/D97118
  • Loading branch information
arai-a committed Dec 12, 2020
1 parent e789d5f commit 071a211
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
5 changes: 4 additions & 1 deletion js/src/frontend/BytecodeCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,10 @@ void CompilationAtomCache::trace(JSTracer* trc) { atoms_.trace(trc); }

void CompilationInfo::trace(JSTracer* trc) { input.trace(trc); }

void CompilationInfoVector::trace(JSTracer* trc) { initial.trace(trc); }
void CompilationInfoVector::trace(JSTracer* trc) {
initial.trace(trc);
delazificationAtomCache.trace(trc);
}

void CompilationGCOutput::trace(JSTracer* trc) {
TraceNullableRoot(trc, &script, "compilation-gc-output-script");
Expand Down
9 changes: 8 additions & 1 deletion js/src/frontend/CompilationInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,16 @@ struct ScopeContext {
};

struct CompilationAtomCache {
public:
using AtomCacheVector = JS::GCVector<JSAtom*, 0, js::SystemAllocPolicy>;

private:
// Atoms lowered into or converted from CompilationStencil.parserAtomData.
//
// This field is here instead of in CompilationGCOutput because atoms lowered
// from JSAtom is part of input (enclosing scope bindings, lazy function name,
// etc), and having 2 vectors in both input/output is error prone.
JS::GCVector<JSAtom*, 0, js::SystemAllocPolicy> atoms_;
AtomCacheVector atoms_;

public:
JSAtom* getExistingAtomAt(ParserAtomIndex index) const;
Expand All @@ -108,6 +111,9 @@ struct CompilationAtomCache {
bool setAtomAt(JSContext* cx, ParserAtomIndex index, JSAtom* atom);
bool allocate(JSContext* cx, size_t length);

void stealBuffer(AtomCacheVector& atoms);
void returnBuffer(AtomCacheVector& atoms);

void trace(JSTracer* trc);
} JS_HAZ_GC_POINTER;

Expand Down Expand Up @@ -488,6 +494,7 @@ struct CompilationInfoVector {
CompilationInfo initial;
Vector<CompilationStencil, 0, js::SystemAllocPolicy> delazifications;
FunctionIndexVector delazificationIndices;
CompilationAtomCache::AtomCacheVector delazificationAtomCache;

CompilationInfoVector(JSContext* cx,
const JS::ReadOnlyCompileOptions& options)
Expand Down
39 changes: 29 additions & 10 deletions js/src/frontend/Stencil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include "mozilla/RefPtr.h" // RefPtr

#include <memory> // std::uninitialized_fill

#include "frontend/AbstractScopePtr.h" // ScopeIndex
#include "frontend/BytecodeSection.h" // EmitScriptThingsVector
#include "frontend/CompilationInfo.h" // CompilationInfo, CompilationInfoVector, CompilationGCOutput
Expand Down Expand Up @@ -748,25 +750,25 @@ bool CompilationInfoVector::instantiateStencilsAfterPreparation(
BaseScript* lazy = fun->baseScript();
MOZ_ASSERT(!lazy->hasBytecode());

CompilationInput input(initial.input.options);
input.initFromLazy(lazy);
Rooted<CompilationInput> input(cx, CompilationInput(initial.input.options));
input.get().initFromLazy(lazy);

input.get().atomCache.stealBuffer(delazificationAtomCache);

if (!CompilationInfo::prepareInputAndStencilForInstantiate(
cx, input, delazification)) {
return false;
}
if (!CompilationInfo::prepareGCOutputForInstantiate(
cx, delazification, gcOutputForDelazification)) {
return false;
}
if (!CompilationInfo::instantiateStencilsAfterPreparation(
cx, input, delazification, gcOutputForDelazification)) {
cx, input.get(), delazification, gcOutputForDelazification)) {
return false;
}

// Destroy elements, without unreserving.
gcOutputForDelazification.functions.shrinkTo(0);
gcOutputForDelazification.scopes.shrinkTo(0);
gcOutputForDelazification.functions.clear();
gcOutputForDelazification.scopes.clear();

input.get().atomCache.returnBuffer(delazificationAtomCache);
}

return true;
Expand Down Expand Up @@ -824,8 +826,11 @@ bool CompilationInfoVector::prepareForInstantiate(

size_t maxScriptDataLength = 0;
size_t maxScopeDataLength = 0;
size_t maxParserAtomDataLength = 0;
for (auto& delazification : delazifications) {
// FIXME: Prepare atomCache vector.
if (maxParserAtomDataLength < delazification.parserAtomData.length()) {
maxParserAtomDataLength = delazification.parserAtomData.length();
}
if (maxScriptDataLength < delazification.scriptData.length()) {
maxScriptDataLength = delazification.scriptData.length();
}
Expand All @@ -834,6 +839,10 @@ bool CompilationInfoVector::prepareForInstantiate(
}
}

if (!delazificationAtomCache.resize(maxParserAtomDataLength)) {
ReportOutOfMemory(cx);
return false;
}
if (!gcOutput.functions.reserve(maxScriptDataLength)) {
ReportOutOfMemory(cx);
return false;
Expand Down Expand Up @@ -1723,6 +1732,16 @@ bool CompilationAtomCache::allocate(JSContext* cx, size_t length) {
return true;
}

void CompilationAtomCache::stealBuffer(AtomCacheVector& atoms) {
atoms_ = std::move(atoms);
// Destroy elements, without unreserving.
atoms_.clear();
}

void CompilationAtomCache::returnBuffer(AtomCacheVector& atoms) {
atoms = std::move(atoms_);
}

const ParserAtom* CompilationStencil::getParserAtomAt(
JSContext* cx, TaggedParserAtomIndex taggedIndex) const {
if (taggedIndex.isParserAtomIndex()) {
Expand Down
14 changes: 14 additions & 0 deletions js/src/jit-test/tests/xdr/delazifications-atoms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
let s = `
function f1() { return "Atom_f1"; }
function f2() { return "Atom_f2"; }
function f3() { return "Atom_f3"; }
assertEq(f1(), "Atom_f1");
assertEq(f2(), "Atom_f2");
assertEq(f3(), "Atom_f3");
`;

let c = cacheEntry(s);
let g = newGlobal();
evaluate(c, {saveIncrementalBytecode:true});
evaluate(c, {global:g, loadBytecode:true});

0 comments on commit 071a211

Please sign in to comment.