<style>
.reveal, .reveal h1, .reveal h2, .reveal h3, .reveal h4, .reveal h5, .reveal h6 { font-family: -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", Helvetica, Arial, PingFangTC-Light, "Microsoft JhengHei", "微軟正黑", sans-serif, "Apple Color Emoji" }
</style>
## 嘗試使用 go 改善修訂比對效能
<img src="https://i.imgur.com/PTbB46S.jpg" style="width:200px;height:200px;border-radius:50%"/>
#### by Max Wu (jackycute)
**@Golang Taipei Gathering #19**
---
# <i class="fa fa-file-text"></i> HackMD
**使用 Markdown 的跨平台即時協作筆記**
---
# 修訂版本
**也就是 Diff**
----
![](https://i.imgur.com/IeRECI0.png)
右上角有個「修訂版本」
----
![](https://i.imgur.com/G2Kk4pu.png)
長這樣
---
## 採用 google-diff-match-patch
https://code.google.com/p/google-diff-match-patch/
----
# 官方實作語言
**Java, JavaScript, Dart, C++, C#, Objective C, Lua, Python**
----
# 發現什麼了嗎?
對,沒有 Golang<!-- .element: class="fragment" data-fragment-index="1" -->
---
# go-diff
https://github.com/sergi/go-diff
---
## GNU diff format (Unified Format)
```
@@ -3,16 +3,19 @@
rem
-ipsum dolor
+dolor sit amet
.
```
----
# 步驟
- diff_main 比對字串
- diff_cleanupSemantic 清除多餘的結果
- patch_make 產生 patch
- patch_toText 轉成類似 GNU 格式
---
# 測試效能 - 短文字
- 執行 10 次
- 字串一:Lorem ipsum dolor.
- 字串二:Lorem dolor sit amet.
----
# Go
- 平均執行時間:0.063 ms
- 平均記憶體使用量:4.61 KB
----
# Node.js
- 平均執行時間:4.7 ms
- 平均記憶體使用量:131.18 KB
----
# 測試結果
- Go 比 Node.js 快 67 倍
- Node.js 比 Go 記憶體使用量多 28 倍
---
# 你以為這就結束了嗎?
---
# 測試效能 - 文字檔
- 執行 10 次
- 檔案一:長度 12979 字元
- 檔案二:長度 11918 字元
----
# Go
- 平均執行時間:359.783 ms
- 平均記憶體使用量:1686 KB
----
# Node.js
- 平均執行時間:1010.0 ms
- 平均記憶體使用量:2329 KB
----
# 測試結果
- Go 比 Node.js 快 2.8 倍
- Node.js 比 Go 記憶體使用量多 1.38 倍
---
# 結論
- Go 確實在 修訂比對上比 Node.js 效能好
- 但是文字長度與效能成反比
---
# 謝謝大家 :smile:
https://github.com/jackycute/hackmd-revision-go
{"metaMigratedAt":"2023-06-14T12:13:43.196Z","metaMigratedFrom":"YAML","title":"HackMD - 嘗試使用 go 改善修訂比對效能","breaks":"true","lang":"zh-tw","disqus":"hackmd","contributors":"[]"}