Bug 1550108 - Eliminate large buffer copies from StartupCache r=froydnj

The signatures were updated in the previous patch to hand us the raw,
uncopied buffers. This just adjusts the callsites to match.

Differential Revision: https://phabricator.services.mozilla.com/D34653

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Doug Thayer 2019-10-04 20:45:09 +00:00
Родитель a74957c6e6
Коммит 5a64723655
7 изменённых файлов: 30 добавлений и 33 удалений

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

@ -186,15 +186,14 @@ nsresult nsXBLDocumentInfo::ReadPrototypeBindings(nsIURI* aURI,
return NS_ERROR_FAILURE;
}
UniquePtr<char[]> buf;
const char* buf;
uint32_t len;
rv = startupCache->GetBuffer(spec.get(), &buf, &len);
// GetBuffer will fail if the binding is not in the cache.
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIObjectInputStream> stream;
rv = NewObjectInputStreamFromBuffer(std::move(buf), len,
getter_AddRefs(stream));
rv = NewObjectInputStreamFromBuffer(buf, len, getter_AddRefs(stream));
NS_ENSURE_SUCCESS(rv, rv);
// The file compatibility.ini stores the build id. This is checked in

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

@ -260,7 +260,7 @@ nsresult nsXULPrototypeCache::GetInputStream(nsIURI* uri,
nsresult rv = PathifyURI(uri, spec);
if (NS_FAILED(rv)) return NS_ERROR_NOT_AVAILABLE;
UniquePtr<char[]> buf;
const char* buf;
uint32_t len;
nsCOMPtr<nsIObjectInputStream> ois;
StartupCache* sc = StartupCache::GetSingleton();
@ -269,7 +269,7 @@ nsresult nsXULPrototypeCache::GetInputStream(nsIURI* uri,
rv = sc->GetBuffer(spec.get(), &buf, &len);
if (NS_FAILED(rv)) return NS_ERROR_NOT_AVAILABLE;
rv = NewObjectInputStreamFromBuffer(std::move(buf), len, getter_AddRefs(ois));
rv = NewObjectInputStreamFromBuffer(buf, len, getter_AddRefs(ois));
NS_ENSURE_SUCCESS(rv, rv);
mInputStreamTable.Put(uri, ois);
@ -398,14 +398,13 @@ nsresult nsXULPrototypeCache::BeginCaching(nsIURI* aURI) {
nsAutoCString fileChromePath, fileLocale;
UniquePtr<char[]> buf;
const char* buf = nullptr;
uint32_t len, amtRead;
nsCOMPtr<nsIObjectInputStream> objectInput;
rv = startupCache->GetBuffer(kXULCacheInfoKey, &buf, &len);
if (NS_SUCCEEDED(rv))
rv = NewObjectInputStreamFromBuffer(std::move(buf), len,
getter_AddRefs(objectInput));
rv = NewObjectInputStreamFromBuffer(buf, len, getter_AddRefs(objectInput));
if (NS_SUCCEEDED(rv)) {
rv = objectInput->ReadCString(fileLocale);
@ -461,10 +460,10 @@ nsresult nsXULPrototypeCache::BeginCaching(nsIURI* aURI) {
}
if (NS_SUCCEEDED(rv)) {
buf = MakeUnique<char[]>(len);
rv = inputStream->Read(buf.get(), len, &amtRead);
auto putBuf = MakeUnique<char[]>(len);
rv = inputStream->Read(putBuf.get(), len, &amtRead);
if (NS_SUCCEEDED(rv) && len == amtRead)
rv = startupCache->PutBuffer(kXULCacheInfoKey, std::move(buf), len);
rv = startupCache->PutBuffer(kXULCacheInfoKey, std::move(putBuf), len);
else {
rv = NS_ERROR_UNEXPECTED;
}

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

@ -48,6 +48,7 @@
#include "nsIMemory.h"
#include "gfxFontConstants.h"
#include "mozilla/EndianUtils.h"
#include "mozilla/Preferences.h"
#include "mozilla/scache/StartupCache.h"
#include <fcntl.h>
@ -647,8 +648,8 @@ class FontNameCache {
}
uint32_t size;
UniquePtr<char[]> buf;
if (NS_FAILED(mCache->GetBuffer(CACHE_KEY, &buf, &size))) {
const char* cur;
if (NS_FAILED(mCache->GetBuffer(CACHE_KEY, &cur, &size))) {
LOG(("no cache of " CACHE_KEY));
return;
}
@ -658,8 +659,6 @@ class FontNameCache {
mMap.Clear();
mWriteNeeded = false;
const char* cur = buf.get();
while (const char* fileEnd = strchr(cur, kFileSep)) {
// The cached record for one file is at [cur, fileEnd].
@ -1065,7 +1064,7 @@ void gfxFT2FontList::FindFontsInOmnijar(FontNameCache* aCache) {
mozilla::scache::StartupCache* cache =
mozilla::scache::StartupCache::GetSingleton();
UniquePtr<char[]> cachedModifiedTimeBuf;
const char* cachedModifiedTimeBuf;
uint32_t longSize;
if (cache &&
NS_SUCCEEDED(cache->GetBuffer(JAR_LAST_MODIFED_TIME,
@ -1073,7 +1072,7 @@ void gfxFT2FontList::FindFontsInOmnijar(FontNameCache* aCache) {
longSize == sizeof(int64_t)) {
nsCOMPtr<nsIFile> jarFile = Omnijar::GetPath(Omnijar::Type::GRE);
jarFile->GetLastModifiedTime(&mJarModifiedTime);
if (mJarModifiedTime > *(int64_t*)cachedModifiedTimeBuf.get()) {
if (mJarModifiedTime > LittleEndian::readInt64(cachedModifiedTimeBuf)) {
jarChanged = true;
}
}
@ -1332,7 +1331,7 @@ void gfxFT2FontList::WriteCache() {
if (cache && mJarModifiedTime > 0) {
const size_t bufSize = sizeof(mJarModifiedTime);
auto buf = MakeUnique<char[]>(bufSize);
memcpy(buf.get(), &mJarModifiedTime, bufSize);
LittleEndian::writeInt64(buf.get(), mJarModifiedTime);
LOG(("WriteCache: putting Jar, length %zu", bufSize));
cache->PutBuffer(JAR_LAST_MODIFED_TIME, std::move(buf), bufSize);

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

@ -20,15 +20,16 @@ using mozilla::UniquePtr;
// principals to the system principals.
nsresult ReadCachedScript(StartupCache* cache, nsACString& uri, JSContext* cx,
MutableHandleScript scriptp) {
UniquePtr<char[]> buf;
const char* buf;
uint32_t len;
nsresult rv = cache->GetBuffer(PromiseFlatCString(uri).get(), &buf, &len);
if (NS_FAILED(rv)) {
return rv; // don't warn since NOT_AVAILABLE is an ok error
}
void* copy = malloc(len);
memcpy(copy, buf, len);
JS::TranscodeBuffer buffer;
buffer.replaceRawBuffer(reinterpret_cast<uint8_t*>(buf.release()), len);
buffer.replaceRawBuffer(reinterpret_cast<uint8_t*>(copy), len);
JS::TranscodeResult code = JS::DecodeScript(cx, buffer, scriptp);
if (code == JS::TranscodeResult_Ok) {
return NS_OK;

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

@ -19,12 +19,12 @@
namespace mozilla {
namespace scache {
nsresult NewObjectInputStreamFromBuffer(UniquePtr<char[]> buffer, uint32_t len,
nsresult NewObjectInputStreamFromBuffer(const char* buffer, uint32_t len,
nsIObjectInputStream** stream) {
nsCOMPtr<nsIInputStream> stringStream;
nsresult rv = NS_NewByteInputStream(getter_AddRefs(stringStream),
MakeSpan(buffer.release(), len),
NS_ASSIGNMENT_ADOPT);
nsresult rv =
NS_NewByteInputStream(getter_AddRefs(stringStream), MakeSpan(buffer, len),
NS_ASSIGNMENT_DEPEND);
MOZ_ALWAYS_SUCCEEDS(rv);
nsCOMPtr<nsIObjectInputStream> objectInput =

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

@ -16,7 +16,7 @@ class nsIURI;
namespace mozilla {
namespace scache {
nsresult NewObjectInputStreamFromBuffer(UniquePtr<char[]> buffer, uint32_t len,
nsresult NewObjectInputStreamFromBuffer(const char* buffer, uint32_t len,
nsIObjectInputStream** stream);
// We can't retrieve the wrapped stream from the objectOutputStream later,

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

@ -86,7 +86,7 @@ TEST_F(TestStartupCache, StartupWriteRead) {
const char* buf = "Market opportunities for BeardBook";
const char* id = "id";
UniquePtr<char[]> outbuf;
const char* outbuf;
uint32_t len;
rv = sc->PutBuffer(id, UniquePtr<char[]>(strdup(buf)), strlen(buf) + 1);
@ -94,7 +94,7 @@ TEST_F(TestStartupCache, StartupWriteRead) {
rv = sc->GetBuffer(id, &outbuf, &len);
EXPECT_TRUE(NS_SUCCEEDED(rv));
EXPECT_STREQ(buf, outbuf.get());
EXPECT_STREQ(buf, outbuf);
rv = sc->ResetStartupWriteTimer();
EXPECT_TRUE(NS_SUCCEEDED(rv));
@ -102,14 +102,14 @@ TEST_F(TestStartupCache, StartupWriteRead) {
rv = sc->GetBuffer(id, &outbuf, &len);
EXPECT_TRUE(NS_SUCCEEDED(rv));
EXPECT_STREQ(buf, outbuf.get());
EXPECT_STREQ(buf, outbuf);
}
TEST_F(TestStartupCache, WriteInvalidateRead) {
nsresult rv;
const char* buf = "BeardBook competitive analysis";
const char* id = "id";
UniquePtr<char[]> outbuf;
const char* outbuf;
uint32_t len;
StartupCache* sc = StartupCache::GetSingleton();
ASSERT_TRUE(sc);
@ -168,14 +168,13 @@ TEST_F(TestStartupCache, WriteObject) {
rv = sc->PutBuffer(id, std::move(buf), len);
EXPECT_TRUE(NS_SUCCEEDED(rv));
UniquePtr<char[]> buf2;
const char* buf2;
uint32_t len2;
nsCOMPtr<nsIObjectInputStream> objectInput;
rv = sc->GetBuffer(id, &buf2, &len2);
EXPECT_TRUE(NS_SUCCEEDED(rv));
rv = NewObjectInputStreamFromBuffer(std::move(buf2), len2,
getter_AddRefs(objectInput));
rv = NewObjectInputStreamFromBuffer(buf2, len2, getter_AddRefs(objectInput));
EXPECT_TRUE(NS_SUCCEEDED(rv));
nsCOMPtr<nsISupports> deserialized;