Bug 1111564 - Backout 10692972a7b6 (bug 1084177) for increasing memory usage. r=me

This commit is contained in:
Benjamin Peterson 2014-12-16 14:13:36 -05:00
Родитель 856a41ceb3
Коммит 19a2e8054b
5 изменённых файлов: 52 добавлений и 89 удалений

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

@ -1752,7 +1752,7 @@ ScriptSource::setSourceCopy(ExclusiveContext *cx, SourceBufferHolder &srcBuf,
}
SourceCompressionTask::ResultType
SourceCompressionTask::work(Compressor &comp)
SourceCompressionTask::work()
{
// Try to keep the maximum memory usage down by only allocating half the
// size of the string, first.
@ -1762,7 +1762,8 @@ SourceCompressionTask::work(Compressor &comp)
if (!compressed)
return OOM;
if (!comp.prepare(reinterpret_cast<const unsigned char *>(ss->uncompressedChars()), inputBytes))
Compressor comp(reinterpret_cast<const unsigned char *>(ss->uncompressedChars()), inputBytes);
if (!comp.init())
return OOM;
comp.setOutput((unsigned char *) compressed, firstSize);

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

@ -6,13 +6,8 @@
#include "vm/Compression.h"
#include "mozilla/DebugOnly.h"
#include <zlib.h>
#include "js/Utility.h"
using mozilla::DebugOnly;
using namespace js;
static void *
@ -27,91 +22,79 @@ zlib_free(void *cx, void *addr)
js_free(addr);
}
Compressor::Compressor(const unsigned char *inp, size_t inplen)
: inp(inp),
inplen(inplen),
outbytes(0),
initialized(false)
{
MOZ_ASSERT(inplen > 0);
zs.opaque = nullptr;
zs.next_in = (Bytef *)inp;
zs.avail_in = 0;
zs.next_out = nullptr;
zs.avail_out = 0;
zs.zalloc = zlib_alloc;
zs.zfree = zlib_free;
}
Compressor::~Compressor()
{
if (initialized) {
int ret = deflateEnd(zs);
int ret = deflateEnd(&zs);
if (ret != Z_OK) {
// If we finished early, we can get a Z_DATA_ERROR.
MOZ_ASSERT(ret == Z_DATA_ERROR);
MOZ_ASSERT(uInt(zs->next_in - inp) < inplen || !zs->avail_out);
MOZ_ASSERT(uInt(zs.next_in - inp) < inplen || !zs.avail_out);
}
}
js_free(zs);
}
bool
Compressor::prepare(const unsigned char *inp_, size_t inplen_)
Compressor::init()
{
MOZ_ASSERT(inplen_ > 0);
if (inplen_ >= UINT32_MAX)
if (inplen >= UINT32_MAX)
return false;
// zlib is slow and we'd rather be done compression sooner
// even if it means decompression is slower which penalizes
// Function.toString()
int ret = deflateInit(&zs, Z_BEST_SPEED);
if (ret != Z_OK) {
MOZ_ASSERT(ret == Z_MEM_ERROR);
return false;
if (!zs) {
zs = js_pod_malloc<z_stream>();
if (!zs)
return false;
zs->zalloc = zlib_alloc;
zs->zfree = zlib_free;
}
inp = inp_;
inplen = inplen_;
outbytes = 0;
zs->opaque = nullptr;
zs->next_in = (Bytef *)inp;
zs->avail_in = 0;
zs->next_out = nullptr;
zs->avail_out = 0;
if (initialized) {
DebugOnly<int> ret = deflateReset(zs);
MOZ_ASSERT(ret == Z_OK);
} else {
// zlib is slow and we'd rather be done compression sooner even if it
// means decompression is slower which penalizes Function.toString()
int ret = deflateInit(zs, Z_BEST_SPEED);
if (ret != Z_OK) {
MOZ_ASSERT(ret == Z_MEM_ERROR);
return false;
}
initialized = true;
}
initialized = true;
return true;
}
void
Compressor::setOutput(unsigned char *out, size_t outlen)
{
MOZ_ASSERT(initialized);
MOZ_ASSERT(outlen > outbytes);
zs->next_out = out + outbytes;
zs->avail_out = outlen - outbytes;
zs.next_out = out + outbytes;
zs.avail_out = outlen - outbytes;
}
Compressor::Status
Compressor::compressMore()
{
MOZ_ASSERT(initialized);
MOZ_ASSERT(zs->next_out);
uInt left = inplen - (zs->next_in - inp);
MOZ_ASSERT(zs.next_out);
uInt left = inplen - (zs.next_in - inp);
bool done = left <= CHUNKSIZE;
if (done)
zs->avail_in = left;
else if (zs->avail_in == 0)
zs->avail_in = CHUNKSIZE;
Bytef *oldout = zs->next_out;
int ret = deflate(zs, done ? Z_FINISH : Z_NO_FLUSH);
outbytes += zs->next_out - oldout;
zs.avail_in = left;
else if (zs.avail_in == 0)
zs.avail_in = CHUNKSIZE;
Bytef *oldout = zs.next_out;
int ret = deflate(&zs, done ? Z_FINISH : Z_NO_FLUSH);
outbytes += zs.next_out - oldout;
if (ret == Z_MEM_ERROR) {
zs->avail_out = 0;
zs.avail_out = 0;
return OOM;
}
if (ret == Z_BUF_ERROR || (done && ret == Z_OK)) {
MOZ_ASSERT(zs->avail_out == 0);
MOZ_ASSERT(zs.avail_out == 0);
return MOREOUTPUT;
}
MOZ_ASSERT_IF(!done, ret == Z_OK);

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

@ -7,19 +7,17 @@
#ifndef vm_Compression_h
#define vm_Compression_h
#include "mozilla/NullPtr.h"
#include <zlib.h>
#include "jstypes.h"
struct z_stream_s; // from <zlib.h>
namespace js {
class Compressor
{
/* Number of bytes we should hand to zlib each compressMore() call. */
static const size_t CHUNKSIZE = 2048;
struct z_stream_s *zs;
z_stream zs;
const unsigned char *inp;
size_t inplen;
size_t outbytes;
@ -33,22 +31,12 @@ class Compressor
OOM
};
Compressor()
: zs(nullptr),
initialized(false)
{}
Compressor(const unsigned char *inp, size_t inplen);
~Compressor();
/*
* Prepare the compressor to process the string |inp| of length
* |inplen|. Return false if initialization failed (usually OOM).
*
* The compressor may be reused for a different input by calling prepare()
* again.
*/
bool prepare(const unsigned char *inp, size_t inplen);
bool init();
void setOutput(unsigned char *out, size_t outlen);
size_t outWritten() const { return outbytes; }
/* Compress some of the input. Return CONTINUE if it should be called again. */
/* Compress some of the input. Return true if it should be called again. */
Status compressMore();
};

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

@ -493,10 +493,8 @@ GlobalHelperThreadState::finish()
{
if (threads) {
MOZ_ASSERT(CanUseExtraThreads());
for (size_t i = 0; i < threadCount; i++) {
for (size_t i = 0; i < threadCount; i++)
threads[i].destroy();
threads[i].~HelperThread();
}
js_free(threads);
}
@ -1212,7 +1210,7 @@ HelperThread::handleCompressionWorkload()
{
AutoUnlockHelperThreadState unlock;
compressionTask->result = compressionTask->work(sourceCompressor);
compressionTask->result = compressionTask->work();
}
compressionTask->helperThread = nullptr;

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

@ -21,7 +21,6 @@
#include "frontend/TokenStream.h"
#include "jit/Ion.h"
#include "vm/Compression.h"
namespace js {
@ -309,12 +308,6 @@ struct HelperThread
/* Any source being compressed on this thread. */
SourceCompressionTask *compressionTask;
/*
* Compressor that is used for servicing SourceCompressionTasks on this
* thread.
*/
Compressor sourceCompressor;
/* Any GC state for background sweeping or allocating being performed. */
GCHelperState *gcHelperState;
@ -550,7 +543,7 @@ struct SourceCompressionTask
complete();
}
ResultType work(Compressor &comp);
ResultType work();
bool complete();
void abort() { abort_ = true; }
bool active() const { return !!ss; }