gecko-dev/mfbt/Compression.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

193 строки
6.6 KiB
C++
Исходник Обычный вид История

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/Compression.h"
#include "mozilla/CheckedInt.h"
// Without including <string>, MSVC 2015 complains about e.g. the impossibility
// to convert `const void* const` to `void*` when calling memchr from
// corecrt_memory.h.
#include <string>
#include "lz4/lz4.h"
Bug 1550108 - Change StartupCache format from zip to custom r=froydnj I am not aware of anything that depends on StartupCache being a zip file, and since I want to use lz4 compression because inflate is showing up quite a lot in profiles, it's simplest to just use a custom format. This loosely mimicks the ScriptPreloader code, with a few diversions: - Obviously the contents of the cache are compressed. I used lz4 for this as I hit the same file size as deflate at a compression level of 1, which is what the StartupCache was using previously, while decompressing an order of magnitude faster. Seemed like the most conservative change to make. I think it's worth investigating what the impact of slower algs with higher ratios would be, but for right now I settled on this. We'd probably want to look at zstd next. - I use streaming compression for this via lz4frame. This is not strictly necessary, but has the benefit of not requiring as much memory for large buffers, as well as giving us a built-in checksum, rather than relying on the much slower CRC that we were doing with the zip-based approach. - I coded the serialization of the headers inline, since I had to jump back to add the offset and compressed size, which would make the nice Code(...) method for the ScriptPreloader stuff rather more complex. Open to cleaner solutions, but moving it out just felt like extra hoops for the reader to jump through to understand without the benefit of being more concise. Differential Revision: https://phabricator.services.mozilla.com/D34652 --HG-- extra : moz-landing-system : lando
2019-10-04 23:44:59 +03:00
#include "lz4/lz4frame.h"
Bug 1550108 - Change StartupCache format from zip to custom r=froydnj I am not aware of anything that depends on StartupCache being a zip file, and since I want to use lz4 compression because inflate is showing up quite a lot in profiles, it's simplest to just use a custom format. This loosely mimicks the ScriptPreloader code, with a few diversions: - Obviously the contents of the cache are compressed. I used lz4 for this as I hit the same file size as deflate at a compression level of 1, which is what the StartupCache was using previously, while decompressing an order of magnitude faster. Seemed like the most conservative change to make. I think it's worth investigating what the impact of slower algs with higher ratios would be, but for right now I settled on this. We'd probably want to look at zstd next. - I use streaming compression for this via lz4frame. This is not strictly necessary, but has the benefit of not requiring as much memory for large buffers, as well as giving us a built-in checksum, rather than relying on the much slower CRC that we were doing with the zip-based approach. - I coded the serialization of the headers inline, since I had to jump back to add the offset and compressed size, which would make the nice Code(...) method for the ScriptPreloader stuff rather more complex. Open to cleaner solutions, but moving it out just felt like extra hoops for the reader to jump through to understand without the benefit of being more concise. Differential Revision: https://phabricator.services.mozilla.com/D34652 --HG-- extra : moz-landing-system : lando
2019-10-04 23:44:59 +03:00
using namespace mozilla;
using namespace mozilla::Compression;
/* Our wrappers */
size_t LZ4::compress(const char* aSource, size_t aInputSize, char* aDest) {
CheckedInt<int> inputSizeChecked = aInputSize;
MOZ_ASSERT(inputSizeChecked.isValid());
return LZ4_compress_default(aSource, aDest, inputSizeChecked.value(),
LZ4_compressBound(inputSizeChecked.value()));
}
size_t LZ4::compressLimitedOutput(const char* aSource, size_t aInputSize,
char* aDest, size_t aMaxOutputSize) {
CheckedInt<int> inputSizeChecked = aInputSize;
MOZ_ASSERT(inputSizeChecked.isValid());
CheckedInt<int> maxOutputSizeChecked = aMaxOutputSize;
MOZ_ASSERT(maxOutputSizeChecked.isValid());
return LZ4_compress_default(aSource, aDest, inputSizeChecked.value(),
maxOutputSizeChecked.value());
}
bool LZ4::decompress(const char* aSource, char* aDest, size_t aOutputSize) {
CheckedInt<int> outputSizeChecked = aOutputSize;
MOZ_ASSERT(outputSizeChecked.isValid());
int ret = LZ4_decompress_fast(aSource, aDest, outputSizeChecked.value());
return ret >= 0;
}
bool LZ4::decompress(const char* aSource, size_t aInputSize, char* aDest,
size_t aMaxOutputSize, size_t* aOutputSize) {
CheckedInt<int> maxOutputSizeChecked = aMaxOutputSize;
MOZ_ASSERT(maxOutputSizeChecked.isValid());
CheckedInt<int> inputSizeChecked = aInputSize;
MOZ_ASSERT(inputSizeChecked.isValid());
int ret = LZ4_decompress_safe(aSource, aDest, inputSizeChecked.value(),
maxOutputSizeChecked.value());
if (ret >= 0) {
*aOutputSize = ret;
return true;
}
*aOutputSize = 0;
return false;
}
bool LZ4::decompressPartial(const char* aSource, size_t aInputSize, char* aDest,
size_t aMaxOutputSize, size_t* aOutputSize) {
CheckedInt<int> maxOutputSizeChecked = aMaxOutputSize;
MOZ_ASSERT(maxOutputSizeChecked.isValid());
CheckedInt<int> inputSizeChecked = aInputSize;
MOZ_ASSERT(inputSizeChecked.isValid());
int ret = LZ4_decompress_safe_partial(
aSource, aDest, inputSizeChecked.value(), maxOutputSizeChecked.value(),
maxOutputSizeChecked.value());
if (ret >= 0) {
*aOutputSize = ret;
return true;
}
*aOutputSize = 0;
return false;
}
Bug 1550108 - Change StartupCache format from zip to custom r=froydnj I am not aware of anything that depends on StartupCache being a zip file, and since I want to use lz4 compression because inflate is showing up quite a lot in profiles, it's simplest to just use a custom format. This loosely mimicks the ScriptPreloader code, with a few diversions: - Obviously the contents of the cache are compressed. I used lz4 for this as I hit the same file size as deflate at a compression level of 1, which is what the StartupCache was using previously, while decompressing an order of magnitude faster. Seemed like the most conservative change to make. I think it's worth investigating what the impact of slower algs with higher ratios would be, but for right now I settled on this. We'd probably want to look at zstd next. - I use streaming compression for this via lz4frame. This is not strictly necessary, but has the benefit of not requiring as much memory for large buffers, as well as giving us a built-in checksum, rather than relying on the much slower CRC that we were doing with the zip-based approach. - I coded the serialization of the headers inline, since I had to jump back to add the offset and compressed size, which would make the nice Code(...) method for the ScriptPreloader stuff rather more complex. Open to cleaner solutions, but moving it out just felt like extra hoops for the reader to jump through to understand without the benefit of being more concise. Differential Revision: https://phabricator.services.mozilla.com/D34652 --HG-- extra : moz-landing-system : lando
2019-10-04 23:44:59 +03:00
LZ4FrameCompressionContext::LZ4FrameCompressionContext(int aCompressionLevel,
size_t aMaxSrcSize,
bool aChecksum,
bool aStableSrc)
: mContext(nullptr),
mCompressionLevel(aCompressionLevel),
mGenerateChecksum(aChecksum),
mStableSrc(aStableSrc),
mMaxSrcSize(aMaxSrcSize),
mWriteBufLen(0) {
LZ4F_contentChecksum_t checksum =
mGenerateChecksum ? LZ4F_contentChecksumEnabled : LZ4F_noContentChecksum;
LZ4F_preferences_t prefs = {
{
LZ4F_max256KB,
LZ4F_blockLinked,
checksum,
},
mCompressionLevel,
};
mWriteBufLen = LZ4F_compressBound(mMaxSrcSize, &prefs);
LZ4F_errorCode_t err = LZ4F_createCompressionContext(&mContext, LZ4F_VERSION);
MOZ_RELEASE_ASSERT(!LZ4F_isError(err));
}
LZ4FrameCompressionContext::~LZ4FrameCompressionContext() {
LZ4F_freeCompressionContext(mContext);
}
Result<Span<const char>, size_t> LZ4FrameCompressionContext::BeginCompressing(
Span<char> aWriteBuffer) {
mWriteBuffer = aWriteBuffer;
LZ4F_contentChecksum_t checksum =
mGenerateChecksum ? LZ4F_contentChecksumEnabled : LZ4F_noContentChecksum;
LZ4F_preferences_t prefs = {
{
LZ4F_max256KB,
LZ4F_blockLinked,
checksum,
},
mCompressionLevel,
};
size_t headerSize = LZ4F_compressBegin(mContext, mWriteBuffer.Elements(),
mWriteBufLen, &prefs);
if (LZ4F_isError(headerSize)) {
return Err(headerSize);
}
return MakeSpan(static_cast<const char*>(mWriteBuffer.Elements()),
headerSize);
}
Result<Span<const char>, size_t>
LZ4FrameCompressionContext::ContinueCompressing(Span<const char> aInput) {
LZ4F_compressOptions_t opts = {};
opts.stableSrc = (uint32_t)mStableSrc;
size_t outputSize =
LZ4F_compressUpdate(mContext, mWriteBuffer.Elements(), mWriteBufLen,
aInput.Elements(), aInput.Length(), &opts);
if (LZ4F_isError(outputSize)) {
return Err(outputSize);
}
return MakeSpan(static_cast<const char*>(mWriteBuffer.Elements()),
outputSize);
}
Result<Span<const char>, size_t> LZ4FrameCompressionContext::EndCompressing() {
size_t outputSize =
LZ4F_compressEnd(mContext, mWriteBuffer.Elements(), mWriteBufLen,
/* options */ nullptr);
if (LZ4F_isError(outputSize)) {
return Err(outputSize);
}
return MakeSpan(static_cast<const char*>(mWriteBuffer.Elements()),
outputSize);
}
LZ4FrameDecompressionContext::LZ4FrameDecompressionContext(bool aStableDest)
: mContext(nullptr), mStableDest(aStableDest) {
LZ4F_errorCode_t err =
LZ4F_createDecompressionContext(&mContext, LZ4F_VERSION);
MOZ_RELEASE_ASSERT(!LZ4F_isError(err));
}
LZ4FrameDecompressionContext::~LZ4FrameDecompressionContext() {
LZ4F_freeDecompressionContext(mContext);
}
Result<LZ4FrameDecompressionResult, size_t>
LZ4FrameDecompressionContext::Decompress(Span<char> aOutput,
Span<const char> aInput) {
LZ4F_decompressOptions_t opts = {};
opts.stableDst = (uint32_t)mStableDest;
size_t outBytes = aOutput.Length();
size_t inBytes = aInput.Length();
size_t result = LZ4F_decompress(mContext, aOutput.Elements(), &outBytes,
aInput.Elements(), &inBytes, &opts);
if (LZ4F_isError(result)) {
return Err(result);
}
LZ4FrameDecompressionResult decompressionResult = {};
decompressionResult.mFinished = !result;
decompressionResult.mSizeRead = inBytes;
decompressionResult.mSizeWritten = outBytes;
return decompressionResult;
}