Improve load tree performance

This commit is contained in:
Daniel 2024-06-22 00:01:20 +08:00
parent cb16111314
commit 1452e7cf3f
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
3 changed files with 19 additions and 22 deletions

View file

@ -37,18 +37,20 @@ import (
) )
func LoadTrees(ids []string) (ret map[string]*parse.Tree) { 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) bts := treenode.GetBlockTrees(ids)
luteEngine := util.NewLute() luteEngine := util.NewLute()
for id, bt := range bts { for id, bt := range bts {
if nil == ret[id] { tree := tmpCache[bt.RootID]
tree, err := LoadTree(bt.BoxID, bt.Path, luteEngine) if nil == tree {
if nil != err { tree, _ = LoadTree(bt.BoxID, bt.Path, luteEngine)
logging.LogErrorf("load tree [%s] failed: %s", bt.Path, err) if nil == tree {
logging.LogWarnf("load tree [%s] failed: %s", id, bt.Path)
continue continue
} }
ret[id] = tree tmpCache[bt.RootID] = tree
} }
ret[id] = tree
} }
return return
} }

View file

@ -557,11 +557,16 @@ func (box *Box) UpdateHistoryGenerated() {
func getBoxesByPaths(paths []string) (ret map[string]*Box) { func getBoxesByPaths(paths []string) (ret map[string]*Box) {
ret = map[string]*Box{} ret = map[string]*Box{}
var ids []string
for _, p := range paths { for _, p := range paths {
id := strings.TrimSuffix(path.Base(p), ".sy") ids = append(ids, strings.TrimSuffix(path.Base(p), ".sy"))
bt := treenode.GetBlockTree(id) }
bts := treenode.GetBlockTrees(ids)
for _, id := range ids {
bt := bts[id]
if nil != bt { if nil != bt {
ret[p] = Conf.Box(bt.BoxID) ret[bt.Path] = Conf.Box(bt.BoxID)
} }
} }
return return

View file

@ -465,21 +465,11 @@ func contentStat(content string, luteEngine *lute.Lute) (ret *util.BlockStatResu
func BlocksWordCount(ids []string) (ret *util.BlockStatResult) { func BlocksWordCount(ids []string) (ret *util.BlockStatResult) {
ret = &util.BlockStatResult{} ret = &util.BlockStatResult{}
trees := map[string]*parse.Tree{} // 缓存 trees := filesys.LoadTrees(ids)
luteEngine := util.NewLute()
for _, id := range ids { for _, id := range ids {
bt := treenode.GetBlockTree(id) tree := trees[id]
if nil == bt {
continue
}
tree := trees[bt.RootID]
if nil == tree { if nil == tree {
tree, _ = filesys.LoadTree(bt.BoxID, bt.Path, luteEngine) continue
if nil == tree {
continue
}
trees[bt.RootID] = tree
} }
node := treenode.GetNodeInTree(tree, id) node := treenode.GetNodeInTree(tree, id)