From c130b01f281f80300dc4d3341591ee22f08514f8 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Wed, 25 Jan 2023 20:21:53 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E6=94=B9=E8=BF=9B=E5=86=85=E6=A0=B8?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E8=B0=83=E5=BA=A6=E6=9C=BA=E5=88=B6=E6=8F=90?= =?UTF-8?q?=E5=8D=87=E7=A8=B3=E5=AE=9A=E6=80=A7=20https://github.com/siyua?= =?UTF-8?q?n-note/siyuan/issues/7113?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/model/index.go | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/kernel/model/index.go b/kernel/model/index.go index 080a7a25b..4c8490ce1 100644 --- a/kernel/model/index.go +++ b/kernel/model/index.go @@ -18,18 +18,21 @@ package model import ( "fmt" - "github.com/siyuan-note/siyuan/kernel/task" + "runtime" "strings" + "sync" "time" "github.com/88250/lute/parse" "github.com/dustin/go-humanize" "github.com/emirpasic/gods/sets/hashset" + "github.com/panjf2000/ants/v2" "github.com/siyuan-note/eventbus" "github.com/siyuan-note/logging" "github.com/siyuan-note/siyuan/kernel/cache" "github.com/siyuan-note/siyuan/kernel/filesys" "github.com/siyuan-note/siyuan/kernel/sql" + "github.com/siyuan-note/siyuan/kernel/task" "github.com/siyuan-note/siyuan/kernel/treenode" "github.com/siyuan-note/siyuan/kernel/util" ) @@ -82,15 +85,20 @@ func index(boxID string) { var treeSize int64 i := 0 util.PushStatusBar(fmt.Sprintf("["+box.Name+"] "+Conf.Language(64), len(files))) - for _, file := range files { - if file.isdir || !strings.HasSuffix(file.name, ".sy") { - continue - } + poolSize := runtime.NumCPU() + if 4 < poolSize { + poolSize = 4 + } + waitGroup := &sync.WaitGroup{} + p, _ := ants.NewPoolWithFunc(poolSize, func(arg interface{}) { + defer waitGroup.Done() + + file := arg.(*FileInfo) tree, err := filesys.LoadTree(box.ID, file.path, luteEngine) if nil != err { logging.LogErrorf("read box [%s] tree [%s] failed: %s", box.ID, file.path, err) - continue + return } docIAL := parse.IAL2MapUnEsc(tree.Root.KramdownIAL) @@ -114,7 +122,17 @@ func index(boxID string) { util.PushStatusBar(fmt.Sprintf(Conf.Language(88), i, len(files)-i)) } i++ + }) + for _, file := range files { + if file.isdir || !strings.HasSuffix(file.name, ".sy") { + continue + } + + waitGroup.Add(1) + p.Invoke(file) } + waitGroup.Wait() + p.Release() box.UpdateHistoryGenerated() // 初始化历史生成时间为当前时间 end := time.Now()