Bug 1362761 - Make prefix and chunk sets as const as possible. r=dimi

MozReview-Commit-ID: JdnNOxnBAgC

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

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Francois Marier 2018-07-11 12:52:37 +00:00
Родитель 81219a04ca
Коммит 53ee4cb546
6 изменённых файлов: 53 добавлений и 58 удалений

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

@ -152,7 +152,7 @@ ChunkSet::Clear()
}
nsresult
ChunkSet::Write(nsIOutputStream* aOut)
ChunkSet::Write(nsIOutputStream* aOut) const
{
nsTArray<uint32_t> chunks(IO_BUFFER_SIZE);

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

@ -28,7 +28,7 @@ public:
nsresult Remove(const ChunkSet& aOther);
void Clear();
nsresult Write(nsIOutputStream* aOut);
nsresult Write(nsIOutputStream* aOut) const;
nsresult Read(nsIInputStream* aIn, uint32_t aNumElements);
private:

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

@ -32,9 +32,8 @@ const uint32_t VariableLengthPrefixSet::MAX_BUFFER_SIZE;
// they will be passed to nsUrlClassifierPrefixSet because of better optimization.
VariableLengthPrefixSet::VariableLengthPrefixSet()
: mLock("VariableLengthPrefixSet.mLock")
, mMemoryReportPath()
, mFixedPrefixSet(new nsUrlClassifierPrefixSet)
{
mFixedPrefixSet = new nsUrlClassifierPrefixSet();
}
nsresult
@ -164,7 +163,8 @@ VariableLengthPrefixSet::GetFixedLengthPrefixes(FallibleTArray<uint32_t>& aPrefi
// full hash. However, if that happens, this method returns any one of them.
// It does not guarantee which one of those will be returned.
nsresult
VariableLengthPrefixSet::Matches(const nsACString& aFullHash, uint32_t* aLength)
VariableLengthPrefixSet::Matches(const nsACString& aFullHash,
uint32_t* aLength) const
{
MutexAutoLock lock(mLock);
@ -199,7 +199,7 @@ VariableLengthPrefixSet::Matches(const nsACString& aFullHash, uint32_t* aLength)
}
nsresult
VariableLengthPrefixSet::IsEmpty(bool* aEmpty)
VariableLengthPrefixSet::IsEmpty(bool* aEmpty) const
{
MutexAutoLock lock(mLock);
@ -254,7 +254,7 @@ VariableLengthPrefixSet::LoadFromFile(nsIFile* aFile)
}
nsresult
VariableLengthPrefixSet::StoreToFile(nsIFile* aFile)
VariableLengthPrefixSet::StoreToFile(nsIFile* aFile) const
{
NS_ENSURE_ARG_POINTER(aFile);
@ -345,7 +345,7 @@ VariableLengthPrefixSet::LoadPrefixes(nsIInputStream* in)
}
uint32_t
VariableLengthPrefixSet::CalculatePreallocateSize()
VariableLengthPrefixSet::CalculatePreallocateSize() const
{
uint32_t fileSize = 0;
@ -362,7 +362,7 @@ VariableLengthPrefixSet::CalculatePreallocateSize()
}
nsresult
VariableLengthPrefixSet::WritePrefixes(nsIOutputStream* out)
VariableLengthPrefixSet::WritePrefixes(nsIOutputStream* out) const
{
uint32_t written;
uint32_t writelen = sizeof(uint32_t);
@ -403,8 +403,8 @@ VariableLengthPrefixSet::WritePrefixes(nsIOutputStream* out)
bool
VariableLengthPrefixSet::BinarySearch(const nsACString& aFullHash,
const nsACString& aPrefixes,
uint32_t aPrefixSize)
const nsACString& aPrefixes,
uint32_t aPrefixSize) const
{
const char* fullhash = aFullHash.BeginReading();
const char* prefixes = aPrefixes.BeginReading();
@ -428,7 +428,7 @@ MOZ_DEFINE_MALLOC_SIZE_OF(UrlClassifierMallocSizeOf)
NS_IMETHODIMP
VariableLengthPrefixSet::CollectReports(nsIHandleReportCallback* aHandleReport,
nsISupports* aData, bool aAnonymize)
nsISupports* aData, bool aAnonymize)
{
MOZ_ASSERT(NS_IsMainThread());
@ -441,7 +441,7 @@ VariableLengthPrefixSet::CollectReports(nsIHandleReportCallback* aHandleReport,
}
size_t
VariableLengthPrefixSet::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf)
VariableLengthPrefixSet::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
{
MutexAutoLock lock(mLock);

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

@ -29,12 +29,12 @@ public:
nsresult SetPrefixes(const mozilla::safebrowsing::PrefixStringMap& aPrefixMap);
nsresult GetPrefixes(mozilla::safebrowsing::PrefixStringMap& aPrefixMap);
nsresult GetFixedLengthPrefixes(FallibleTArray<uint32_t>& aPrefixes);
nsresult Matches(const nsACString& aFullHash, uint32_t* aLength);
nsresult IsEmpty(bool* aEmpty);
nsresult Matches(const nsACString& aFullHash, uint32_t* aLength) const;
nsresult IsEmpty(bool* aEmpty) const;
nsresult LoadFromFile(nsIFile* aFile);
nsresult StoreToFile(nsIFile* aFile);
nsresult StoreToFile(nsIFile* aFile) const;
size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf);
size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIMEMORYREPORTER
@ -47,19 +47,19 @@ private:
bool BinarySearch(const nsACString& aFullHash,
const nsACString& aPrefixes,
uint32_t aPrefixSize);
uint32_t aPrefixSize) const;
uint32_t CalculatePreallocateSize();
nsresult WritePrefixes(nsIOutputStream* out);
uint32_t CalculatePreallocateSize() const;
nsresult WritePrefixes(nsIOutputStream* out) const;
nsresult LoadPrefixes(nsIInputStream* in);
// Lock to prevent races between the url-classifier thread (which does most
// of the operations) and the main thread (which does memory reporting).
// It should be held for all operations between Init() and destruction that
// touch this class's data members.
mozilla::Mutex mLock;
mutable mozilla::Mutex mLock;
RefPtr<nsUrlClassifierPrefixSet> mFixedPrefixSet;
const RefPtr<nsUrlClassifierPrefixSet> mFixedPrefixSet;
mozilla::safebrowsing::PrefixStringMap mVLPrefixSet;
nsCString mMemoryReportPath;

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

@ -40,11 +40,25 @@ NS_IMPL_ISUPPORTS(
// Definition required due to std::max<>()
const uint32_t nsUrlClassifierPrefixSet::MAX_BUFFER_SIZE;
template<typename T>
static void
CalculateTArrayChecksum(const nsTArray<T>& aArray, uint32_t* outChecksum)
{
*outChecksum = ~0;
for (size_t i = 0; i < aArray.Length(); i++) {
const T& element = aArray[i];
const void* pointer = &element;
*outChecksum = ComputeCrc32c(*outChecksum,
reinterpret_cast<const uint8_t*>(pointer),
sizeof(void*));
}
}
nsUrlClassifierPrefixSet::nsUrlClassifierPrefixSet()
: mLock("nsUrlClassifierPrefixSet.mLock")
, mIndexDeltasChecksum(~0)
, mTotalPrefixes(0)
, mMemoryReportPath()
{
}
@ -226,9 +240,9 @@ nsUrlClassifierPrefixSet::GetPrefixes(uint32_t* aCount,
return NS_OK;
}
uint32_t nsUrlClassifierPrefixSet::BinSearch(uint32_t start,
uint32_t end,
uint32_t target)
uint32_t
nsUrlClassifierPrefixSet::BinSearch(uint32_t start, uint32_t end,
uint32_t target) const
{
mLock.AssertCurrentThreadOwns();
@ -316,7 +330,7 @@ nsUrlClassifierPrefixSet::CollectReports(nsIHandleReportCallback* aHandleReport,
}
size_t
nsUrlClassifierPrefixSet::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf)
nsUrlClassifierPrefixSet::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
{
MutexAutoLock lock(mLock);
@ -497,7 +511,7 @@ nsUrlClassifierPrefixSet::LoadPrefixes(nsIInputStream* in)
}
uint32_t
nsUrlClassifierPrefixSet::CalculatePreallocateSize()
nsUrlClassifierPrefixSet::CalculatePreallocateSize() const
{
uint32_t fileSize = 4 * sizeof(uint32_t);
uint32_t deltas = mTotalPrefixes - mIndexPrefixes.Length();
@ -507,7 +521,7 @@ nsUrlClassifierPrefixSet::CalculatePreallocateSize()
}
nsresult
nsUrlClassifierPrefixSet::WritePrefixes(nsIOutputStream* out)
nsUrlClassifierPrefixSet::WritePrefixes(nsIOutputStream* out) const
{
mCanary.Check();
@ -550,27 +564,27 @@ nsUrlClassifierPrefixSet::WritePrefixes(nsIOutputStream* out)
}
}
rv = out->Write(reinterpret_cast<char*>(&indexSize), writelen, &written);
rv = out->Write(reinterpret_cast<const char*>(&indexSize), writelen, &written);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(written == writelen, NS_ERROR_FAILURE);
rv = out->Write(reinterpret_cast<char*>(&totalDeltas), writelen, &written);
rv = out->Write(reinterpret_cast<const char*>(&totalDeltas), writelen, &written);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(written == writelen, NS_ERROR_FAILURE);
writelen = indexSize * sizeof(uint32_t);
rv = out->Write(reinterpret_cast<char*>(mIndexPrefixes.Elements()), writelen, &written);
rv = out->Write(reinterpret_cast<const char*>(mIndexPrefixes.Elements()), writelen, &written);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(written == writelen, NS_ERROR_FAILURE);
rv = out->Write(reinterpret_cast<char*>(indexStarts.Elements()), writelen, &written);
rv = out->Write(reinterpret_cast<const char*>(indexStarts.Elements()), writelen, &written);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(written == writelen, NS_ERROR_FAILURE);
if (totalDeltas > 0) {
for (uint32_t i = 0; i < indexDeltaSize; i++) {
writelen = mIndexDeltas[i].Length() * sizeof(uint16_t);
rv = out->Write(reinterpret_cast<char*>(mIndexDeltas[i].Elements()), writelen, &written);
rv = out->Write(reinterpret_cast<const char*>(mIndexDeltas[i].Elements()), writelen, &written);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(written == writelen, NS_ERROR_FAILURE);
}
@ -580,19 +594,3 @@ nsUrlClassifierPrefixSet::WritePrefixes(nsIOutputStream* out)
return NS_OK;
}
template<typename T>
void
nsUrlClassifierPrefixSet::CalculateTArrayChecksum(nsTArray<T>& aArray,
uint32_t* outChecksum)
{
*outChecksum = ~0;
for (size_t i = 0; i < aArray.Length(); i++) {
const T& element = aArray[i];
const void* pointer = &element;
*outChecksum = ComputeCrc32c(*outChecksum,
reinterpret_cast<const uint8_t*>(pointer),
sizeof(void*));
}
}

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

@ -46,7 +46,7 @@ public:
nsresult GetPrefixesNative(FallibleTArray<uint32_t>& outArray);
size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf);
size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIMEMORYREPORTER
@ -63,20 +63,17 @@ private:
void Clear();
nsresult MakePrefixSet(const uint32_t* aArray, uint32_t aLength);
uint32_t BinSearch(uint32_t start, uint32_t end, uint32_t target);
uint32_t BinSearch(uint32_t start, uint32_t end, uint32_t target) const;
uint32_t CalculatePreallocateSize();
nsresult WritePrefixes(nsIOutputStream* out);
uint32_t CalculatePreallocateSize() const;
nsresult WritePrefixes(nsIOutputStream* out) const;
nsresult LoadPrefixes(nsIInputStream* in);
template<typename T>
void CalculateTArrayChecksum(nsTArray<T>& aArray, uint32_t* outChecksum);
// Lock to prevent races between the url-classifier thread (which does most
// of the operations) and the main thread (which does memory reporting).
// It should be held for all operations between Init() and destruction that
// touch this class's data members.
mozilla::Mutex mLock;
mutable mozilla::Mutex mLock;
// list of fully stored prefixes, that also form the
// start of a run of deltas in mIndexDeltas.
nsTArray<uint32_t> mIndexPrefixes;