Commit d201799
authored
fix(cache): Estimate size of posting lists (#9515)
**Description**
The maximum cost of the Ristretto cache is set to a number of megabytes,
but when inserting a posting list item into the cache, the cost is set
to 1. Because of this the cache only grows and no items get evicted.
This results in high memory consumption, especially with the new UID
cache (see #9513).
This PR introduces a cost function which estimates the memory size of
each item. For more details see
predictable-labs#6. Credits to
@darkcoderrises for the implementation.
The default cache size is changed from 1024 to 4096, to reflect the more
accurate cost estimation. See below for how the size of the cache
relates to the occupied memory by the Dgrpah process, when this PR is
applied.
size-mb=1024
Dgraph process occupies up to 3.7 GiB of memory
heap:
```
344.49MB 30.34% 30.34% 407.26MB 35.87% github.com/hypermodeinc/dgraph/v25/posting.(*List).calculateUids
130.37MB 11.48% 41.83% 130.37MB 11.48% github.com/dgraph-io/ristretto/v2.newCmRow
83.20MB 7.33% 49.15% 83.20MB 7.33% github.com/dgraph-io/badger/v4/skl.newArena
69.50MB 6.12% 55.28% 69.50MB 6.12% github.com/hypermodeinc/dgraph/v25/posting.(*List).Uids.func1
65.16MB 5.74% 61.02% 65.16MB 5.74% github.com/dgraph-io/ristretto/v2/z.(*Bloom).Size
53.27MB 4.69% 65.71% 53.27MB 4.69% github.com/hypermodeinc/dgraph/v25/posting.(*List).calculateUids.func1
```
size-mb=2048
Dgraph process occupies up to 5.4 GiB of memory
heap:
```
637.07MB 33.68% 33.68% 699.33MB 36.97% github.com/hypermodeinc/dgraph/v25/posting.(*List).calculateUids
260.63MB 13.78% 47.46% 260.63MB 13.78% github.com/dgraph-io/ristretto/v2.newCmRow
130.04MB 6.88% 54.34% 130.04MB 6.88% github.com/dgraph-io/ristretto/v2/z.(*Bloom).Size
99.01MB 5.23% 59.57% 99.01MB 5.23% github.com/hypermodeinc/dgraph/v25/posting.(*List).Uids.func1
89.89MB 4.75% 64.32% 89.89MB 4.75% github.com/dgraph-io/ristretto/v2.(*lockedMap[go.shape.*uint8]).Set
83.20MB 4.40% 68.72% 83.20MB 4.40% github.com/dgraph-io/badger/v4/skl.newArena
```
size-mb=4096
Dgraph process occupies up to 8.4 GiB of memory
heap:
```
1343.44MB 41.61% 41.61% 1404.71MB 43.51% github.com/hypermodeinc/dgraph/v25/posting.(*List).calculateUids
520.15MB 16.11% 57.72% 520.15MB 16.11% github.com/dgraph-io/ristretto/v2.newCmRow
260MB 8.05% 65.77% 260MB 8.05% github.com/dgraph-io/ristretto/v2/z.(*Bloom).Size
136.70MB 4.23% 70.01% 136.70MB 4.23% github.com/dgraph-io/ristretto/v2.(*lockedMap[go.shape.*uint8]).Set
106.51MB 3.30% 73.31% 106.51MB 3.30% reflect.New
83.20MB 2.58% 75.88% 83.20MB 2.58% github.com/dgraph-io/badger/v4/skl.newArena
```
size-mb=8192
Dgraph process occupies up to 13.3 GiB of memory
heap:
```
2539.78MB 45.10% 45.10% 2601.10MB 46.19% github.com/hypermodeinc/dgraph/v25/posting.(*List).calculateUids
1040.01MB 18.47% 63.56% 1040.01MB 18.47% github.com/dgraph-io/ristretto/v2.newCmRow
520MB 9.23% 72.80% 520MB 9.23% github.com/dgraph-io/ristretto/v2/z.(*Bloom).Size
223.52MB 3.97% 76.77% 223.52MB 3.97% reflect.New
157.97MB 2.81% 79.57% 157.97MB 2.81% github.com/dgraph-io/ristretto/v2.(*lockedMap[go.shape.*uint8]).Set
136.01MB 2.42% 81.99% 259.03MB 4.60% github.com/hypermodeinc/dgraph/v25/posting.copyList
```
Closes #95131 parent c5b88b5 commit d201799
3 files changed
+99
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
370 | 370 | | |
371 | 371 | | |
372 | 372 | | |
373 | | - | |
| 373 | + | |
374 | 374 | | |
375 | 375 | | |
376 | 376 | | |
| |||
508 | 508 | | |
509 | 509 | | |
510 | 510 | | |
511 | | - | |
| 511 | + | |
512 | 512 | | |
513 | 513 | | |
514 | 514 | | |
515 | 515 | | |
516 | 516 | | |
517 | 517 | | |
518 | | - | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
519 | 523 | | |
520 | 524 | | |
521 | 525 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 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 | + | |
19 | 110 | | |
20 | 111 | | |
21 | 112 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
44 | | - | |
| 44 | + | |
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| |||
0 commit comments