Skip to content

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

:art: 改进块树数据存取 https://github.com/siyuan-note/siyuan/issues/7168

Liang Ding7fdd3104ad59
diff --git a/kernel/treenode/blocktree.go b/kernel/treenode/blocktree.go
index 01db7bad14..4cddf92172 100644
--- a/kernel/treenode/blocktree.go
+++ b/kernel/treenode/blocktree.go
@@ -29,6 +29,7 @@ import (
"github.com/88250/lute/ast"
"github.com/88250/lute/parse"
"github.com/dustin/go-humanize"
+ "github.com/panjf2000/ants/v2"
util2 "github.com/siyuan-note/dejavu/util"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/util"
@@ -425,11 +426,13 @@ func InitBlockTree(force bool) {
}
size := uint64(0)
- for _, entry := range entries {
- if !strings.HasSuffix(entry.Name(), ".msgpack") {
- continue
- }
+ poolSize := runtime.NumCPU()
+ waitGroup := &sync.WaitGroup{}
+ p, _ := ants.NewPoolWithFunc(poolSize, func(arg interface{}) {
+ defer waitGroup.Done()
+
+ entry := arg.(os.DirEntry)
p := filepath.Join(util.BlockTreePath, entry.Name())
var fh *os.File
fh, err = os.OpenFile(p, os.O_RDWR, 0644)
@@ -461,13 +464,22 @@ func InitBlockTree(force bool) {
name := entry.Name()[0:strings.Index(entry.Name(), ".")]
blockTrees.Store(name, &btSlice{data: sliceData, changed: time.Time{}, m: &sync.Mutex{}})
size += uint64(len(data))
+ })
+ for _, entry := range entries {
+ if !strings.HasSuffix(entry.Name(), ".msgpack") {
+ continue
+ }
+
+ waitGroup.Add(1)
+ p.Invoke(entry)
}
- runtime.GC()
+ waitGroup.Wait()
+ p.Release()
- if elapsed := time.Since(start).Seconds(); 2 < elapsed {
- logging.LogWarnf("read block tree [%s] to [%s], elapsed [%.2fs]", humanize.Bytes((size)), util.BlockTreePath, elapsed)
- }
+ runtime.GC()
+ elapsed := time.Since(start).Seconds()
+ logging.LogInfof("read block tree [%s] to [%s], elapsed [%.2fs]", humanize.Bytes((size)), util.BlockTreePath, elapsed)
return
}