perf: tighten thumbnail cache memory usage
This commit is contained in:
@@ -37,7 +37,7 @@ func newThumbnailCache() *thumbnailCache {
|
||||
items: make(map[thumbnailCacheKey]*list.Element),
|
||||
order: list.New(),
|
||||
flights: make(map[thumbnailCacheKey]*thumbnailFlight),
|
||||
maxBytes: 32 << 20,
|
||||
maxBytes: 8 << 20,
|
||||
maxItems: 64,
|
||||
}
|
||||
}
|
||||
@@ -53,7 +53,7 @@ func (c *thumbnailCache) Do(key thumbnailCacheKey, fn func() ([]byte, error)) ([
|
||||
if f, ok := c.flights[key]; ok {
|
||||
c.mu.Unlock()
|
||||
<-f.done
|
||||
return append([]byte(nil), f.data...), f.err
|
||||
return f.data, f.err
|
||||
}
|
||||
f := &thumbnailFlight{done: make(chan struct{})}
|
||||
c.flights[key] = f
|
||||
@@ -63,7 +63,7 @@ func (c *thumbnailCache) Do(key thumbnailCacheKey, fn func() ([]byte, error)) ([
|
||||
if err == nil && len(data) > 0 && int64(len(data)) <= c.maxBytes {
|
||||
c.mu.Lock()
|
||||
c.putLocked(key, data)
|
||||
f.data = append([]byte(nil), data...)
|
||||
f.data = data
|
||||
f.err = err
|
||||
delete(c.flights, key)
|
||||
close(f.done)
|
||||
@@ -82,11 +82,11 @@ func (c *thumbnailCache) putLocked(key thumbnailCacheKey, data []byte) {
|
||||
if e, ok := c.items[key]; ok {
|
||||
old := e.Value.(thumbnailCacheEntry)
|
||||
c.bytes -= int64(len(old.data))
|
||||
e.Value = thumbnailCacheEntry{key: key, data: append([]byte(nil), data...)}
|
||||
e.Value = thumbnailCacheEntry{key: key, data: data}
|
||||
c.bytes += int64(len(data))
|
||||
c.order.MoveToFront(e)
|
||||
} else {
|
||||
e := c.order.PushFront(thumbnailCacheEntry{key: key, data: append([]byte(nil), data...)})
|
||||
e := c.order.PushFront(thumbnailCacheEntry{key: key, data: data})
|
||||
c.items[key] = e
|
||||
c.bytes += int64(len(data))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user