2014-05-30 09:40:33 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-10-18 22:25:30 +04:00
|
|
|
/* 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"
|
2014-05-30 09:40:33 +04:00
|
|
|
|
2015-08-21 10:05:40 +03:00
|
|
|
// 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>
|
|
|
|
|
2019-10-04 23:44:08 +03:00
|
|
|
#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"
|
2017-03-09 05:11:15 +03:00
|
|
|
|
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;
|
2013-10-18 22:25:30 +04:00
|
|
|
using namespace mozilla::Compression;
|
|
|
|
|
|
|
|
/* Our wrappers */
|
|
|
|
|
2014-05-30 09:40:33 +04:00
|
|
|
size_t LZ4::compress(const char* aSource, size_t aInputSize, char* aDest) {
|
|
|
|
CheckedInt<int> inputSizeChecked = aInputSize;
|
|
|
|
MOZ_ASSERT(inputSizeChecked.isValid());
|
2017-09-12 16:00:37 +03:00
|
|
|
return LZ4_compress_default(aSource, aDest, inputSizeChecked.value(),
|
|
|
|
LZ4_compressBound(inputSizeChecked.value()));
|
2013-10-18 22:25:30 +04:00
|
|
|
}
|
|
|
|
|
2014-05-30 09:40:33 +04:00
|
|
|
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());
|
2017-09-12 16:00:37 +03:00
|
|
|
return LZ4_compress_default(aSource, aDest, inputSizeChecked.value(),
|
|
|
|
maxOutputSizeChecked.value());
|
2013-10-18 22:25:30 +04:00
|
|
|
}
|
|
|
|
|
2014-05-30 09:40:33 +04:00
|
|
|
bool LZ4::decompress(const char* aSource, size_t aInputSize, char* aDest,
|
2014-07-11 06:10:17 +04:00
|
|
|
size_t aMaxOutputSize, size_t* aOutputSize) {
|
2014-05-30 09:40:33 +04:00
|
|
|
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;
|
|
|
|
}
|
2013-10-18 22:25:30 +04:00
|
|
|
|
2014-05-30 09:40:33 +04:00
|
|
|
*aOutputSize = 0;
|
|
|
|
return false;
|
2013-10-18 22:25:30 +04:00
|
|
|
}
|
|
|
|
|
2017-09-07 20:40:59 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-08-07 10:42:50 +03:00
|
|
|
return Span{static_cast<const char*>(mWriteBuffer.Elements()), headerSize};
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-08-07 10:42:50 +03:00
|
|
|
return Span{static_cast<const char*>(mWriteBuffer.Elements()), outputSize};
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-08-07 10:42:50 +03:00
|
|
|
return Span{static_cast<const char*>(mWriteBuffer.Elements()), outputSize};
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|