Bug 1102687 - Avoid copying and allocating 3 times in GetPrefixes. r=dmajor

This commit is contained in:
Gian-Carlo Pascutto 2014-11-24 08:38:29 +01:00
parent 7c3b67bf71
commit f08ba43b40
5 changed files with 40 additions and 32 deletions

View File

@ -611,8 +611,8 @@ Classifier::ApplyTableUpdates(nsTArray<TableUpdate*>* aUpdates,
if (!prefixSet) {
return NS_ERROR_FAILURE;
}
nsTArray<uint32_t> AddPrefixHashes;
rv = prefixSet->GetPrefixes(&AddPrefixHashes);
FallibleTArray<uint32_t> AddPrefixHashes;
rv = prefixSet->GetPrefixes(AddPrefixHashes);
NS_ENSURE_SUCCESS(rv, rv);
rv = store->AugmentAdds(AddPrefixHashes);
NS_ENSURE_SUCCESS(rv, rv);
@ -734,8 +734,8 @@ Classifier::ReadNoiseEntries(const Prefix& aPrefix,
return NS_ERROR_FAILURE;
}
nsTArray<uint32_t> prefixes;
nsresult rv = cache->GetPrefixes(&prefixes);
FallibleTArray<uint32_t> prefixes;
nsresult rv = cache->GetPrefixes(prefixes);
NS_ENSURE_SUCCESS(rv, rv);
size_t idx = prefixes.BinaryIndexOf(aPrefix.ToUint32());

View File

@ -684,24 +684,14 @@ LookupCache::LoadPrefixSet()
}
nsresult
LookupCache::GetPrefixes(nsTArray<uint32_t>* aAddPrefixes)
LookupCache::GetPrefixes(FallibleTArray<uint32_t>& aAddPrefixes)
{
if (!mPrimed) {
// This can happen if its a new table, so no error.
LOG(("GetPrefixes from empty LookupCache"));
return NS_OK;
}
uint32_t cnt;
uint32_t *arr;
nsresult rv = mPrefixSet->GetPrefixes(&cnt, &arr);
NS_ENSURE_SUCCESS(rv, rv);
bool appendOk = aAddPrefixes->AppendElements(arr, cnt);
nsMemory::Free(arr);
if (appendOk) {
return NS_OK;
} else {
return NS_ERROR_FAILURE;
}
return mPrefixSet->GetPrefixesNative(aAddPrefixes);
}

View File

@ -99,7 +99,7 @@ public:
// This will Clear() the passed arrays when done.
nsresult Build(AddPrefixArray& aAddPrefixes,
AddCompleteArray& aAddCompletes);
nsresult GetPrefixes(nsTArray<uint32_t>* aAddPrefixes);
nsresult GetPrefixes(FallibleTArray<uint32_t>& aAddPrefixes);
void ClearCompleteCache();
#if DEBUG && defined(PR_LOGGING)

View File

@ -140,6 +140,30 @@ nsUrlClassifierPrefixSet::MakePrefixSet(const uint32_t* aPrefixes, uint32_t aLen
return NS_OK;
}
nsresult
nsUrlClassifierPrefixSet::GetPrefixesNative(FallibleTArray<uint32_t>& outArray)
{
if (!outArray.SetLength(mTotalPrefixes)) {
return NS_ERROR_OUT_OF_MEMORY;
}
uint32_t prefixIdxLength = mIndexPrefixes.Length();
uint32_t prefixCnt = 0;
for (uint32_t i = 0; i < prefixIdxLength; i++) {
uint32_t prefix = mIndexPrefixes[i];
outArray[prefixCnt++] = prefix;
for (uint32_t j = 0; j < mIndexDeltas[i].Length(); j++) {
prefix += mIndexDeltas[i][j];
outArray[prefixCnt++] = prefix;
}
}
NS_ASSERTION(mTotalPrefixes == prefixCnt, "Lengths are inconsistent");
return NS_OK;
}
NS_IMETHODIMP
nsUrlClassifierPrefixSet::GetPrefixes(uint32_t* aCount,
uint32_t** aPrefixes)
@ -149,24 +173,17 @@ nsUrlClassifierPrefixSet::GetPrefixes(uint32_t* aCount,
NS_ENSURE_ARG_POINTER(aPrefixes);
*aPrefixes = nullptr;
uint64_t itemCount = mTotalPrefixes;
FallibleTArray<uint32_t> prefixes;
nsresult rv = GetPrefixesNative(prefixes);
if (NS_FAILED(rv)) {
return rv;
}
uint64_t itemCount = prefixes.Length();
uint32_t* prefixArray = static_cast<uint32_t*>(nsMemory::Alloc(itemCount * sizeof(uint32_t)));
NS_ENSURE_TRUE(prefixArray, NS_ERROR_OUT_OF_MEMORY);
uint32_t prefixIdxLength = mIndexPrefixes.Length();
uint32_t prefixCnt = 0;
for (uint32_t i = 0; i < prefixIdxLength; i++) {
uint32_t prefix = mIndexPrefixes[i];
prefixArray[prefixCnt++] = prefix;
for (uint32_t j = 0; j < mIndexDeltas[i].Length(); j++) {
prefix += mIndexDeltas[i][j];
prefixArray[prefixCnt++] = prefix;
}
}
NS_ASSERTION(itemCount == prefixCnt, "Lengths are inconsistent");
memcpy(prefixArray, prefixes.Elements(), sizeof(uint32_t) * itemCount);
*aCount = itemCount;
*aPrefixes = prefixArray;

View File

@ -36,6 +36,7 @@ public:
NS_IMETHOD LoadFromFile(nsIFile* aFile);
NS_IMETHOD StoreToFile(nsIFile* aFile);
nsresult GetPrefixesNative(FallibleTArray<uint32_t>& outArray);
size_t SizeInMemory() { return mMemoryInUse; };
NS_DECL_THREADSAFE_ISUPPORTS