Bug 1154974 (Part 2) - Merge image cache entries for blobs URIs with the same underlying blob. r=baku

This commit is contained in:
Seth Fowler 2015-05-20 18:49:53 -07:00
parent 72737bf49c
commit ef213163b6
2 changed files with 59 additions and 10 deletions

View File

@ -17,37 +17,67 @@ using namespace dom;
namespace image { namespace image {
bool
URISchemeIs(ImageURL* aURI, const char* aScheme)
{
bool schemeMatches = false;
if (NS_WARN_IF(NS_FAILED(aURI->SchemeIs(aScheme, &schemeMatches)))) {
return false;
}
return schemeMatches;
}
static Maybe<uint64_t>
BlobSerial(ImageURL* aURI)
{
nsAutoCString spec;
aURI->GetSpec(spec);
nsRefPtr<BlobImpl> blob;
if (NS_SUCCEEDED(NS_GetBlobForBlobURISpec(spec, getter_AddRefs(blob))) &&
blob) {
return Some(blob->GetSerialNumber());
}
return Nothing();
}
ImageCacheKey::ImageCacheKey(nsIURI* aURI) ImageCacheKey::ImageCacheKey(nsIURI* aURI)
: mURI(new ImageURL(aURI)) : mURI(new ImageURL(aURI))
, mIsChrome(URISchemeIs(mURI, "chrome"))
{ {
MOZ_ASSERT(NS_IsMainThread()); MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mURI);
bool isChrome; if (URISchemeIs(mURI, "blob")) {
mIsChrome = NS_SUCCEEDED(aURI->SchemeIs("chrome", &isChrome)) && isChrome; mBlobSerial = BlobSerial(mURI);
}
mHash = ComputeHash(mURI); mHash = ComputeHash(mURI, mBlobSerial);
} }
ImageCacheKey::ImageCacheKey(ImageURL* aURI) ImageCacheKey::ImageCacheKey(ImageURL* aURI)
: mURI(aURI) : mURI(aURI)
, mIsChrome(URISchemeIs(mURI, "chrome"))
{ {
MOZ_ASSERT(mURI); MOZ_ASSERT(aURI);
bool isChrome; if (URISchemeIs(mURI, "blob")) {
mIsChrome = NS_SUCCEEDED(aURI->SchemeIs("chrome", &isChrome)) && isChrome; mBlobSerial = BlobSerial(mURI);
}
mHash = ComputeHash(mURI); mHash = ComputeHash(mURI, mBlobSerial);
} }
ImageCacheKey::ImageCacheKey(const ImageCacheKey& aOther) ImageCacheKey::ImageCacheKey(const ImageCacheKey& aOther)
: mURI(aOther.mURI) : mURI(aOther.mURI)
, mBlobSerial(aOther.mBlobSerial)
, mHash(aOther.mHash) , mHash(aOther.mHash)
, mIsChrome(aOther.mIsChrome) , mIsChrome(aOther.mIsChrome)
{ } { }
ImageCacheKey::ImageCacheKey(ImageCacheKey&& aOther) ImageCacheKey::ImageCacheKey(ImageCacheKey&& aOther)
: mURI(Move(aOther.mURI)) : mURI(Move(aOther.mURI))
, mBlobSerial(Move(aOther.mBlobSerial))
, mHash(aOther.mHash) , mHash(aOther.mHash)
, mIsChrome(aOther.mIsChrome) , mIsChrome(aOther.mIsChrome)
{ } { }
@ -55,6 +85,12 @@ ImageCacheKey::ImageCacheKey(ImageCacheKey&& aOther)
bool bool
ImageCacheKey::operator==(const ImageCacheKey& aOther) const ImageCacheKey::operator==(const ImageCacheKey& aOther) const
{ {
if (mBlobSerial || aOther.mBlobSerial) {
// If at least one of us has a blob serial, just compare those.
return mBlobSerial == aOther.mBlobSerial;
}
// For non-blob URIs, compare the URIs.
return *mURI == *aOther.mURI; return *mURI == *aOther.mURI;
} }
@ -65,10 +101,19 @@ ImageCacheKey::Spec() const
} }
/* static */ uint32_t /* static */ uint32_t
ImageCacheKey::ComputeHash(ImageURL* aURI) ImageCacheKey::ComputeHash(ImageURL* aURI,
const Maybe<uint64_t>& aBlobSerial)
{ {
// Since we frequently call Hash() several times in a row on the same // Since we frequently call Hash() several times in a row on the same
// ImageCacheKey, as an optimization we compute our hash once and store it. // ImageCacheKey, as an optimization we compute our hash once and store it.
if (aBlobSerial) {
// For blob URIs, we hash the serial number of the underlying blob, so that
// different blob URIs which point to the same blob share a cache entry.
return HashGeneric(*aBlobSerial);
}
// For non-blob URIs, we hash the URI spec.
nsAutoCString spec; nsAutoCString spec;
aURI->GetSpec(spec); aURI->GetSpec(spec);
return HashString(spec); return HashString(spec);

View File

@ -10,6 +10,8 @@
#ifndef mozilla_image_src_ImageCacheKey_h #ifndef mozilla_image_src_ImageCacheKey_h
#define mozilla_image_src_ImageCacheKey_h #define mozilla_image_src_ImageCacheKey_h
#include "mozilla/Maybe.h"
class nsIURI; class nsIURI;
namespace mozilla { namespace mozilla {
@ -42,9 +44,11 @@ public:
bool IsChrome() const { return mIsChrome; } bool IsChrome() const { return mIsChrome; }
private: private:
static uint32_t ComputeHash(ImageURL* aURI); static uint32_t ComputeHash(ImageURL* aURI,
const Maybe<uint64_t>& aBlobSerial);
nsRefPtr<ImageURL> mURI; nsRefPtr<ImageURL> mURI;
Maybe<uint64_t> mBlobSerial;
uint32_t mHash; uint32_t mHash;
bool mIsChrome; bool mIsChrome;
}; };