forked from mirrors/gecko-dev
The overall goal of this patch is to make the StartupCache accessible anywhere.
There's two main pieces to that equation:
1. Allowing it to be accessed off main thread, which means modifying the
mutex usage to ensure that all data accessed from non-main threads is
protected.
2. Allowing it to be accessed out of the chrome process, which means passing
a handle to a shared cache buffer down to child processes.
Number 1 is somewhat fiddly, but it's all generally straightforward work. I'll
hope that the comments and the code are sufficient to explain what's going on
there.
Number 2 has some decisions to be made:
- The first decision was to pass a handle to a frozen chunk of memory down to
all child processes, rather than passing a handle to an actual file. There's
two reasons for this: 1) since we want to compress the underlying file on
disk, giving that file to child processes would mean they have to decompress
it themselves, eating CPU time. 2) since they would have to decompress it
themselves, they would have to allocate the memory for the decompressed
buffers, meaning they cannot all simply share one big decompressed buffer.
- The drawback of this decision is that we have to load and decompress the
buffer up front, before we spawn any child processes. We attempt to
mitigate this by keeping track of all the entries that child processes
access, and only including those in the frozen decompressed shared buffer.
- We base our implementation of this approach off of the shared preferences
implementation. Hopefully I got all of the pieces to fit together
correctly. They seem to work in local testing and on try, but I think
they require a set of experienced eyes looking carefully at them.
- Another decision was whether to send the handles to the buffers over IPC or
via command line. We went with the command line approach, because the startup
cache would need to be accessed very early on in order to ensure we do not
read from any omnijars, and we could not make that work via IPC.
- Unfortunately this means adding another hard-coded FD, similar to
kPrefMapFileDescriptor. It seems like at the very least we need to rope all
of these together into one place, but I think that should be filed as a
follow-up?
Lastly, because this patch is a bit of a monster to review - first, thank you
for looking at it, and second, the reason we're invested in this is because we
saw a >10% improvement in cold startup times on reference hardware, with a p
value less than 0.01. It's still not abundantly clear how reference hardware
numbers translate to numbers on release, and they certainly don't translate
well to Nightly numbers, but it's enough to convince me that it's worth some
effort.
Depends on D78584
Differential Revision: https://phabricator.services.mozilla.com/D77635
224 lines
5 KiB
C++
224 lines
5 KiB
C++
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
|
|
/* 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/. */
|
|
|
|
#ifndef IOBuffers_h
|
|
#define IOBuffers_h
|
|
|
|
#include "mozilla/Assertions.h"
|
|
#include "mozilla/CheckedInt.h"
|
|
#include "mozilla/EndianUtils.h"
|
|
#include "mozilla/EnumSet.h"
|
|
#include "mozilla/Range.h"
|
|
#include "mozilla/Span.h"
|
|
#include "nsString.h"
|
|
#include "nsTArray.h"
|
|
|
|
namespace mozilla {
|
|
namespace loader {
|
|
|
|
class OutputBuffer {
|
|
public:
|
|
OutputBuffer() {}
|
|
|
|
uint8_t* write(size_t size) {
|
|
auto buf = data.AppendElements(size);
|
|
cursor_ += size;
|
|
return buf;
|
|
}
|
|
|
|
void codeUint8(const uint8_t& val) { *write(sizeof val) = val; }
|
|
|
|
template <typename T>
|
|
void codeUint8(const EnumSet<T>& val) {
|
|
// EnumSets are always represented as uint32_t values, so we need to
|
|
// assert that the value actually fits in a uint8 before writing it.
|
|
uint32_t value = val.serialize();
|
|
codeUint8(CheckedUint8(value).value());
|
|
}
|
|
|
|
void codeUint16(const uint16_t& val) {
|
|
LittleEndian::writeUint16(write(sizeof val), val);
|
|
}
|
|
|
|
void codeUint32(const uint32_t& val) {
|
|
LittleEndian::writeUint32(write(sizeof val), val);
|
|
}
|
|
|
|
void codeString(const nsCString& str) {
|
|
auto len = CheckedUint16(str.Length()).value();
|
|
|
|
codeUint16(len);
|
|
memcpy(write(len), str.BeginReading(), len);
|
|
}
|
|
|
|
size_t cursor() const { return cursor_; }
|
|
|
|
uint8_t* Get() { return data.Elements(); }
|
|
|
|
const uint8_t* Get() const { return data.Elements(); }
|
|
|
|
private:
|
|
nsTArray<uint8_t> data;
|
|
size_t cursor_ = 0;
|
|
};
|
|
|
|
// This is similar to OutputBuffer, but with a fixed-size buffer, rather than
|
|
// a dynamically growing one. This is currently used in order to share
|
|
// StartupCache data across processes.
|
|
class PreallocatedOutputBuffer {
|
|
public:
|
|
explicit PreallocatedOutputBuffer(Range<uint8_t>& buffer) : data(buffer) {}
|
|
|
|
uint8_t* write(size_t size) {
|
|
MOZ_ASSERT(checkCapacity(size));
|
|
|
|
auto buf = &data[cursor_];
|
|
cursor_ += size;
|
|
return buf;
|
|
}
|
|
|
|
bool codeUint8(const uint8_t& val) {
|
|
if (checkCapacity(sizeof val)) {
|
|
*write(sizeof val) = val;
|
|
}
|
|
return !error_;
|
|
}
|
|
|
|
template <typename T>
|
|
bool codeUint8(const EnumSet<T>& val) {
|
|
return codeUint8(val.serialize());
|
|
}
|
|
|
|
bool codeUint16(const uint16_t& val) {
|
|
if (checkCapacity(sizeof val)) {
|
|
LittleEndian::writeUint16(write(sizeof val), val);
|
|
}
|
|
return !error_;
|
|
}
|
|
|
|
bool codeUint32(const uint32_t& val) {
|
|
if (checkCapacity(sizeof val)) {
|
|
LittleEndian::writeUint32(write(sizeof val), val);
|
|
}
|
|
return !error_;
|
|
}
|
|
|
|
bool codeString(const nsCString& str) {
|
|
uint16_t len = CheckedUint16(str.Length()).value();
|
|
if (codeUint16(len)) {
|
|
if (checkCapacity(len)) {
|
|
memcpy(write(len), str.get(), len);
|
|
}
|
|
}
|
|
return !error_;
|
|
}
|
|
|
|
bool error() { return error_; }
|
|
|
|
bool finished() { return error_ || !remainingCapacity(); }
|
|
|
|
size_t remainingCapacity() { return data.length() - cursor_; }
|
|
|
|
size_t cursor() const { return cursor_; }
|
|
|
|
const uint8_t* Get() const { return data.begin().get(); }
|
|
|
|
private:
|
|
bool checkCapacity(size_t size) {
|
|
if (size > remainingCapacity()) {
|
|
error_ = true;
|
|
}
|
|
return !error_;
|
|
}
|
|
|
|
bool error_ = false;
|
|
|
|
public:
|
|
Range<uint8_t>& data;
|
|
size_t cursor_ = 0;
|
|
};
|
|
|
|
class InputBuffer {
|
|
public:
|
|
explicit InputBuffer(const Range<uint8_t>& buffer) : data(buffer) {}
|
|
|
|
const uint8_t* read(size_t size) {
|
|
MOZ_ASSERT(checkCapacity(size));
|
|
|
|
auto buf = &data[cursor_];
|
|
cursor_ += size;
|
|
return buf;
|
|
}
|
|
|
|
bool codeUint8(uint8_t& val) {
|
|
if (checkCapacity(sizeof val)) {
|
|
val = *read(sizeof val);
|
|
}
|
|
return !error_;
|
|
}
|
|
|
|
template <typename T>
|
|
bool codeUint8(EnumSet<T>& val) {
|
|
uint8_t value;
|
|
if (codeUint8(value)) {
|
|
val.deserialize(value);
|
|
}
|
|
return !error_;
|
|
}
|
|
|
|
bool codeUint16(uint16_t& val) {
|
|
if (checkCapacity(sizeof val)) {
|
|
val = LittleEndian::readUint16(read(sizeof val));
|
|
}
|
|
return !error_;
|
|
}
|
|
|
|
bool codeUint32(uint32_t& val) {
|
|
if (checkCapacity(sizeof val)) {
|
|
val = LittleEndian::readUint32(read(sizeof val));
|
|
}
|
|
return !error_;
|
|
}
|
|
|
|
bool codeString(nsCString& str) {
|
|
uint16_t len;
|
|
if (codeUint16(len)) {
|
|
if (checkCapacity(len)) {
|
|
str.SetLength(len);
|
|
memcpy(str.BeginWriting(), read(len), len);
|
|
}
|
|
}
|
|
return !error_;
|
|
}
|
|
|
|
bool error() { return error_; }
|
|
|
|
bool finished() { return error_ || !remainingCapacity(); }
|
|
|
|
size_t remainingCapacity() { return data.length() - cursor_; }
|
|
|
|
size_t cursor() const { return cursor_; }
|
|
|
|
const uint8_t* Get() const { return data.begin().get(); }
|
|
|
|
private:
|
|
bool checkCapacity(size_t size) {
|
|
if (size > remainingCapacity()) {
|
|
error_ = true;
|
|
}
|
|
return !error_;
|
|
}
|
|
|
|
bool error_ = false;
|
|
|
|
public:
|
|
const Range<uint8_t>& data;
|
|
size_t cursor_ = 0;
|
|
};
|
|
|
|
} // namespace loader
|
|
} // namespace mozilla
|
|
|
|
#endif // IOBuffers_h
|