Skip to content

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

:zap: Improve block loading performance on Windows/Linux/macOS amd64 arch https://github.com/siyuan-note/siyuan/issues/9084

Daniel26d5832641f9
diff --git a/kernel/filesys/json_parser.go b/kernel/filesys/json_parser.go
index bf7a817533..0b5f234e6f 100644
--- a/kernel/filesys/json_parser.go
+++ b/kernel/filesys/json_parser.go
@@ -1,12 +1,18 @@
-// Lute - 一款结构化的 Markdown 引擎,支持 Go 和 JavaScript
-// Copyright (c) 2019-present, b3log.org
+// SiYuan - Refactor your thinking
+// Copyright (c) 2020-present, b3log.org
//
-// Lute is licensed under Mulan PSL v2.
-// You can use this software according to the terms and conditions of the Mulan PSL v2.
-// You may obtain a copy of Mulan PSL v2 at:
-// http://license.coscl.org.cn/MulanPSL2
-// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
-// See the Mulan PSL v2 for more details.
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
package filesys
@@ -14,17 +20,16 @@ import (
"bytes"
"strings"
+ "github.com/88250/gulu"
"github.com/88250/lute/ast"
"github.com/88250/lute/editor"
"github.com/88250/lute/parse"
- "github.com/88250/lute/util"
- "github.com/goccy/go-json"
"github.com/siyuan-note/siyuan/kernel/treenode"
)
func ParseJSONWithoutFix(jsonData []byte, options *parse.Options) (ret *parse.Tree, err error) {
root := &ast.Node{}
- err = json.Unmarshal(jsonData, root)
+ err = unmarshalJSON(jsonData, root)
if nil != err {
return
}
@@ -45,7 +50,7 @@ func ParseJSONWithoutFix(jsonData []byte, options *parse.Options) (ret *parse.Tr
func ParseJSON(jsonData []byte, options *parse.Options) (ret *parse.Tree, needFix bool, err error) {
root := &ast.Node{}
- err = json.Unmarshal(jsonData, root)
+ err = unmarshalJSON(jsonData, root)
if nil != err {
return
}
@@ -91,7 +96,7 @@ func ParseJSON(jsonData []byte, options *parse.Options) (ret *parse.Tree, needFi
}
func genTreeByJSON(node *ast.Node, tree *parse.Tree, idMap *map[string]bool, needFix, needMigrate2Spec1 *bool, ignoreFix bool) {
- node.Tokens, node.Type = util.StrToBytes(node.Data), ast.Str2NodeType(node.TypeStr)
+ node.Tokens, node.Type = gulu.Str.ToBytes(node.Data), ast.Str2NodeType(node.TypeStr)
node.Data, node.TypeStr = "", ""
node.KramdownIAL = parse.Map2IAL(node.Properties)
node.Properties = nil
@@ -178,14 +183,14 @@ func fixLegacyData(tip, node *ast.Node, idMap *map[string]bool, needFix, needMig
switch node.Type {
case ast.NodeIFrame:
- if bytes.Contains(node.Tokens, util.StrToBytes("iframe-content")) {
- start := bytes.Index(node.Tokens, util.StrToBytes("<iframe"))
- end := bytes.Index(node.Tokens, util.StrToBytes("</iframe>"))
+ if bytes.Contains(node.Tokens, gulu.Str.ToBytes("iframe-content")) {
+ start := bytes.Index(node.Tokens, gulu.Str.ToBytes("<iframe"))
+ end := bytes.Index(node.Tokens, gulu.Str.ToBytes("</iframe>"))
node.Tokens = node.Tokens[start : end+9]
*needFix = true
}
case ast.NodeWidget:
- if bytes.Contains(node.Tokens, util.StrToBytes("http://127.0.0.1:6806")) {
+ if bytes.Contains(node.Tokens, gulu.Str.ToBytes("http://127.0.0.1:6806")) {
node.Tokens = bytes.ReplaceAll(node.Tokens, []byte("http://127.0.0.1:6806"), nil)
*needFix = true
}
diff --git a/kernel/filesys/json_unmarshal.go b/kernel/filesys/json_unmarshal.go
new file mode 100644
index 0000000000..4b278f24d5
--- /dev/null
+++ b/kernel/filesys/json_unmarshal.go
@@ -0,0 +1,36 @@
+// SiYuan - Refactor your thinking
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+//go:build !(windows && amd64)
+
+package filesys
+
+import (
+ "time"
+
+ "github.com/goccy/go-json"
+ "github.com/siyuan-note/logging"
+)
+
+func unmarshalJSON(data []byte, v interface{}) error {
+ now := time.Now()
+ defer func() {
+ elapsed := time.Since(now)
+ logging.LogInfof("[go-json] unmarshalJSON took %s", elapsed)
+ }()
+
+ return json.Unmarshal(data, v)
+}
diff --git a/kernel/filesys/json_unmarshal_windows_amd64.go b/kernel/filesys/json_unmarshal_windows_amd64.go
new file mode 100644
index 0000000000..2d11ed0eb1
--- /dev/null
+++ b/kernel/filesys/json_unmarshal_windows_amd64.go
@@ -0,0 +1,34 @@
+// SiYuan - Refactor your thinking
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+package filesys
+
+import (
+ "time"
+
+ "github.com/bytedance/sonic"
+ "github.com/siyuan-note/logging"
+)
+
+func unmarshalJSON(data []byte, v interface{}) error {
+ now := time.Now()
+ defer func() {
+ elapsed := time.Since(now)
+ logging.LogInfof("[sonic] unmarshalJSON took %s", elapsed)
+ }()
+
+ return sonic.Unmarshal(data, v)
+}
diff --git a/kernel/go.mod b/kernel/go.mod
index ef764cc7af..6678454d7c 100644
--- a/kernel/go.mod
+++ b/kernel/go.mod
@@ -17,6 +17,7 @@ require (
github.com/PuerkitoBio/goquery v1.8.1
github.com/Xuanwo/go-locale v1.1.0
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de
+ github.com/bytedance/sonic v1.10.0
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be
github.com/denisbrodbeck/machineid v1.0.1
github.com/dgraph-io/ristretto v0.1.1
@@ -77,7 +78,6 @@ require (
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef // indirect
github.com/aws/aws-sdk-go v1.44.332 // indirect
- github.com/bytedance/sonic v1.10.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect