@@ -41,14 +41,16 @@ namespace {
41
41
42
42
class FrecencyComparator {
43
43
public:
44
- bool Equals (CacheIndexRecord* a, CacheIndexRecord* b) const {
44
+ bool Equals (const RefPtr<CacheIndexRecordWrapper>& a,
45
+ const RefPtr<CacheIndexRecordWrapper>& b) const {
45
46
if (!a || !b) {
46
47
return false ;
47
48
}
48
49
49
- return a->mFrecency == b->mFrecency ;
50
+ return a->Get ()-> mFrecency == b-> Get () ->mFrecency ;
50
51
}
51
- bool LessThan (CacheIndexRecord* a, CacheIndexRecord* b) const {
52
+ bool LessThan (const RefPtr<CacheIndexRecordWrapper>& a,
53
+ const RefPtr<CacheIndexRecordWrapper>& b) const {
52
54
// Removed (=null) entries must be at the end of the array.
53
55
if (!a) {
54
56
return false ;
@@ -58,39 +60,19 @@ class FrecencyComparator {
58
60
}
59
61
60
62
// Place entries with frecency 0 at the end of the non-removed entries.
61
- if (a->mFrecency == 0 ) {
63
+ if (a->Get ()-> mFrecency == 0 ) {
62
64
return false ;
63
65
}
64
- if (b->mFrecency == 0 ) {
66
+ if (b->Get ()-> mFrecency == 0 ) {
65
67
return true ;
66
68
}
67
69
68
- return a->mFrecency < b->mFrecency ;
70
+ return a->Get ()-> mFrecency < b-> Get () ->mFrecency ;
69
71
}
70
72
};
71
73
72
74
} // namespace
73
75
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
-
94
76
/* *
95
77
* This helper class is responsible for keeping CacheIndex::mIndexStats and
96
78
* CacheIndex::mFrecencyArray up to date.
@@ -109,18 +91,8 @@ class CacheIndexEntryAutoManage {
109
91
const CacheIndexEntry* entry = FindEntry ();
110
92
mIndex ->mIndexStats .BeforeChange (entry);
111
93
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 ;
124
96
}
125
97
}
126
98
@@ -134,29 +106,28 @@ class CacheIndexEntryAutoManage {
134
106
}
135
107
136
108
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 );
139
111
} else if (!entry && mOldRecord ) {
140
112
mIndex ->mFrecencyArray .RemoveRecord (mOldRecord );
141
113
mIndex ->RemoveRecordFromIterators (mOldRecord );
142
114
} else if (entry && mOldRecord ) {
143
- auto rec = entry->mRec .get ();
144
- if (rec != mOldRecord ) {
115
+ if (entry->mRec != mOldRecord ) {
145
116
// record has a different address, we have to replace it
146
- mIndex ->ReplaceRecordInIterators (mOldRecord , rec );
117
+ mIndex ->ReplaceRecordInIterators (mOldRecord , entry-> mRec );
147
118
148
- if (entry->mRec ->mFrecency == mOldFrecency ) {
119
+ if (entry->mRec ->Get ()-> mFrecency == mOldFrecency ) {
149
120
// If frecency hasn't changed simply replace the pointer
150
- mIndex ->mFrecencyArray .ReplaceRecord (mOldRecord , rec );
121
+ mIndex ->mFrecencyArray .ReplaceRecord (mOldRecord , entry-> mRec );
151
122
} else {
152
123
// Remove old pointer and insert the new one at the end of the array
153
124
mIndex ->mFrecencyArray .RemoveRecord (mOldRecord );
154
- mIndex ->mFrecencyArray .AppendRecord (rec );
125
+ mIndex ->mFrecencyArray .AppendRecord (entry-> mRec );
155
126
}
156
- } else if (entry->mRec ->mFrecency != mOldFrecency ) {
127
+ } else if (entry->mRec ->Get ()-> mFrecency != mOldFrecency ) {
157
128
// 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 );
160
131
}
161
132
} else {
162
133
// both entries were removed or not initialized, do nothing
@@ -198,7 +169,7 @@ class CacheIndexEntryAutoManage {
198
169
199
170
const SHA1Sum::Hash* mHash ;
200
171
RefPtr<CacheIndex> mIndex ;
201
- CacheIndexRecord* mOldRecord ;
172
+ RefPtr<CacheIndexRecordWrapper> mOldRecord ;
202
173
uint32_t mOldFrecency ;
203
174
bool mDoNotSearchInIndex ;
204
175
bool mDoNotSearchInUpdates ;
@@ -874,7 +845,6 @@ nsresult CacheIndex::RemoveEntry(const SHA1Sum::Hash* aHash) {
874
845
return NS_ERROR_NOT_AVAILABLE;
875
846
}
876
847
877
- CacheIndexRecord* removedRecord = nullptr ;
878
848
{
879
849
CacheIndexEntryAutoManage entryMng (aHash, index);
880
850
@@ -905,7 +875,6 @@ nsresult CacheIndex::RemoveEntry(const SHA1Sum::Hash* aHash) {
905
875
} else {
906
876
if (entry) {
907
877
if (!entry->IsDirty () && entry->IsFileEmpty ()) {
908
- removedRecord = entry->mRec .get ();
909
878
index->mIndex .RemoveEntry (entry);
910
879
entry = nullptr ;
911
880
} else {
@@ -947,13 +916,6 @@ nsresult CacheIndex::RemoveEntry(const SHA1Sum::Hash* aHash) {
947
916
updated->MarkFresh ();
948
917
}
949
918
}
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
957
919
index->StartUpdatingIndexIfNeeded ();
958
920
index->WriteIndexToDiskIfNeeded ();
959
921
@@ -1316,7 +1278,7 @@ nsresult CacheIndex::GetEntryForEviction(bool aIgnoreEmptyEntries,
1316
1278
index->mFrecencyArray .SortIfNeeded ();
1317
1279
1318
1280
for (auto iter = index->mFrecencyArray .Iter (); !iter.Done (); iter.Next ()) {
1319
- CacheIndexRecord* rec = iter.Get ();
1281
+ CacheIndexRecord* rec = iter.Get ()-> Get () ;
1320
1282
1321
1283
memcpy (&hash, rec->mHash , sizeof (SHA1Sum::Hash));
1322
1284
@@ -1433,11 +1395,11 @@ nsresult CacheIndex::GetCacheStats(nsILoadContextInfo* aInfo, uint32_t* aSize,
1433
1395
*aCount = 0 ;
1434
1396
1435
1397
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))
1438
1400
continue ;
1439
1401
1440
- *aSize += CacheIndexEntry::GetFileSize (*record );
1402
+ *aSize += CacheIndexEntry::GetFileSize (*(iter. Get ()-> Get ()) );
1441
1403
++*aCount;
1442
1404
}
1443
1405
@@ -1646,7 +1608,6 @@ void CacheIndex::ProcessPendingOperations() {
1646
1608
MOZ_ASSERT (update->IsFresh ());
1647
1609
1648
1610
CacheIndexEntry* entry = mIndex .GetEntry (*update->Hash ());
1649
- CacheIndexRecord* removedRecord = nullptr ;
1650
1611
{
1651
1612
CacheIndexEntryAutoManage emng (update->Hash (), this );
1652
1613
emng.DoNotSearchInUpdates ();
@@ -1660,7 +1621,6 @@ void CacheIndex::ProcessPendingOperations() {
1660
1621
// Entries with empty file are not stored in index on disk. Just
1661
1622
// remove the entry, but only in case the entry is not dirty, i.e.
1662
1623
// the entry file was empty when we wrote the index.
1663
- removedRecord = entry->mRec .get ();
1664
1624
mIndex .RemoveEntry (entry);
1665
1625
entry = nullptr ;
1666
1626
} else {
@@ -1681,13 +1641,6 @@ void CacheIndex::ProcessPendingOperations() {
1681
1641
*entry = *update;
1682
1642
}
1683
1643
}
1684
- #ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
1685
- if (removedRecord) {
1686
- MOZ_DIAGNOSTIC_ASSERT (!mFrecencyArray .RecordExisted (removedRecord));
1687
- }
1688
- #else
1689
- Unused << removedRecord;
1690
- #endif
1691
1644
iter.Remove ();
1692
1645
}
1693
1646
@@ -3385,23 +3338,23 @@ void CacheIndex::ReleaseBuffer() {
3385
3338
mRWBufPos = 0 ;
3386
3339
}
3387
3340
3388
- void CacheIndex::FrecencyArray::AppendRecord (CacheIndexRecord * aRecord) {
3341
+ void CacheIndex::FrecencyArray::AppendRecord (CacheIndexRecordWrapper * aRecord) {
3389
3342
LOG (
3390
3343
(" CacheIndex::FrecencyArray::AppendRecord() [record=%p, hash=%08x%08x%08x"
3391
3344
" %08x%08x]" ,
3392
- aRecord, LOGSHA1 (aRecord->mHash )));
3345
+ aRecord, LOGSHA1 (aRecord->Get ()-> mHash )));
3393
3346
3394
3347
MOZ_ASSERT (!mRecs .Contains (aRecord));
3395
3348
mRecs .AppendElement (aRecord);
3396
3349
3397
3350
// If the new frecency is 0, the element should be at the end of the array,
3398
3351
// i.e. this change doesn't affect order of the array
3399
- if (aRecord->mFrecency != 0 ) {
3352
+ if (aRecord->Get ()-> mFrecency != 0 ) {
3400
3353
++mUnsortedElements ;
3401
3354
}
3402
3355
}
3403
3356
3404
- void CacheIndex::FrecencyArray::RemoveRecord (CacheIndexRecord * aRecord) {
3357
+ void CacheIndex::FrecencyArray::RemoveRecord (CacheIndexRecordWrapper * aRecord) {
3405
3358
LOG ((" CacheIndex::FrecencyArray::RemoveRecord() [record=%p]" , aRecord));
3406
3359
3407
3360
decltype (mRecs )::index_type idx;
@@ -3415,8 +3368,8 @@ void CacheIndex::FrecencyArray::RemoveRecord(CacheIndexRecord* aRecord) {
3415
3368
SortIfNeeded ();
3416
3369
}
3417
3370
3418
- void CacheIndex::FrecencyArray::ReplaceRecord (CacheIndexRecord* aOldRecord,
3419
- CacheIndexRecord * aNewRecord) {
3371
+ void CacheIndex::FrecencyArray::ReplaceRecord (
3372
+ CacheIndexRecordWrapper* aOldRecord, CacheIndexRecordWrapper * aNewRecord) {
3420
3373
LOG (
3421
3374
(" CacheIndex::FrecencyArray::ReplaceRecord() [oldRecord=%p, "
3422
3375
" newRecord=%p]" ,
@@ -3428,10 +3381,6 @@ void CacheIndex::FrecencyArray::ReplaceRecord(CacheIndexRecord* aOldRecord,
3428
3381
mRecs [idx] = aNewRecord;
3429
3382
}
3430
3383
3431
- bool CacheIndex::FrecencyArray::RecordExisted (CacheIndexRecord* aRecord) {
3432
- return mRecs .Contains (aRecord);
3433
- }
3434
-
3435
3384
void CacheIndex::FrecencyArray::SortIfNeeded () {
3436
3385
const uint32_t kMaxUnsortedCount = 512 ;
3437
3386
const uint32_t kMaxUnsortedPercent = 10 ;
@@ -3463,7 +3412,7 @@ void CacheIndex::FrecencyArray::SortIfNeeded() {
3463
3412
}
3464
3413
}
3465
3414
3466
- void CacheIndex::AddRecordToIterators (CacheIndexRecord * aRecord) {
3415
+ void CacheIndex::AddRecordToIterators (CacheIndexRecordWrapper * aRecord) {
3467
3416
sLock .AssertCurrentThreadOwns ();
3468
3417
3469
3418
for (uint32_t i = 0 ; i < mIterators .Length (); ++i) {
@@ -3474,7 +3423,7 @@ void CacheIndex::AddRecordToIterators(CacheIndexRecord* aRecord) {
3474
3423
}
3475
3424
}
3476
3425
3477
- void CacheIndex::RemoveRecordFromIterators (CacheIndexRecord * aRecord) {
3426
+ void CacheIndex::RemoveRecordFromIterators (CacheIndexRecordWrapper * aRecord) {
3478
3427
sLock .AssertCurrentThreadOwns ();
3479
3428
3480
3429
for (uint32_t i = 0 ; i < mIterators .Length (); ++i) {
@@ -3485,8 +3434,8 @@ void CacheIndex::RemoveRecordFromIterators(CacheIndexRecord* aRecord) {
3485
3434
}
3486
3435
}
3487
3436
3488
- void CacheIndex::ReplaceRecordInIterators (CacheIndexRecord * aOldRecord,
3489
- CacheIndexRecord * aNewRecord) {
3437
+ void CacheIndex::ReplaceRecordInIterators (CacheIndexRecordWrapper * aOldRecord,
3438
+ CacheIndexRecordWrapper * aNewRecord) {
3490
3439
sLock .AssertCurrentThreadOwns ();
3491
3440
3492
3441
for (uint32_t i = 0 ; i < mIterators .Length (); ++i) {
0 commit comments