forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMruDictionary.h
More file actions
213 lines (180 loc) · 7.46 KB
/
MruDictionary.h
File metadata and controls
213 lines (180 loc) · 7.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#pragma once
namespace JsUtil
{
// - Maintains an MRU linked list of fixed maximum length and a dictionary that contains all added values
// - RemoveRecentlyUnusedItems should be called to remove values that are not in the MRU list from the dictionary
// - Added values are linked to the beginning of the MRU list (most recently used) as part of an MruListEntry, and when the
// list is full, entries from the end of the list (least recently used) are reused for the new values
// - TryGetValue, if the key exists in the dictionary, adds the value to the MRU list as the most recently used value, and
// removes the least recently used value as necessary
template<
class TKey,
class TValue,
class TAllocator = Recycler,
class TSizePolicy = PowerOf2SizePolicy,
template<class ValueOrKey> class TComparer = DefaultComparer,
template<class K, class V> class TDictionaryEntry = SimpleDictionaryEntry>
class MruDictionary
{
private:
struct MruListEntry : public DoublyLinkedListElement<MruListEntry, TAllocator>
{
Field(TValue, TAllocator) value;
Field(TKey, TAllocator) key;
Field(int) dictionaryDataIndex;
MruListEntry(const TKey &key, const TValue &value) : key(key), value(value), dictionaryDataIndex(0)
{
}
PREVENT_COPY(MruListEntry);
};
public:
class MruDictionaryData
{
private:
Field(MruListEntry *, TAllocator) entry;
Field(TValue, TAllocator) value;
public:
MruDictionaryData() : entry(nullptr)
{
}
MruDictionaryData &operator =(const void *const nullValue)
{
// Needed to support KeyValueEntry::Clear for dictionaries
Assert(!nullValue);
entry = nullptr;
value = nullptr; // TValue must also support this for the same reason
return *this;
}
MruListEntry *Entry() const
{
return entry;
}
const TValue &Value() const
{
Assert(!entry);
return value;
}
void OnAddedToMruList(MruListEntry *const entry)
{
Assert(!this->entry);
this->entry = entry;
}
void OnRemovedFromMruList()
{
Assert(entry);
value = entry->value;
entry = nullptr;
}
};
private:
Field(const int) mruListCapacity;
Field(int) mruListCount;
typedef DoublyLinkedList<MruListEntry, TAllocator> EntriesType;
Field(EntriesType) entries;
typedef
BaseDictionary<
TKey,
MruDictionaryData,
// MruDictionaryData always has pointer to GC pointer (MruEntry)
typename ForceNonLeafAllocator<TAllocator>::AllocatorType,
TSizePolicy,
TComparer,
TDictionaryEntry>
TDictionary;
TDictionary dictionary;
typedef typename TDictionary::AllocatorType AllocatorType;
public:
MruDictionary(AllocatorType *const allocator, const int mruListCapacity)
: mruListCapacity(mruListCapacity), mruListCount(0), dictionary(allocator)
{
Assert(allocator);
Assert(mruListCapacity > 0);
}
static MruDictionary *New(TAllocator *const allocator, DECLSPEC_GUARD_OVERFLOW const int mruListCapacity)
{
return AllocatorNew(TAllocator, allocator, MruDictionary, allocator, mruListCapacity);
}
private:
void AddToDictionary(MruListEntry *const entry)
{
const auto dictionaryDataIndex = dictionary.Add(entry->key, MruDictionaryData());
dictionary.GetReferenceAt(dictionaryDataIndex)->OnAddedToMruList(entry);
entry->dictionaryDataIndex = dictionaryDataIndex;
}
void ReuseLeastRecentlyUsedEntry(const TKey &key, const TValue &value, const int dictionaryDataIndex)
{
Assert(mruListCount == mruListCapacity);
// Reuse the least recently used entry for this key/value pair and make it the most recently used
const auto entry = entries.Tail();
dictionary.GetReferenceAt(dictionaryDataIndex)->OnAddedToMruList(entry);
dictionary.GetReferenceAt(entry->dictionaryDataIndex)->OnRemovedFromMruList();
entries.MoveToBeginning(entry);
entry->key = key;
entry->value = value;
entry->dictionaryDataIndex = dictionaryDataIndex;
}
public:
bool TryGetValue(const TKey &key, TValue *const value)
{
MruDictionaryData *dictionaryData;
int dictionaryDataIndex;
if(!dictionary.TryGetReference(key, &dictionaryData, &dictionaryDataIndex))
return false;
const auto entry = dictionaryData->Entry();
if(entry)
{
// Make this the most recently used entry
entries.MoveToBeginning(entry);
*value = entry->value;
return true;
}
*value = dictionaryData->Value();
// The key passed into this function may be temporary, and should not be placed in the MRU list or dictionary. Get
// the proper key to be used from the dictionary. That key should have the necessary lifetime.
ReuseLeastRecentlyUsedEntry(dictionary.GetKeyAt(dictionaryDataIndex), dictionaryData->Value(), dictionaryDataIndex);
return true;
}
void Add(const TKey &key, const TValue &value)
{
Assert(!dictionary.ContainsKey(key));
Assert(mruListCount <= mruListCapacity);
if(mruListCount == mruListCapacity)
{
ReuseLeastRecentlyUsedEntry(key, value, dictionary.Add(key, MruDictionaryData()));
return;
}
const auto entry = AllocatorNew(TAllocator, dictionary.GetAllocator(), MruListEntry, key, value);
AddToDictionary(entry);
entries.LinkToBeginning(entry);
++mruListCount;
}
void RemoveRecentlyUnusedItems()
{
if(dictionary.Count() == mruListCount)
return;
if(dictionary.Count() / 2 <= mruListCount)
{
dictionary.MapAndRemoveIf(
[](const typename TDictionary::EntryType &dictionaryEntry) -> bool
{
return !dictionaryEntry.Value().Entry();
});
return;
}
dictionary.Clear();
for(auto entry = entries.Head(); entry; entry = entry->Next())
AddToDictionary(entry);
}
void Clear()
{
dictionary.Clear();
entries.Clear();
mruListCount = 0;
}
PREVENT_COPY(MruDictionary);
};
}