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

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

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