Skip to content

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

:zap: Improve load tree performance

Daniel1452e7cf3f5d
diff --git a/kernel/filesys/tree.go b/kernel/filesys/tree.go
index fb768d2f38..029257b265 100644
--- a/kernel/filesys/tree.go
+++ b/kernel/filesys/tree.go
@@ -37,18 +37,20 @@ import (
)
func LoadTrees(ids []string) (ret map[string]*parse.Tree) {
- ret = map[string]*parse.Tree{}
+ ret, tmpCache := map[string]*parse.Tree{}, map[string]*parse.Tree{}
bts := treenode.GetBlockTrees(ids)
luteEngine := util.NewLute()
for id, bt := range bts {
- if nil == ret[id] {
- tree, err := LoadTree(bt.BoxID, bt.Path, luteEngine)
- if nil != err {
- logging.LogErrorf("load tree [%s] failed: %s", bt.Path, err)
+ tree := tmpCache[bt.RootID]
+ if nil == tree {
+ tree, _ = LoadTree(bt.BoxID, bt.Path, luteEngine)
+ if nil == tree {
+ logging.LogWarnf("load tree [%s] failed: %s", id, bt.Path)
continue
}
- ret[id] = tree
+ tmpCache[bt.RootID] = tree
}
+ ret[id] = tree
}
return
}
diff --git a/kernel/model/box.go b/kernel/model/box.go
index 1f56065af5..69600fd5fd 100644
--- a/kernel/model/box.go
+++ b/kernel/model/box.go
@@ -557,11 +557,16 @@ func (box *Box) UpdateHistoryGenerated() {
func getBoxesByPaths(paths []string) (ret map[string]*Box) {
ret = map[string]*Box{}
+ var ids []string
for _, p := range paths {
- id := strings.TrimSuffix(path.Base(p), ".sy")
- bt := treenode.GetBlockTree(id)
+ ids = append(ids, strings.TrimSuffix(path.Base(p), ".sy"))
+ }
+
+ bts := treenode.GetBlockTrees(ids)
+ for _, id := range ids {
+ bt := bts[id]
if nil != bt {
- ret[p] = Conf.Box(bt.BoxID)
+ ret[bt.Path] = Conf.Box(bt.BoxID)
}
}
return
diff --git a/kernel/model/file.go b/kernel/model/file.go
index c09571b5b1..85aaf3c395 100644
--- a/kernel/model/file.go
+++ b/kernel/model/file.go
@@ -465,21 +465,11 @@ func contentStat(content string, luteEngine *lute.Lute) (ret *util.BlockStatResu
func BlocksWordCount(ids []string) (ret *util.BlockStatResult) {
ret = &util.BlockStatResult{}
- trees := map[string]*parse.Tree{} // 缓存
- luteEngine := util.NewLute()
+ trees := filesys.LoadTrees(ids)
for _, id := range ids {
- bt := treenode.GetBlockTree(id)
- if nil == bt {
- continue
- }
-
- tree := trees[bt.RootID]
+ tree := trees[id]
if nil == tree {
- tree, _ = filesys.LoadTree(bt.BoxID, bt.Path, luteEngine)
- if nil == tree {
- continue
- }
- trees[bt.RootID] = tree
+ continue
}
node := treenode.GetNodeInTree(tree, id)