Skip to content

Commit f5dce82

Browse files
committed
Bug 1662676 - Wrap CacheIndexRecord, r=necko-reviewers,dragana
Differential Revision: https://phabricator.services.mozilla.com/D113112
1 parent ff7e90c commit f5dce82

File tree

6 files changed

+173
-207
lines changed

6 files changed

+173
-207
lines changed

netwerk/cache2/CacheIndex.cpp

Lines changed: 35 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,16 @@ namespace {
4141

4242
class FrecencyComparator {
4343
public:
44-
bool Equals(CacheIndexRecord* a, CacheIndexRecord* b) const {
44+
bool Equals(const RefPtr<CacheIndexRecordWrapper>& a,
45+
const RefPtr<CacheIndexRecordWrapper>& b) const {
4546
if (!a || !b) {
4647
return false;
4748
}
4849

49-
return a->mFrecency == b->mFrecency;
50+
return a->Get()->mFrecency == b->Get()->mFrecency;
5051
}
51-
bool LessThan(CacheIndexRecord* a, CacheIndexRecord* b) const {
52+
bool LessThan(const RefPtr<CacheIndexRecordWrapper>& a,
53+
const RefPtr<CacheIndexRecordWrapper>& b) const {
5254
// Removed (=null) entries must be at the end of the array.
5355
if (!a) {
5456
return false;
@@ -58,39 +60,19 @@ class FrecencyComparator {
5860
}
5961

6062
// Place entries with frecency 0 at the end of the non-removed entries.
61-
if (a->mFrecency == 0) {
63+
if (a->Get()->mFrecency == 0) {
6264
return false;
6365
}
64-
if (b->mFrecency == 0) {
66+
if (b->Get()->mFrecency == 0) {
6567
return true;
6668
}
6769

68-
return a->mFrecency < b->mFrecency;
70+
return a->Get()->mFrecency < b->Get()->mFrecency;
6971
}
7072
};
7173

7274
} // namespace
7375

74-
CacheIndexRecord::~CacheIndexRecord() {
75-
if (!StaticPrefs::network_cache_frecency_array_check_enabled()) {
76-
return;
77-
}
78-
79-
if (!(mFlags & CacheIndexEntry::kDirtyMask) &&
80-
((mFlags & CacheIndexEntry::kFileSizeMask) == 0)) {
81-
return;
82-
}
83-
84-
RefPtr<CacheIndex> index = CacheIndex::gInstance;
85-
if (index) {
86-
CacheIndex::sLock.AssertCurrentThreadOwns();
87-
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
88-
bool found = index->mFrecencyArray.RecordExisted(this);
89-
MOZ_DIAGNOSTIC_ASSERT(!found);
90-
#endif
91-
}
92-
}
93-
9476
/**
9577
* This helper class is responsible for keeping CacheIndex::mIndexStats and
9678
* CacheIndex::mFrecencyArray up to date.
@@ -109,18 +91,8 @@ class CacheIndexEntryAutoManage {
10991
const CacheIndexEntry* entry = FindEntry();
11092
mIndex->mIndexStats.BeforeChange(entry);
11193
if (entry && entry->IsInitialized() && !entry->IsRemoved()) {
112-
mOldRecord = entry->mRec.get();
113-
mOldFrecency = entry->mRec->mFrecency;
114-
} else {
115-
if (entry) {
116-
// If we are here, it means mOldRecord is null. We'd like to check this
117-
// entry's record is not in the frecency array, since we remove the
118-
// record from the frecency array only when mOldRecord is not null.
119-
if (mIndex->mFrecencyArray.RecordExisted(entry->mRec.get())) {
120-
MOZ_DIAGNOSTIC_ASSERT(false);
121-
mIndex->mFrecencyArray.RemoveRecord(entry->mRec.get());
122-
}
123-
}
94+
mOldRecord = entry->mRec;
95+
mOldFrecency = entry->mRec->Get()->mFrecency;
12496
}
12597
}
12698

@@ -134,29 +106,28 @@ class CacheIndexEntryAutoManage {
134106
}
135107

136108
if (entry && !mOldRecord) {
137-
mIndex->mFrecencyArray.AppendRecord(entry->mRec.get());
138-
mIndex->AddRecordToIterators(entry->mRec.get());
109+
mIndex->mFrecencyArray.AppendRecord(entry->mRec);
110+
mIndex->AddRecordToIterators(entry->mRec);
139111
} else if (!entry && mOldRecord) {
140112
mIndex->mFrecencyArray.RemoveRecord(mOldRecord);
141113
mIndex->RemoveRecordFromIterators(mOldRecord);
142114
} else if (entry && mOldRecord) {
143-
auto rec = entry->mRec.get();
144-
if (rec != mOldRecord) {
115+
if (entry->mRec != mOldRecord) {
145116
// record has a different address, we have to replace it
146-
mIndex->ReplaceRecordInIterators(mOldRecord, rec);
117+
mIndex->ReplaceRecordInIterators(mOldRecord, entry->mRec);
147118

148-
if (entry->mRec->mFrecency == mOldFrecency) {
119+
if (entry->mRec->Get()->mFrecency == mOldFrecency) {
149120
// If frecency hasn't changed simply replace the pointer
150-
mIndex->mFrecencyArray.ReplaceRecord(mOldRecord, rec);
121+
mIndex->mFrecencyArray.ReplaceRecord(mOldRecord, entry->mRec);
151122
} else {
152123
// Remove old pointer and insert the new one at the end of the array
153124
mIndex->mFrecencyArray.RemoveRecord(mOldRecord);
154-
mIndex->mFrecencyArray.AppendRecord(rec);
125+
mIndex->mFrecencyArray.AppendRecord(entry->mRec);
155126
}
156-
} else if (entry->mRec->mFrecency != mOldFrecency) {
127+
} else if (entry->mRec->Get()->mFrecency != mOldFrecency) {
157128
// Move the element at the end of the array
158-
mIndex->mFrecencyArray.RemoveRecord(rec);
159-
mIndex->mFrecencyArray.AppendRecord(rec);
129+
mIndex->mFrecencyArray.RemoveRecord(entry->mRec);
130+
mIndex->mFrecencyArray.AppendRecord(entry->mRec);
160131
}
161132
} else {
162133
// both entries were removed or not initialized, do nothing
@@ -198,7 +169,7 @@ class CacheIndexEntryAutoManage {
198169

199170
const SHA1Sum::Hash* mHash;
200171
RefPtr<CacheIndex> mIndex;
201-
CacheIndexRecord* mOldRecord;
172+
RefPtr<CacheIndexRecordWrapper> mOldRecord;
202173
uint32_t mOldFrecency;
203174
bool mDoNotSearchInIndex;
204175
bool mDoNotSearchInUpdates;
@@ -874,7 +845,6 @@ nsresult CacheIndex::RemoveEntry(const SHA1Sum::Hash* aHash) {
874845
return NS_ERROR_NOT_AVAILABLE;
875846
}
876847

877-
CacheIndexRecord* removedRecord = nullptr;
878848
{
879849
CacheIndexEntryAutoManage entryMng(aHash, index);
880850

@@ -905,7 +875,6 @@ nsresult CacheIndex::RemoveEntry(const SHA1Sum::Hash* aHash) {
905875
} else {
906876
if (entry) {
907877
if (!entry->IsDirty() && entry->IsFileEmpty()) {
908-
removedRecord = entry->mRec.get();
909878
index->mIndex.RemoveEntry(entry);
910879
entry = nullptr;
911880
} else {
@@ -947,13 +916,6 @@ nsresult CacheIndex::RemoveEntry(const SHA1Sum::Hash* aHash) {
947916
updated->MarkFresh();
948917
}
949918
}
950-
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
951-
if (removedRecord) {
952-
MOZ_DIAGNOSTIC_ASSERT(!index->mFrecencyArray.RecordExisted(removedRecord));
953-
}
954-
#else
955-
Unused << removedRecord;
956-
#endif
957919
index->StartUpdatingIndexIfNeeded();
958920
index->WriteIndexToDiskIfNeeded();
959921

@@ -1316,7 +1278,7 @@ nsresult CacheIndex::GetEntryForEviction(bool aIgnoreEmptyEntries,
13161278
index->mFrecencyArray.SortIfNeeded();
13171279

13181280
for (auto iter = index->mFrecencyArray.Iter(); !iter.Done(); iter.Next()) {
1319-
CacheIndexRecord* rec = iter.Get();
1281+
CacheIndexRecord* rec = iter.Get()->Get();
13201282

13211283
memcpy(&hash, rec->mHash, sizeof(SHA1Sum::Hash));
13221284

@@ -1433,11 +1395,11 @@ nsresult CacheIndex::GetCacheStats(nsILoadContextInfo* aInfo, uint32_t* aSize,
14331395
*aCount = 0;
14341396

14351397
for (auto iter = index->mFrecencyArray.Iter(); !iter.Done(); iter.Next()) {
1436-
CacheIndexRecord* record = iter.Get();
1437-
if (aInfo && !CacheIndexEntry::RecordMatchesLoadContextInfo(record, aInfo))
1398+
if (aInfo &&
1399+
!CacheIndexEntry::RecordMatchesLoadContextInfo(iter.Get(), aInfo))
14381400
continue;
14391401

1440-
*aSize += CacheIndexEntry::GetFileSize(*record);
1402+
*aSize += CacheIndexEntry::GetFileSize(*(iter.Get()->Get()));
14411403
++*aCount;
14421404
}
14431405

@@ -1646,7 +1608,6 @@ void CacheIndex::ProcessPendingOperations() {
16461608
MOZ_ASSERT(update->IsFresh());
16471609

16481610
CacheIndexEntry* entry = mIndex.GetEntry(*update->Hash());
1649-
CacheIndexRecord* removedRecord = nullptr;
16501611
{
16511612
CacheIndexEntryAutoManage emng(update->Hash(), this);
16521613
emng.DoNotSearchInUpdates();
@@ -1660,7 +1621,6 @@ void CacheIndex::ProcessPendingOperations() {
16601621
// Entries with empty file are not stored in index on disk. Just
16611622
// remove the entry, but only in case the entry is not dirty, i.e.
16621623
// the entry file was empty when we wrote the index.
1663-
removedRecord = entry->mRec.get();
16641624
mIndex.RemoveEntry(entry);
16651625
entry = nullptr;
16661626
} else {
@@ -1681,13 +1641,6 @@ void CacheIndex::ProcessPendingOperations() {
16811641
*entry = *update;
16821642
}
16831643
}
1684-
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
1685-
if (removedRecord) {
1686-
MOZ_DIAGNOSTIC_ASSERT(!mFrecencyArray.RecordExisted(removedRecord));
1687-
}
1688-
#else
1689-
Unused << removedRecord;
1690-
#endif
16911644
iter.Remove();
16921645
}
16931646

@@ -3385,23 +3338,23 @@ void CacheIndex::ReleaseBuffer() {
33853338
mRWBufPos = 0;
33863339
}
33873340

3388-
void CacheIndex::FrecencyArray::AppendRecord(CacheIndexRecord* aRecord) {
3341+
void CacheIndex::FrecencyArray::AppendRecord(CacheIndexRecordWrapper* aRecord) {
33893342
LOG(
33903343
("CacheIndex::FrecencyArray::AppendRecord() [record=%p, hash=%08x%08x%08x"
33913344
"%08x%08x]",
3392-
aRecord, LOGSHA1(aRecord->mHash)));
3345+
aRecord, LOGSHA1(aRecord->Get()->mHash)));
33933346

33943347
MOZ_ASSERT(!mRecs.Contains(aRecord));
33953348
mRecs.AppendElement(aRecord);
33963349

33973350
// If the new frecency is 0, the element should be at the end of the array,
33983351
// i.e. this change doesn't affect order of the array
3399-
if (aRecord->mFrecency != 0) {
3352+
if (aRecord->Get()->mFrecency != 0) {
34003353
++mUnsortedElements;
34013354
}
34023355
}
34033356

3404-
void CacheIndex::FrecencyArray::RemoveRecord(CacheIndexRecord* aRecord) {
3357+
void CacheIndex::FrecencyArray::RemoveRecord(CacheIndexRecordWrapper* aRecord) {
34053358
LOG(("CacheIndex::FrecencyArray::RemoveRecord() [record=%p]", aRecord));
34063359

34073360
decltype(mRecs)::index_type idx;
@@ -3415,8 +3368,8 @@ void CacheIndex::FrecencyArray::RemoveRecord(CacheIndexRecord* aRecord) {
34153368
SortIfNeeded();
34163369
}
34173370

3418-
void CacheIndex::FrecencyArray::ReplaceRecord(CacheIndexRecord* aOldRecord,
3419-
CacheIndexRecord* aNewRecord) {
3371+
void CacheIndex::FrecencyArray::ReplaceRecord(
3372+
CacheIndexRecordWrapper* aOldRecord, CacheIndexRecordWrapper* aNewRecord) {
34203373
LOG(
34213374
("CacheIndex::FrecencyArray::ReplaceRecord() [oldRecord=%p, "
34223375
"newRecord=%p]",
@@ -3428,10 +3381,6 @@ void CacheIndex::FrecencyArray::ReplaceRecord(CacheIndexRecord* aOldRecord,
34283381
mRecs[idx] = aNewRecord;
34293382
}
34303383

3431-
bool CacheIndex::FrecencyArray::RecordExisted(CacheIndexRecord* aRecord) {
3432-
return mRecs.Contains(aRecord);
3433-
}
3434-
34353384
void CacheIndex::FrecencyArray::SortIfNeeded() {
34363385
const uint32_t kMaxUnsortedCount = 512;
34373386
const uint32_t kMaxUnsortedPercent = 10;
@@ -3463,7 +3412,7 @@ void CacheIndex::FrecencyArray::SortIfNeeded() {
34633412
}
34643413
}
34653414

3466-
void CacheIndex::AddRecordToIterators(CacheIndexRecord* aRecord) {
3415+
void CacheIndex::AddRecordToIterators(CacheIndexRecordWrapper* aRecord) {
34673416
sLock.AssertCurrentThreadOwns();
34683417

34693418
for (uint32_t i = 0; i < mIterators.Length(); ++i) {
@@ -3474,7 +3423,7 @@ void CacheIndex::AddRecordToIterators(CacheIndexRecord* aRecord) {
34743423
}
34753424
}
34763425

3477-
void CacheIndex::RemoveRecordFromIterators(CacheIndexRecord* aRecord) {
3426+
void CacheIndex::RemoveRecordFromIterators(CacheIndexRecordWrapper* aRecord) {
34783427
sLock.AssertCurrentThreadOwns();
34793428

34803429
for (uint32_t i = 0; i < mIterators.Length(); ++i) {
@@ -3485,8 +3434,8 @@ void CacheIndex::RemoveRecordFromIterators(CacheIndexRecord* aRecord) {
34853434
}
34863435
}
34873436

3488-
void CacheIndex::ReplaceRecordInIterators(CacheIndexRecord* aOldRecord,
3489-
CacheIndexRecord* aNewRecord) {
3437+
void CacheIndex::ReplaceRecordInIterators(CacheIndexRecordWrapper* aOldRecord,
3438+
CacheIndexRecordWrapper* aNewRecord) {
34903439
sLock.AssertCurrentThreadOwns();
34913440

34923441
for (uint32_t i = 0; i < mIterators.Length(); ++i) {

0 commit comments

Comments
 (0)