Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Account for IAL cache memory costs https://github.com/siyuan-note/siyuan/issues/20349

Daniel708dc553a72f
diff --git a/kernel/cache/av.go b/kernel/cache/av.go
index cfe7e89a5e..72bac78035 100644
--- a/kernel/cache/av.go
+++ b/kernel/cache/av.go
@@ -24,7 +24,7 @@ import (
)
var avCache, _ = ristretto.NewCache(&ristretto.Config{
- NumCounters: 1000,
+ NumCounters: 100000,
MaxCost: 1024 * 1024 * 200,
BufferItems: 64,
})
diff --git a/kernel/cache/ial.go b/kernel/cache/ial.go
index 7739703899..6a4233c511 100644
--- a/kernel/cache/ial.go
+++ b/kernel/cache/ial.go
@@ -35,9 +35,23 @@ func docIALCacheKey(p, boxID string) string {
return boxID + "\x00" + p
}
+// ialCacheCost 保守估算映射、条目及字符串的驻留字节数,不依赖特定 Go 版本的映射布局。
+func ialCacheCost(key string, ial map[string]string) int64 {
+ cost := int64(256 + len(key))
+ for name, value := range ial {
+ cost += 128 + int64(len(name)) + int64(len(value))
+ }
+ return cost
+}
+
func PutDocIALInBox(p, boxID string, ial map[string]string) {
key := docIALCacheKey(p, boxID)
- docIALCache.Set(key, ial, 128)
+ cost := ialCacheCost(key, ial)
+ if cost >= docIALCache.MaxCost() {
+ RemoveDocIALInBox(p, boxID)
+ return
+ }
+ docIALCache.Set(key, maps.Clone(ial), cost)
docIALCacheKeysMu.Lock()
defer docIALCacheKeysMu.Unlock()
@@ -111,7 +125,12 @@ func PutBlockIAL(id string, ial map[string]string) {
func PutBlockIALInBox(id, boxID string, ial map[string]string) {
key := blockIALCacheKey(id, boxID)
- blockIALCache.Set(key, ial, 128)
+ cost := ialCacheCost(key, ial)
+ if cost >= blockIALCache.MaxCost() {
+ RemoveBlockIALInBox(id, boxID)
+ return
+ }
+ blockIALCache.Set(key, maps.Clone(ial), cost)
blockIALCacheKeysMu.Lock()
defer blockIALCacheKeysMu.Unlock()
@@ -132,7 +151,7 @@ func GetBlockIALInBox(id, boxID string) (ret map[string]string) {
if nil == ial {
return
}
- return ial.(map[string]string)
+ return maps.Clone(ial.(map[string]string))
}
// GetBlockIALWithBoxFallback 先查 box-aware key,未命中再回退到 bare key。
diff --git a/kernel/cache/ial_test.go b/kernel/cache/ial_test.go
new file mode 100644
index 0000000000..16ea212e75
--- /dev/null
+++ b/kernel/cache/ial_test.go
@@ -0,0 +1,79 @@
+package cache
+
+import (
+ "strings"
+ "testing"
+
+ "github.com/dgraph-io/ristretto"
+)
+
+func TestIALCacheChargesAttributePayload(t *testing.T) {
+ for _, document := range []bool{false, true} {
+ name := "block"
+ if document {
+ name = "document"
+ }
+ t.Run(name, func(t *testing.T) {
+ controlled, err := ristretto.NewCache(&ristretto.Config{
+ NumCounters: 100, MaxCost: 1024, BufferItems: 64,
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+ var put func(string, string, map[string]string)
+ var get func(string, string) map[string]string
+ if document {
+ original := docIALCache
+ docIALCache = controlled
+ put, get = PutDocIALInBox, GetDocIALInBox
+ t.Cleanup(func() {
+ ClearDocsIAL()
+ controlled.Close()
+ docIALCache = original
+ })
+ } else {
+ original := blockIALCache
+ blockIALCache = controlled
+ put, get = PutBlockIALInBox, GetBlockIALInBox
+ t.Cleanup(func() {
+ ClearBlocksIAL()
+ controlled.Close()
+ blockIALCache = original
+ })
+ }
+ attributes := map[string]string{"id": "small"}
+ put("small", "box", attributes)
+ controlled.Wait()
+ attributes["id"] = "changed"
+ if got := get("small", "box"); got["id"] != "small" {
+ t.Fatal("small attributes should fit in the cache")
+ }
+ get("small", "box")["id"] = "changed"
+ if got := get("small", "box"); got["id"] != "small" {
+ t.Fatal("caller mutation changed the accounted cache snapshot")
+ }
+ put("large", "box", map[string]string{"custom-value": strings.Repeat("x", 2048)})
+ controlled.Wait()
+ if got := get("large", "box"); got != nil {
+ t.Fatal("attributes exceeding the entire byte budget were admitted")
+ }
+ many := map[string]string{}
+ for i := range 20 {
+ many[strings.Repeat("x", i+1)] = "value"
+ }
+ put("many", "box", many)
+ controlled.Wait()
+ if got := get("many", "box"); got != nil {
+ t.Fatal("map entry overhead was not charged to the byte budget")
+ }
+ if got := get("small", "other-box"); got != nil {
+ t.Fatal("attributes leaked across notebook cache keys")
+ }
+ put("small", "box", map[string]string{"custom-value": strings.Repeat("x", 2048)})
+ controlled.Wait()
+ if got := get("small", "box"); got != nil {
+ t.Fatal("an oversized update kept stale or over-budget attributes")
+ }
+ })
+ }
+}