Go 性能優化
Golang 中使用 Slice + 索引 Map, 替代 Map 提高效能
- https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0567.Permutation-in-String/main.go
- https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0003.Longest-Substring-Without-Repeating-Characters/Longest-Substring-Without-Repeating-Characters.go
小筆記
- 互斥鎖的代價比原子操作大, 可通過原子檢查狀態提高性能concurrency/06_Sync_Package/01_Mutex
- 傳統鎖的效能比 channel 好
USE Method
Utilization (利用率)
For a set time interval, the percentage of time that the resource was busy servicing work. While busy, the resource may still able to accept more work; the degree to which it connot do so is identified by saturation
Saturation (飽和)
The degree to which the resource has extra work that it can't service, often waiting on a queue. Another term for this is pressure.
Errors
The count of error events.
Workflow

In software

In microservices

RED Method
Request rate
The number of service requests per second
Errors
The number of requests that failed
Duration
The time for requests to complete (consider distribution statistics such as per - centiles in additin to the average)
Go related optimizations

1. blocking (lock, udp fd, logger fd)
1.1 lock blocking

1.2 udp fd lock

1.3 logger fd lock

2. hight CPU usage (runtime, user app)
2.1 runtime
2.1.1 scheduler 調度器
- use g pool
2.1.2 garbage collection
- merge objects
- ptr -> values
- sync.Pool (最常見的手段)
- heap -> stack
- unsafe convert
2.2 user app
2.2.1 json
- jsoniter shared mem IPC
- binary based marshal
2.2.2 app logic
- case by case
- benchmark
3. hight Memory usage (heap allocation, massive g stacks)
3.1 heap allocation
- sync.Pool (可以減少 memory 使用)
- multi-level sync.Pool
- map -> slice. 因為map不太好清空, slice 只要 s=s[:0]
- offheap
- reuse everything!!

3.2 massive g(goroutine) stacks
