Skip to content
This repository was archived by the owner on Jun 19, 2023. It is now read-only.

Commit 5d1a33c

Browse files
committed
avoid fetching the size when not requested
1 parent 0d5887b commit 5d1a33c

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

arc_cache.go

+29-17
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import (
99
metrics "github.com/ipfs/go-metrics-interface"
1010
)
1111

12+
type cacheHave bool
13+
type cacheSize int
14+
1215
// arccache wraps a BlockStore with an Adaptive Replacement Cache (ARC) for
1316
// block Cids. This provides block access-time improvements, allowing
1417
// to short-cut many searches without query-ing the underlying datastore.
@@ -41,7 +44,7 @@ func (b *arccache) DeleteBlock(k *cid.Cid) error {
4144
err := b.blockstore.DeleteBlock(k)
4245
switch err {
4346
case nil, ErrNotFound:
44-
b.addCache(k, -1)
47+
b.cacheHave(k, false)
4548
return err
4649
default:
4750
return err
@@ -62,21 +65,26 @@ func (b *arccache) hasCached(k *cid.Cid) (has bool, size int, ok bool) {
6265
h, ok := b.arc.Get(k.KeyString())
6366
if ok {
6467
b.hits.Inc()
65-
if h.(int) > -1 {
66-
return true, h.(int), true
67-
} else {
68-
return false, h.(int), true
68+
switch h := h.(type) {
69+
case cacheHave:
70+
return bool(h), -1, true
71+
case cacheSize:
72+
return true, int(h), true
6973
}
7074
}
7175
return false, -1, false
7276
}
7377

7478
func (b *arccache) Has(k *cid.Cid) (bool, error) {
75-
blockSize, err := b.GetSize(k)
76-
if err == ErrNotFound {
77-
return false, nil
79+
if has, _, ok := b.hasCached(k); ok {
80+
return has, nil
81+
}
82+
has, err := b.blockstore.Has(k)
83+
if err != nil {
84+
return false, err
7885
}
79-
return blockSize > -1, err
86+
b.cacheHave(k, has)
87+
return has, nil
8088
}
8189

8290
func (b *arccache) GetSize(k *cid.Cid) (int, error) {
@@ -85,9 +93,9 @@ func (b *arccache) GetSize(k *cid.Cid) (int, error) {
8593
}
8694
blockSize, err := b.blockstore.GetSize(k)
8795
if err == ErrNotFound {
88-
b.addCache(k, -1)
96+
b.cacheHave(k, false)
8997
} else if err == nil {
90-
b.addCache(k, blockSize)
98+
b.cacheSize(k, blockSize)
9199
}
92100
return blockSize, err
93101
}
@@ -104,9 +112,9 @@ func (b *arccache) Get(k *cid.Cid) (blocks.Block, error) {
104112

105113
bl, err := b.blockstore.Get(k)
106114
if bl == nil && err == ErrNotFound {
107-
b.addCache(k, -1)
115+
b.cacheHave(k, false)
108116
} else if bl != nil {
109-
b.addCache(k, len(bl.RawData()))
117+
b.cacheSize(k, len(bl.RawData()))
110118
}
111119
return bl, err
112120
}
@@ -118,7 +126,7 @@ func (b *arccache) Put(bl blocks.Block) error {
118126

119127
err := b.blockstore.Put(bl)
120128
if err == nil {
121-
b.addCache(bl.Cid(), len(bl.RawData()))
129+
b.cacheSize(bl.Cid(), len(bl.RawData()))
122130
}
123131
return err
124132
}
@@ -137,7 +145,7 @@ func (b *arccache) PutMany(bs []blocks.Block) error {
137145
return err
138146
}
139147
for _, block := range good {
140-
b.addCache(block.Cid(), len(block.RawData()))
148+
b.cacheSize(block.Cid(), len(block.RawData()))
141149
}
142150
return nil
143151
}
@@ -146,8 +154,12 @@ func (b *arccache) HashOnRead(enabled bool) {
146154
b.blockstore.HashOnRead(enabled)
147155
}
148156

149-
func (b *arccache) addCache(c *cid.Cid, blockSize int) {
150-
b.arc.Add(c.KeyString(), blockSize)
157+
func (b *arccache) cacheHave(c *cid.Cid, have bool) {
158+
b.arc.Add(c.KeyString(), cacheHave(have))
159+
}
160+
161+
func (b *arccache) cacheSize(c *cid.Cid, blockSize int) {
162+
b.arc.Add(c.KeyString(), cacheSize(blockSize))
151163
}
152164

153165
func (b *arccache) AllKeysChan(ctx context.Context) (<-chan *cid.Cid, error) {

0 commit comments

Comments
 (0)