Bug 1687338 - Transcode XDR-Stencil header directly to target buffer. r=arai

Differential Revision: https://phabricator.services.mozilla.com/D102243
This commit is contained in:
Ted Campbell 2021-01-19 00:41:54 +00:00
Родитель b49b911ba5
Коммит 716649077e
2 изменённых файлов: 28 добавлений и 20 удалений

Просмотреть файл

@ -756,21 +756,25 @@ XDRResult XDRIncrementalStencilEncoder::linearize(JS::TranscodeBuffer& buffer,
JS::IsTranscodingBytecodeAligned(buffer.begin())); JS::IsTranscodingBytecodeAligned(buffer.begin()));
MOZ_ASSERT(JS::IsTranscodingBytecodeOffsetAligned(buffer.length())); MOZ_ASSERT(JS::IsTranscodingBytecodeOffsetAligned(buffer.length()));
Rooted<ScriptSourceHolder> holder(cx(), ss); // Use the output buffer directly. The caller may have already have data in
uint32_t nchunks = 1 + encodedFunctions_.count(); // the buffer so ensure we skip over it.
XDRBuffer<XDR_ENCODE> outputBuf(cx(), buffer, buffer.length());
switchToHeaderBuf(); // Code the header directly in the output buffer.
MOZ_TRY(XDRStencilHeader(this, nullptr, &holder, &nchunks)); {
switchToMainBuf(); switchToBuffer(&outputBuf);
size_t totalLength = buffer.length() + header_.length() + slices_.length(); Rooted<ScriptSourceHolder> holder(cx(), ss);
if (!buffer.reserve(totalLength)) { uint32_t nchunks = 1 + encodedFunctions_.count();
ReportOutOfMemory(cx()); MOZ_TRY(XDRStencilHeader(this, nullptr, &holder, &nchunks));
return fail(JS::TranscodeResult_Throw);
switchToMainBuf();
} }
buffer.infallibleAppend(header_.begin(), header_.length()); // The accumlated transcode data can now be copied to the output buffer.
buffer.infallibleAppend(slices_.begin(), slices_.length()); if (!buffer.append(slices_.begin(), slices_.length())) {
return fail(JS::TranscodeResult_Throw);
}
return Ok(); return Ok();
} }

Просмотреть файл

@ -676,20 +676,16 @@ class XDRIncrementalEncoderBase : public XDREncoder {
protected: protected:
JS::TranscodeBuffer slices_; JS::TranscodeBuffer slices_;
// Header buffer.
JS::TranscodeBuffer header_;
XDRBuffer<XDR_ENCODE> headerBuf_;
public: public:
explicit XDRIncrementalEncoderBase(JSContext* cx) explicit XDRIncrementalEncoderBase(JSContext* cx)
: XDREncoder(cx, slices_, 0), headerBuf_(cx, header_, 0) {} : XDREncoder(cx, slices_, 0) {}
void switchToBuffer(XDRBuffer<XDR_ENCODE>* target) { buf = target; }
bool isMainBuf() override { return buf == &mainBuf; } bool isMainBuf() override { return buf == &mainBuf; }
// Switch to streaming into the main buffer. // Switch to streaming into the main buffer.
void switchToMainBuf() override { buf = &mainBuf; } void switchToMainBuf() override { switchToBuffer(&mainBuf); }
// Switch to streaming into the header buffer.
void switchToHeaderBuf() override { buf = &headerBuf_; }
virtual XDRResult linearize(JS::TranscodeBuffer& buffer, virtual XDRResult linearize(JS::TranscodeBuffer& buffer,
js::ScriptSource* ss) { js::ScriptSource* ss) {
@ -755,6 +751,10 @@ class XDRIncrementalEncoder : public XDRIncrementalEncoderBase {
HashMap<AutoXDRTree::Key, SlicesNode, DefaultHasher<AutoXDRTree::Key>, HashMap<AutoXDRTree::Key, SlicesNode, DefaultHasher<AutoXDRTree::Key>,
SystemAllocPolicy>; SystemAllocPolicy>;
// Header buffer.
JS::TranscodeBuffer header_;
XDRBuffer<XDR_ENCODE> headerBuf_;
// Atom buffer. // Atom buffer.
JS::TranscodeBuffer atoms_; JS::TranscodeBuffer atoms_;
XDRBuffer<XDR_ENCODE> atomBuf_; XDRBuffer<XDR_ENCODE> atomBuf_;
@ -776,6 +776,7 @@ class XDRIncrementalEncoder : public XDRIncrementalEncoderBase {
public: public:
explicit XDRIncrementalEncoder(JSContext* cx) explicit XDRIncrementalEncoder(JSContext* cx)
: XDRIncrementalEncoderBase(cx), : XDRIncrementalEncoderBase(cx),
headerBuf_(cx, header_, 0),
atomBuf_(cx, atoms_, 0), atomBuf_(cx, atoms_, 0),
scope_(nullptr), scope_(nullptr),
node_(nullptr), node_(nullptr),
@ -787,7 +788,10 @@ class XDRIncrementalEncoder : public XDRIncrementalEncoderBase {
uint32_t& natoms() override { return natoms_; } uint32_t& natoms() override { return natoms_; }
// Switch from streaming into the main buffer into the atom buffer. // Switch from streaming into the main buffer into the atom buffer.
void switchToAtomBuf() override { buf = &atomBuf_; } void switchToAtomBuf() override { switchToBuffer(&atomBuf_); }
// Switch to streaming into the header buffer.
void switchToHeaderBuf() override { switchToBuffer(&headerBuf_); }
bool hasAtomMap() const override { return true; } bool hasAtomMap() const override { return true; }
XDRAtomMap& atomMap() override { return atomMap_; } XDRAtomMap& atomMap() override { return atomMap_; }