perf: tighten thumbnail cache memory usage

This commit is contained in:
OpenClaw Agent
2026-09-22 17:39:11 +08:00
parent a2113f831d
commit 9af81fdc8d
2 changed files with 21 additions and 5 deletions
+5 -5
View File
@@ -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))
}