Commit 32b0408
authored
Bound native price cache with moka (#4154)
# Description
The native price cache is backed by an unbounded `Mutex<HashMap<Address,
CachedResult>>`. Entries are inserted on every price estimate but never
removed. Once at steady state with many unique tokens, the cache grows
without bound.
The size limit is currently hardcoded at 20k, since on mainnet we have
around that number of unique tokens:
```sql
SELECT COUNT(*) AS unique_token_count
FROM (SELECT sell_token AS token
FROM orders
UNION
SELECT buy_token AS token
FROM orders) t;
Returns -> 20109
```
Per cache entry breakdown:
- CachedResult value: ~32 bytes
- Address key: 20 bytes
- Moka internal overhead: ~100-120 bytes
- Total per entry: ~170 bytes
At 20,000 entries: ~3.4 MB upper bound
# Changes
- Replace the `Mutex<HashMap<...>>` cache with `moka::sync::Cache`.
- Adapt get_cached_price():
- No more MutexGuard parameter. Use data.get(&token) which returns
Option<CachedResult> (moka clones the value). Check staleness +
is_ready() on the returned value. No `requested_at` to update, since
moka tracks access internally.
- Remove the `requested_at` field from the `CachedResult` since it is
not needed anymore and turned out to be dead code.
- Adapt the `insert()` function to use
`entry_by_ref().and_compute_with()`, which gives gives atomic
read-modify-write for `accumulative_errors_count`.
## How to test
Existing tests.1 parent 95b5017 commit 32b0408
3 files changed
Lines changed: 41 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
| 56 | + | |
56 | 57 | | |
57 | 58 | | |
58 | 59 | | |
| |||
Lines changed: 39 additions & 39 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| |||
94 | 94 | | |
95 | 95 | | |
96 | 96 | | |
97 | | - | |
98 | 97 | | |
99 | 98 | | |
100 | 99 | | |
| |||
112 | 111 | | |
113 | 112 | | |
114 | 113 | | |
115 | | - | |
116 | 114 | | |
117 | 115 | | |
118 | 116 | | |
119 | 117 | | |
120 | 118 | | |
121 | 119 | | |
122 | | - | |
123 | 120 | | |
124 | 121 | | |
125 | 122 | | |
| |||
159 | 156 | | |
160 | 157 | | |
161 | 158 | | |
| 159 | + | |
| 160 | + | |
162 | 161 | | |
163 | | - | |
| 162 | + | |
164 | 163 | | |
165 | 164 | | |
166 | 165 | | |
| |||
169 | 168 | | |
170 | 169 | | |
171 | 170 | | |
172 | | - | |
173 | | - | |
174 | | - | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
175 | 177 | | |
176 | | - | |
| 178 | + | |
177 | 179 | | |
178 | 180 | | |
179 | | - | |
| 181 | + | |
180 | 182 | | |
181 | | - | |
182 | 183 | | |
183 | 184 | | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
192 | 190 | | |
193 | 191 | | |
194 | 192 | | |
| |||
205 | 203 | | |
206 | 204 | | |
207 | 205 | | |
208 | | - | |
| 206 | + | |
| 207 | + | |
209 | 208 | | |
210 | 209 | | |
211 | 210 | | |
212 | 211 | | |
213 | 212 | | |
214 | | - | |
| 213 | + | |
215 | 214 | | |
216 | 215 | | |
217 | | - | |
218 | | - | |
| 216 | + | |
219 | 217 | | |
220 | | - | |
| 218 | + | |
221 | 219 | | |
222 | 220 | | |
223 | 221 | | |
| |||
226 | 224 | | |
227 | 225 | | |
228 | 226 | | |
229 | | - | |
230 | 227 | | |
231 | 228 | | |
232 | | - | |
| 229 | + | |
233 | 230 | | |
234 | 231 | | |
235 | 232 | | |
| |||
243 | 240 | | |
244 | 241 | | |
245 | 242 | | |
246 | | - | |
247 | | - | |
248 | | - | |
249 | | - | |
250 | | - | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
251 | 254 | | |
252 | 255 | | |
253 | 256 | | |
| |||
311 | 314 | | |
312 | 315 | | |
313 | 316 | | |
| 317 | + | |
| 318 | + | |
314 | 319 | | |
315 | | - | |
316 | | - | |
317 | | - | |
318 | | - | |
| 320 | + | |
319 | 321 | | |
320 | 322 | | |
321 | 323 | | |
| |||
390 | 392 | | |
391 | 393 | | |
392 | 394 | | |
393 | | - | |
394 | | - | |
| 395 | + | |
395 | 396 | | |
396 | 397 | | |
397 | 398 | | |
| |||
565 | 566 | | |
566 | 567 | | |
567 | 568 | | |
568 | | - | |
569 | | - | |
| 569 | + | |
570 | 570 | | |
571 | 571 | | |
572 | 572 | | |
| |||
0 commit comments