2010-08-12 23:37:44 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +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/. */
|
2010-08-12 23:37:44 +04:00
|
|
|
|
|
|
|
#ifndef StartupCache_h_
|
|
|
|
#define StartupCache_h_
|
|
|
|
|
2020-03-17 15:42:12 +03:00
|
|
|
#include <utility>
|
|
|
|
|
2010-08-12 23:37:44 +04:00
|
|
|
#include "nsClassHashtable.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
2015-05-22 23:29:51 +03:00
|
|
|
#include "nsTArray.h"
|
2021-03-23 13:36:39 +03:00
|
|
|
#include "nsTHashSet.h"
|
2020-09-14 20:00:53 +03:00
|
|
|
#include "nsTStringHasher.h" // mozilla::DefaultHasher<nsCString>
|
2010-08-12 23:37:44 +04:00
|
|
|
#include "nsZipArchive.h"
|
|
|
|
#include "nsITimer.h"
|
2013-11-07 09:35:30 +04:00
|
|
|
#include "nsIMemoryReporter.h"
|
2010-08-12 23:37:44 +04:00
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsIObserver.h"
|
2017-10-20 20:40:50 +03:00
|
|
|
#include "nsIObjectOutputStream.h"
|
2010-08-12 23:37:44 +04:00
|
|
|
#include "nsIFile.h"
|
2012-06-20 07:45:32 +04:00
|
|
|
#include "mozilla/Attributes.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 "mozilla/AutoMemMap.h"
|
|
|
|
#include "mozilla/Compression.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
Bug 1614795 - use the background task queue for startupcache writes, r=dthayer,decoder
Prior to this patch, the startupcache created its own mWriteThread off which it
wrote to disk. It's initialized by MaybeSpawnWriteThread, which got called
at shutdown, to do the shutdown write if there was any reason to do so, and
from a timer that is re-initialized after every addition to the startup cache,
to run 60s after the last change to the cache.
It then joined that write thread on the main thread (in other words, blocks
on that off-main-thread write completing from the main thread) when:
- xpcom-shutdown fired
- the startupcache itself gets destroyed
- someone calls any of:
* HasEntry
* GetBuffer
* PutBuffer
* InvalidateCache
This patch removes the separate write thread, and instead dispatches a task to
the background task queue, indicating it can block. The task is started in
the same circumstances where we previously used to write (timer from the last
PutBuffer call, and shutdown if necessary).
To ensure it cannot be trying to use the data it writes out (mTable) from
the other thread while that data changes on the main thread, we use a mutex.
The task locks the mutex before starting, and unlocks when finished.
Enumerating the cases that we used to block on joining the thread:
In terms of application shutdown, we expect the background task queue to
either finish the write task, or fail to run it if it hasn't started it yet.
In the FastStartup case, we check if a write was necessary; if so, we
attempt to gain the lock without waiting. If we're successful, the write has
not yet started, and we instead run the write on the main thread. Otherwise,
we retry gaining the lock, blocking this time, thus guaranteeing the
off-the-main-thread write completes.
The task keeps a reference to the startupcache object, so it cannot be
destroyed while the task is pending.
Because the write does not modify `mTable`, and neither does `HasEntry`,
we do not need to do anything there.
In the `GetBuffer` case, we do not modify the table unless we have to read
the entry off disk (memmapped into `mCacheData`). This can only happen if
`mCacheData.initialized()` returns true, and we specifically call
`mCacheData.reset()` before firing off the write task to avoid this.
`mCacheData` is only re-initialized if someone calls `LoadArchive()`,
which can only happen from `Init()` (which is guaranteed not to run
again because this is a singleton), or `InvalidateCache()`, where we lock
the mutex (see below). So this is safe - but we assert on the lock to try
and avoid people breaking this chain of assumptions in the future.
When `PutBuffer` is called, we try to lock the mutex - but if locking fails
(ie the background thread is writing), we simply fail to store the entry
in the startupcache. In practice, this should be rare - it'd happen if
new calls to PutBuffer happen while writing during shutdown (when really,
we don't care) or when it's been 60 seconds since the last PutBuffer so
we started writing the startupcache.
When InvalidateCache is called, we lock the mutex - we shouldn't try to
write while invalidating, or invalidate while writing. This may be slow,
but in practice nothing should call `InvalidateCache` except developer
restarts or the `-purgecaches` commandline flag, so it shouldn't
matter a great deal.
Differential Revision: https://phabricator.services.mozilla.com/D70413
--HG--
extra : moz-landing-system : lando
2020-04-15 23:43:44 +03:00
|
|
|
#include "mozilla/Mutex.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 "mozilla/Result.h"
|
2016-02-18 20:26:28 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2013-11-28 05:05:00 +04:00
|
|
|
|
2010-08-12 23:37:44 +04:00
|
|
|
/**
|
|
|
|
* The StartupCache is a persistent cache of simple key-value pairs,
|
|
|
|
* where the keys are null-terminated c-strings and the values are
|
|
|
|
* arbitrary data, passed as a (char*, size) tuple.
|
|
|
|
*
|
|
|
|
* Clients should use the GetSingleton() static method to access the cache. It
|
2014-09-26 08:20:23 +04:00
|
|
|
* will be available from the end of XPCOM init (NS_InitXPCOM3 in
|
2010-08-12 23:37:44 +04:00
|
|
|
* XPCOMInit.cpp), until XPCOM shutdown begins. The GetSingleton() method will
|
|
|
|
* return null if the cache is unavailable. The cache is only provided for
|
|
|
|
* libxul builds -- it will fail to link in non-libxul builds. The XPCOM
|
|
|
|
* interface is provided only to allow compiled-code tests; clients should avoid
|
|
|
|
* using it.
|
|
|
|
*
|
|
|
|
* The API provided is very simple: GetBuffer() returns a buffer that was
|
|
|
|
* previously stored in the cache (if any), and PutBuffer() inserts a buffer
|
|
|
|
* into the cache. GetBuffer returns a new buffer, and the caller must take
|
|
|
|
* ownership of it. PutBuffer will assert if the client attempts to insert a
|
|
|
|
* buffer with the same name as an existing entry. The cache makes a copy of the
|
|
|
|
* passed-in buffer, so client retains ownership.
|
|
|
|
*
|
|
|
|
* InvalidateCache() may be called if a client suspects data corruption
|
|
|
|
* or wishes to invalidate for any other reason. This will remove all existing
|
2012-10-11 12:17:15 +04:00
|
|
|
* cache data. Additionally, the static method IgnoreDiskCache() can be called
|
|
|
|
* if it is believed that the on-disk cache file is itself corrupt. This call
|
|
|
|
* implicitly calls InvalidateCache (if the singleton has been initialized) to
|
|
|
|
* ensure any data already read from disk is discarded. The cache will not load
|
|
|
|
* data from the disk file until a successful write occurs.
|
|
|
|
*
|
2010-08-12 23:37:44 +04:00
|
|
|
* Finally, getDebugObjectOutputStream() allows debug code to wrap an
|
|
|
|
* objectstream with a debug objectstream, to check for multiply-referenced
|
|
|
|
* objects. These will generally fail to deserialize correctly, unless they are
|
|
|
|
* stateless singletons or the client maintains their own object data map for
|
|
|
|
* deserialization.
|
|
|
|
*
|
|
|
|
* Writes before the final-ui-startup notification are placed in an intermediate
|
|
|
|
* cache in memory, then written out to disk at a later time, to get writes off
|
|
|
|
* the startup path. In any case, clients should not rely on being able to
|
|
|
|
* GetBuffer() data that is written to the cache, since it may not have been
|
|
|
|
* written to disk or another client may have invalidated the cache. In other
|
|
|
|
* words, it should be used as a cache only, and not a reliable persistent
|
|
|
|
* store.
|
|
|
|
*
|
|
|
|
* Some utility functions are provided in StartupCacheUtils. These functions
|
|
|
|
* wrap the buffers into object streams, which may be useful for serializing
|
|
|
|
* objects. Note the above caution about multiply-referenced objects, though --
|
|
|
|
* the streams are just as 'dumb' as the underlying buffers about
|
|
|
|
* multiply-referenced objects. They just provide some convenience in writing
|
|
|
|
* out data.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace mozilla {
|
2013-11-26 03:57:40 +04:00
|
|
|
|
2010-08-12 23:37:44 +04:00
|
|
|
namespace scache {
|
|
|
|
|
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
|
|
|
struct StartupCacheEntry {
|
2020-09-14 20:00:53 +03:00
|
|
|
UniquePtr<char[]> mData;
|
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
|
|
|
uint32_t mOffset;
|
|
|
|
uint32_t mCompressedSize;
|
|
|
|
uint32_t mUncompressedSize;
|
|
|
|
int32_t mHeaderOffsetInFile;
|
|
|
|
int32_t mRequestedOrder;
|
2020-09-14 20:00:53 +03:00
|
|
|
bool mRequested;
|
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
|
|
|
|
|
|
|
MOZ_IMPLICIT StartupCacheEntry(uint32_t aOffset, uint32_t aCompressedSize,
|
2020-09-14 20:00:53 +03:00
|
|
|
uint32_t aUncompressedSize)
|
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
|
|
|
: mData(nullptr),
|
|
|
|
mOffset(aOffset),
|
|
|
|
mCompressedSize(aCompressedSize),
|
|
|
|
mUncompressedSize(aUncompressedSize),
|
|
|
|
mHeaderOffsetInFile(0),
|
2020-09-14 20:00:53 +03:00
|
|
|
mRequestedOrder(0),
|
|
|
|
mRequested(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
|
|
|
|
|
|
|
StartupCacheEntry(UniquePtr<char[]> aData, size_t aLength,
|
2020-09-14 20:00:53 +03:00
|
|
|
int32_t aRequestedOrder)
|
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
|
|
|
: mData(std::move(aData)),
|
|
|
|
mOffset(0),
|
|
|
|
mCompressedSize(0),
|
|
|
|
mUncompressedSize(aLength),
|
|
|
|
mHeaderOffsetInFile(0),
|
2020-09-14 20:00:53 +03:00
|
|
|
mRequestedOrder(0),
|
|
|
|
mRequested(true) {}
|
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
|
|
|
|
|
|
|
struct Comparator {
|
2020-09-14 20:00:53 +03:00
|
|
|
using Value = std::pair<const nsCString*, StartupCacheEntry*>;
|
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
|
|
|
|
|
|
|
bool Equals(const Value& a, const Value& b) const {
|
2020-09-14 20:00:53 +03:00
|
|
|
return a.second->mRequestedOrder == b.second->mRequestedOrder;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
bool LessThan(const Value& a, const Value& b) const {
|
2020-09-14 20:00:53 +03:00
|
|
|
return a.second->mRequestedOrder < b.second->mRequestedOrder;
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
2010-08-12 23:37:44 +04:00
|
|
|
|
|
|
|
// We don't want to refcount StartupCache, and ObserverService wants to
|
|
|
|
// refcount its listeners, so we'll let it refcount this instead.
|
2015-03-21 19:28:04 +03:00
|
|
|
class StartupCacheListener final : public nsIObserver {
|
2020-03-10 11:48:37 +03:00
|
|
|
~StartupCacheListener() = default;
|
2013-07-19 06:24:14 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2010-08-12 23:37:44 +04:00
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
};
|
|
|
|
|
2013-12-08 09:39:47 +04:00
|
|
|
class StartupCache : public nsIMemoryReporter {
|
2010-08-12 23:37:44 +04:00
|
|
|
friend class StartupCacheListener;
|
2013-11-26 03:57:40 +04:00
|
|
|
|
2010-08-12 23:37:44 +04:00
|
|
|
public:
|
2013-12-08 09:39:47 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIMEMORYREPORTER
|
2010-08-12 23:37:44 +04:00
|
|
|
|
|
|
|
// StartupCache methods. See above comments for a more detailed description.
|
|
|
|
|
2019-10-04 23:44:32 +03:00
|
|
|
// true if the archive has an entry for the buffer or not.
|
|
|
|
bool HasEntry(const char* id);
|
|
|
|
|
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
|
|
|
// Returns a buffer that was previously stored, caller does not take ownership
|
|
|
|
nsresult GetBuffer(const char* id, const char** outbuf, uint32_t* length);
|
2010-08-12 23:37:44 +04:00
|
|
|
|
2018-02-17 02:30:47 +03:00
|
|
|
// Stores a buffer. Caller yields ownership.
|
2020-09-14 20:00:53 +03:00
|
|
|
nsresult PutBuffer(const char* id, UniquePtr<char[]>&& inbuf,
|
|
|
|
uint32_t length);
|
2010-08-12 23:37:44 +04:00
|
|
|
|
2020-09-14 20:00:53 +03:00
|
|
|
// Removes the cache file.
|
|
|
|
void InvalidateCache(bool memoryOnly = false);
|
2010-08-12 23:37:44 +04:00
|
|
|
|
2022-03-22 18:19:47 +03:00
|
|
|
// If some event knowingly re-generates the startup cache (like live language
|
|
|
|
// switching) count these events in order to allow them.
|
|
|
|
void CountAllowedInvalidation();
|
|
|
|
|
2020-02-12 22:02:12 +03:00
|
|
|
// For use during shutdown - this will write the startupcache's data
|
|
|
|
// to disk if the timer hasn't already gone off.
|
|
|
|
void MaybeInitShutdownWrite();
|
|
|
|
|
Bug 1614795 - use the background task queue for startupcache writes, r=dthayer,decoder
Prior to this patch, the startupcache created its own mWriteThread off which it
wrote to disk. It's initialized by MaybeSpawnWriteThread, which got called
at shutdown, to do the shutdown write if there was any reason to do so, and
from a timer that is re-initialized after every addition to the startup cache,
to run 60s after the last change to the cache.
It then joined that write thread on the main thread (in other words, blocks
on that off-main-thread write completing from the main thread) when:
- xpcom-shutdown fired
- the startupcache itself gets destroyed
- someone calls any of:
* HasEntry
* GetBuffer
* PutBuffer
* InvalidateCache
This patch removes the separate write thread, and instead dispatches a task to
the background task queue, indicating it can block. The task is started in
the same circumstances where we previously used to write (timer from the last
PutBuffer call, and shutdown if necessary).
To ensure it cannot be trying to use the data it writes out (mTable) from
the other thread while that data changes on the main thread, we use a mutex.
The task locks the mutex before starting, and unlocks when finished.
Enumerating the cases that we used to block on joining the thread:
In terms of application shutdown, we expect the background task queue to
either finish the write task, or fail to run it if it hasn't started it yet.
In the FastStartup case, we check if a write was necessary; if so, we
attempt to gain the lock without waiting. If we're successful, the write has
not yet started, and we instead run the write on the main thread. Otherwise,
we retry gaining the lock, blocking this time, thus guaranteeing the
off-the-main-thread write completes.
The task keeps a reference to the startupcache object, so it cannot be
destroyed while the task is pending.
Because the write does not modify `mTable`, and neither does `HasEntry`,
we do not need to do anything there.
In the `GetBuffer` case, we do not modify the table unless we have to read
the entry off disk (memmapped into `mCacheData`). This can only happen if
`mCacheData.initialized()` returns true, and we specifically call
`mCacheData.reset()` before firing off the write task to avoid this.
`mCacheData` is only re-initialized if someone calls `LoadArchive()`,
which can only happen from `Init()` (which is guaranteed not to run
again because this is a singleton), or `InvalidateCache()`, where we lock
the mutex (see below). So this is safe - but we assert on the lock to try
and avoid people breaking this chain of assumptions in the future.
When `PutBuffer` is called, we try to lock the mutex - but if locking fails
(ie the background thread is writing), we simply fail to store the entry
in the startupcache. In practice, this should be rare - it'd happen if
new calls to PutBuffer happen while writing during shutdown (when really,
we don't care) or when it's been 60 seconds since the last PutBuffer so
we started writing the startupcache.
When InvalidateCache is called, we lock the mutex - we shouldn't try to
write while invalidating, or invalidate while writing. This may be slow,
but in practice nothing should call `InvalidateCache` except developer
restarts or the `-purgecaches` commandline flag, so it shouldn't
matter a great deal.
Differential Revision: https://phabricator.services.mozilla.com/D70413
--HG--
extra : moz-landing-system : lando
2020-04-15 23:43:44 +03:00
|
|
|
// For use during shutdown - ensure we complete the shutdown write
|
|
|
|
// before shutdown, even in the FastShutdown case.
|
|
|
|
void EnsureShutdownWriteComplete();
|
|
|
|
|
2012-10-11 12:17:15 +04:00
|
|
|
// Signal that data should not be loaded from the cache file
|
|
|
|
static void IgnoreDiskCache();
|
|
|
|
|
2010-08-12 23:37:44 +04:00
|
|
|
// In DEBUG builds, returns a stream that will attempt to check for
|
|
|
|
// and disallow multiple writes of the same object.
|
|
|
|
nsresult GetDebugObjectOutputStream(nsIObjectOutputStream* aStream,
|
|
|
|
nsIObjectOutputStream** outStream);
|
|
|
|
|
2020-09-14 20:00:53 +03:00
|
|
|
static StartupCache* GetSingletonNoInit();
|
2010-08-12 23:37:44 +04:00
|
|
|
static StartupCache* GetSingleton();
|
|
|
|
static void DeleteSingleton();
|
|
|
|
|
2011-12-19 04:20:36 +04:00
|
|
|
// This measures all the heap memory used by the StartupCache, i.e. it
|
|
|
|
// excludes the mapping.
|
2015-07-31 07:19:57 +03:00
|
|
|
size_t HeapSizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
|
2011-12-19 04:20:36 +04:00
|
|
|
|
2019-10-04 23:45:18 +03:00
|
|
|
bool ShouldCompactCache();
|
2020-09-14 20:00:53 +03:00
|
|
|
nsresult ResetStartupWriteTimerCheckingReadCount();
|
2016-11-11 19:57:08 +03:00
|
|
|
nsresult ResetStartupWriteTimer();
|
|
|
|
bool StartupWriteComplete();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-08-12 23:37:44 +04:00
|
|
|
private:
|
|
|
|
StartupCache();
|
2013-11-26 03:57:40 +04:00
|
|
|
virtual ~StartupCache();
|
2010-08-12 23:37:44 +04:00
|
|
|
|
2020-04-02 22:40:39 +03:00
|
|
|
friend class StartupCacheInfo;
|
|
|
|
|
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<Ok, nsresult> LoadArchive();
|
2020-09-14 20:00:53 +03:00
|
|
|
nsresult Init();
|
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
|
|
|
|
|
|
|
// Returns a file pointer for the cache file with the given name in the
|
|
|
|
// current profile.
|
|
|
|
Result<nsCOMPtr<nsIFile>, nsresult> GetCacheFile(const nsAString& suffix);
|
|
|
|
|
|
|
|
// Opens the cache file for reading.
|
|
|
|
Result<Ok, nsresult> OpenCache();
|
|
|
|
|
|
|
|
// Writes the cache to disk
|
|
|
|
Result<Ok, nsresult> WriteToDisk();
|
|
|
|
|
2019-10-04 23:45:32 +03:00
|
|
|
void WaitOnPrefetchThread();
|
|
|
|
void StartPrefetchMemoryThread();
|
2010-08-12 23:37:44 +04:00
|
|
|
|
2020-09-14 20:00:53 +03:00
|
|
|
static nsresult InitSingleton();
|
2010-08-12 23:37:44 +04:00
|
|
|
static void WriteTimeout(nsITimer* aTimer, void* aClosure);
|
Bug 1614795 - use the background task queue for startupcache writes, r=dthayer,decoder
Prior to this patch, the startupcache created its own mWriteThread off which it
wrote to disk. It's initialized by MaybeSpawnWriteThread, which got called
at shutdown, to do the shutdown write if there was any reason to do so, and
from a timer that is re-initialized after every addition to the startup cache,
to run 60s after the last change to the cache.
It then joined that write thread on the main thread (in other words, blocks
on that off-main-thread write completing from the main thread) when:
- xpcom-shutdown fired
- the startupcache itself gets destroyed
- someone calls any of:
* HasEntry
* GetBuffer
* PutBuffer
* InvalidateCache
This patch removes the separate write thread, and instead dispatches a task to
the background task queue, indicating it can block. The task is started in
the same circumstances where we previously used to write (timer from the last
PutBuffer call, and shutdown if necessary).
To ensure it cannot be trying to use the data it writes out (mTable) from
the other thread while that data changes on the main thread, we use a mutex.
The task locks the mutex before starting, and unlocks when finished.
Enumerating the cases that we used to block on joining the thread:
In terms of application shutdown, we expect the background task queue to
either finish the write task, or fail to run it if it hasn't started it yet.
In the FastStartup case, we check if a write was necessary; if so, we
attempt to gain the lock without waiting. If we're successful, the write has
not yet started, and we instead run the write on the main thread. Otherwise,
we retry gaining the lock, blocking this time, thus guaranteeing the
off-the-main-thread write completes.
The task keeps a reference to the startupcache object, so it cannot be
destroyed while the task is pending.
Because the write does not modify `mTable`, and neither does `HasEntry`,
we do not need to do anything there.
In the `GetBuffer` case, we do not modify the table unless we have to read
the entry off disk (memmapped into `mCacheData`). This can only happen if
`mCacheData.initialized()` returns true, and we specifically call
`mCacheData.reset()` before firing off the write task to avoid this.
`mCacheData` is only re-initialized if someone calls `LoadArchive()`,
which can only happen from `Init()` (which is guaranteed not to run
again because this is a singleton), or `InvalidateCache()`, where we lock
the mutex (see below). So this is safe - but we assert on the lock to try
and avoid people breaking this chain of assumptions in the future.
When `PutBuffer` is called, we try to lock the mutex - but if locking fails
(ie the background thread is writing), we simply fail to store the entry
in the startupcache. In practice, this should be rare - it'd happen if
new calls to PutBuffer happen while writing during shutdown (when really,
we don't care) or when it's been 60 seconds since the last PutBuffer so
we started writing the startupcache.
When InvalidateCache is called, we lock the mutex - we shouldn't try to
write while invalidating, or invalidate while writing. This may be slow,
but in practice nothing should call `InvalidateCache` except developer
restarts or the `-purgecaches` commandline flag, so it shouldn't
matter a great deal.
Differential Revision: https://phabricator.services.mozilla.com/D70413
--HG--
extra : moz-landing-system : lando
2020-04-15 23:43:44 +03:00
|
|
|
void MaybeWriteOffMainThread();
|
2019-10-04 23:45:32 +03:00
|
|
|
static void ThreadedPrefetch(void* aClosure);
|
2010-08-12 23:37:44 +04:00
|
|
|
|
2020-09-14 20:00:53 +03:00
|
|
|
HashMap<nsCString, StartupCacheEntry> mTable;
|
2022-03-22 18:19:47 +03:00
|
|
|
// This owns references to the contents of tables which have been invalidated.
|
|
|
|
// In theory it grows forever if the cache is continually filled and then
|
|
|
|
// invalidated, but this should not happen in practice. Deleting old tables
|
|
|
|
// could create dangling pointers. RefPtrs could be introduced, but it would
|
|
|
|
// be a large amount of error-prone work to change.
|
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
|
|
|
nsTArray<decltype(mTable)> mOldTables;
|
2022-03-22 18:19:47 +03:00
|
|
|
size_t mAllowedInvalidationsCount;
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> mFile;
|
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
|
|
|
loader::AutoMemMap mCacheData;
|
2022-03-16 21:47:08 +03:00
|
|
|
Mutex mTableLock MOZ_UNANNOTATED;
|
2013-11-26 03:57:40 +04:00
|
|
|
|
2010-08-12 23:37:44 +04:00
|
|
|
nsCOMPtr<nsIObserverService> mObserverService;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<StartupCacheListener> mListener;
|
2020-09-14 20:00:53 +03:00
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
2010-08-12 23:37:44 +04: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
|
|
|
Atomic<bool> mDirty;
|
2020-02-14 23:29:48 +03:00
|
|
|
Atomic<bool> mWrittenOnce;
|
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
|
|
|
bool mCurTableReferenced;
|
2019-10-04 23:45:18 +03:00
|
|
|
uint32_t mRequestedCount;
|
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
|
|
|
size_t mCacheEntriesBaseOffset;
|
2010-08-12 23:37:44 +04:00
|
|
|
|
2013-11-26 03:57:40 +04:00
|
|
|
static StaticRefPtr<StartupCache> gStartupCache;
|
2020-09-14 20:00:53 +03:00
|
|
|
static bool gShutdownInitiated;
|
2012-10-11 12:17:15 +04:00
|
|
|
static bool gIgnoreDiskCache;
|
2020-04-02 22:40:39 +03:00
|
|
|
static bool gFoundDiskCacheOnInit;
|
2019-10-04 23:45:32 +03:00
|
|
|
PRThread* mPrefetchThread;
|
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
|
|
|
UniquePtr<Compression::LZ4FrameDecompressionContext> mDecompressionContext;
|
2010-08-12 23:37:44 +04:00
|
|
|
#ifdef DEBUG
|
2021-03-23 13:36:39 +03:00
|
|
|
nsTHashSet<nsCOMPtr<nsISupports>> mWriteObjectMap;
|
2010-08-12 23:37:44 +04:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
// This debug outputstream attempts to detect if clients are writing multiple
|
|
|
|
// references to the same object. We only support that if that object
|
|
|
|
// is a singleton.
|
|
|
|
#ifdef DEBUG
|
2015-03-21 19:28:04 +03:00
|
|
|
class StartupCacheDebugOutputStream final : public nsIObjectOutputStream {
|
2020-03-10 11:48:37 +03:00
|
|
|
~StartupCacheDebugOutputStream() = default;
|
2014-06-24 02:40:03 +04:00
|
|
|
|
2010-08-12 23:37:44 +04:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBJECTOUTPUTSTREAM
|
|
|
|
|
|
|
|
StartupCacheDebugOutputStream(nsIObjectOutputStream* binaryStream,
|
2021-03-23 13:36:39 +03:00
|
|
|
nsTHashSet<nsCOMPtr<nsISupports>>* objectMap)
|
2010-08-12 23:37:44 +04:00
|
|
|
: mBinaryStream(binaryStream), mObjectMap(objectMap) {}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2010-08-12 23:37:44 +04:00
|
|
|
NS_FORWARD_SAFE_NSIBINARYOUTPUTSTREAM(mBinaryStream)
|
|
|
|
NS_FORWARD_SAFE_NSIOUTPUTSTREAM(mBinaryStream)
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool CheckReferences(nsISupports* aObject);
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2010-08-12 23:37:44 +04:00
|
|
|
nsCOMPtr<nsIObjectOutputStream> mBinaryStream;
|
2021-03-23 13:36:39 +03:00
|
|
|
nsTHashSet<nsCOMPtr<nsISupports>>* mObjectMap;
|
2010-08-12 23:37:44 +04:00
|
|
|
};
|
|
|
|
#endif // DEBUG
|
|
|
|
|
|
|
|
} // namespace scache
|
|
|
|
} // namespace mozilla
|
2015-07-13 18:25:42 +03:00
|
|
|
|
2010-08-12 23:37:44 +04:00
|
|
|
#endif // StartupCache_h_
|