This report summarizes three notable performance optimization pull requests merged in the past two weeks across popular open-source Golang projects. Each PR targets a specific bottleneck — ranging from algorithmic complexity reduction to in-memory caching — and delivers measurable, significant speedups.
在實作檔案上傳並加解密的服務時,遇到了 user request 數量增多,造成 lead time 大幅提升的問題。服務本身是使用 Python + FastAPI framework 實作,在排除了網路頻寬問題和 server 效能問題後,懷疑是 Python 或 framework 導致延遲時間拉長,所以就決定從此地方著手進行 benchmark 實驗,來觀察瓶頸是發生在何處。
由於最終目的還是希望能找出改善的方式,而既然認為問題是出在 Python 和 framework 上,這次實驗就會先使用 FastAPI 與 aiohttp 寫的 HTTP API service 進行效能比較;此外也使用 Golang 實作的版本來測量不同語言之間的效能落差會到多少。
While implementing a file upload service with encryption and decryption, we encountered an issue where an increase in user requests significantly increased the lead time. The service is built using Python and the FastAPI framework. After ruling out network bandwidth and server performance issues, we suspect that the delay may be due to Python or the framework. Therefore, we decided to start with a benchmarking experiment in this area to identify where the bottleneck occurs.
用 Golang 刷 leetcode 題目時,如果不太清楚 Golang slice 與相關 function 的運作原理,很容易踩到坑,尤其是使用其他高階語言的開發者,剛轉換到 Golang 時會覺得為什麼同樣的程式邏輯,但是出來的結果卻不一樣。因此本篇簡單說明 Golang 最常使用到的 slice append function 運作原理,並且使用 objdump 來觀察記憶體操作狀況。
Slice internal
首先推薦先閱讀 Golang blog Go Slices: usage and internals,雖然距離文章發布的日期已久遠,但是 slice internal 結構還是可以參考。文中很重要的一個概念是:A slice is a descriptor of an array segment., 雖然我們使用 slice 方式很像 array ,但實際上 slice 是一個 descriptor struct ,其中會有一個 array pointer 指向真正存放數值的 array 中。
TinyGo - Go compiler for small places. TinyGo 自 2019 年正式公開以來,就逐漸受到關注,尤其是 Google 在 2019 年 10 years of Go 也有特別提到 Go 原本將目標放在網路或是雲端等應用程式,不過未來期望能夠更廣泛地應用在 microcontroller 上。在近期 TinyGo 開始支援 Raspberry Pi Pico,再加上 TinyGo 架構也逐漸成熟,因此就來試用看看。
Run applications on Raspberry Pi Pico
在分析 TinyGo 如何編譯適合跑在 Raspberry Pi Pico 的 Go program 之前,先來複習如何讓 user application 在 Raspberry Pi Pico (以下皆簡稱為 Pico) 上執行。在 Pico 進入 user application 的 entry point 之前,會有幾個前置流程,包含: