Go 下一步計劃,新標準庫 sync/v2!
大家好,我是煎魚。
之前 Go 核心團隊乘機推廣了標準庫 v2 的更新計劃,想著把一些老舊的標準庫給干掉。在上一步已經(jīng)把 math/rand/v2
給做了,官方也認為非常成功。
后續(xù)將開始計劃新標準庫 sync/v2
的更新和發(fā)布。今天文章主要分享此提案。
#go/issues/71076
起源
本次新標準庫 sync/v2
的一個改造重點,來自于一個一攬子提案《spec: add generic programming using type parameters[1]》:
圖片
核心目的就是:“建議在 Go 語言中添加對類型參數(shù)的支持。這將改變 Go 語言,使其支持一種通用編程形式?!?/p>
簡單來講,就是用逐步用泛型重構(gòu)一切適用的標準庫。
新提案:sync/v2
背景
當前 sync
包提供了 Map
和 Pool
類型。這些類型是在 Go 支持泛型之前設(shè)計的,其操作的值類型均為 any
。
圖片
導致存在兩個核心問題:
- 類型不安全:因設(shè)計于泛型前,依賴
any
類型,無法保證編譯時類型校驗,違背 Go 的類型安全原則; - 性能損耗:非指針值轉(zhuǎn)換為
any
需額外內(nèi)存分配,導致存儲字符串鍵或切片值時效率低下。
解決思路
通過將 sync.Map
和 sync.Pool
改造為泛型類型,可以輕松解決和避免這些問題。
但是由于 Go1 兼容性保障。也就是兼容性問題,考慮 Go 核心團隊無法直接修改現(xiàn)有 sync v1 包中的 Map 和 Pool 類型。
解決思路,將主要采取以下兩點方法:
- 需要通過泛型重構(gòu)為強類型設(shè)計。
- 新增
sync/v2
包引入泛型版本。例如:Map[K,V]/Pool[T]
,避免命名混亂(例如:PoolOf
的歧義),并支持 v1 到 v2 到平滑遷移。
這樣后續(xù)大家可以直接通過類似 goimports
工具,直接將 v1 版本遷移至 sync/v2
,不需要手動處理導入路徑。
同時可以保留原 v1 包兼容性,可以徹底解決后續(xù)類型與性能問題,符合 Go 語言長期演進方向和規(guī)范。
具體改造
在 sync/v2
包中,以下類型和函數(shù)將與當前 sync
v1 包保持一致(不變):
func OnceFunc(f func()) func()
func OnceValue[T any](f func( "T any") T) func() T
func OnceValues[T1, T2 any](f func( "T1, T2 any") (T1, T2)) func() (T1, T2)
type Cond struct{ ... }
func NewCond(l Locker) *Cond
type Locker interface{ ... }
type Mutex struct{ ... }
type Once struct{ ... }
type RWMutex struct{ ... }
type WaitGroup struct{ ... }
改造點之一:現(xiàn)有的 Map
類型將被需要兩個類型參數(shù)的新泛型類型取代。需注意,原 Range
方法將變更為返回迭代器的 All
方法。
如下代碼:
// Map is like a Go map[K]V but is safe for concurrent use
// by multiple goroutines without additional locking or coordination.
// ...and so forth
type Map[K comparable, V any] struct { ... }
// Load returns the value stored in the map for a key, or the zero value if no
// value is present.
// The ok result indicates whether value was found in the map.
func (m *Map[K, V]) Load(key K) (value V, ok bool)
// Store sets the value for a key.
func (m *Map[K, V]) Store(key K, value V)
// LoadOrStore returns the existing value for the key if present.
// Otherwise, it stores and returns the given value.
// The loaded result is true if the value was loaded, false if stored.
func (m *Map[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)
// LoadAndDelete deletes the value for a key, returning the previous value if any.
// The loaded result reports whether the key was present.
func (m *Map[K, V]) LoadAndDelete(key K) (value V, loaded bool)
// Delete deletes the value for a key.
func (m *Map[K, V]) Delete(key K)
// Swap swaps the value for a key and returns the previous value if any.
// The loaded result reports whether the key was present.
func (m *Map[K, V]) Swap(key K, value V) (previous V, loaded bool)
// CompareAndDelete deletes the entry for key if its value is equal to old.
// This panics if V is not a comparable type.
//
// If there is no current value for key in the map, CompareAndDelete
// returns false.
func (m *Map[K, V]) CompareAndDelete(key K, old V) (deleted bool)
// CompareAndSwap swaps the old and new values for key
// if the value stored in the map is equal to old.
// This panics if V is not a comparable type.
func (m *Map[K, V]) CompareAndSwap(key K, old, new V) (swapped bool)
// Clear deletes all the entries, resulting in an empty Map.
func (m *Map[K, V]) Clear()
// All returns an iterator over the keys and values in the map.
// ... and so forth
func (m *Map[K, V]) All() iter.Seq2[K, V]
// TODO: Consider Keys and Values methods that return iterators, like maps.Keys and maps.Values.
改造點之二:現(xiàn)有的 Pool 類型將被一個需要類型參數(shù)的新泛型類型取代。
同時新版 Pool 將不再暴露公開的 New
字段,改為通過 NewPool
函數(shù)創(chuàng)建實例。該函數(shù)接受一個生成新值的函數(shù)參數(shù),用于替代原 New
字段的初始化行為。
如下代碼:
// A Pool is a set of temporary objects of type T that may be individually saved and retrieved.
// ...and so forth
type Pool[T any] struct {
...
}
// NewPool returns a new pool. If the newf argument is not nil, then when the pool is empty,
// newf is called to fetch a new value. This is useful when the values in the pool should be initialized.
func NewPool[T any] (newf func() T) *Pool[T]
// Put adds x to the pool.
func (p *Pool[T]) Put(x T)
// Get selects an arbitrary item from the Pool, removes it from the
// Pool, and returns it to the caller.
// ...and so forth
//
// If Get does not have a value to return, and p was created with a call to [NewPool] with a non-nil argument,
// Get returns the result of calling the function passed to [NewPool].
func (p *Pool[T]) Get() T
總結(jié)
之前標準庫 math/rand/v2
在 Go 核心團隊看來,已經(jīng)取得了不錯的成果。泛型在 Go1.18 也輸出了(雖然還不完善),Go1 向前兼容性和向后兼容性保障的方式也確立了。
接下來想必 Go 就會每年更新幾個 v2 標準庫。這次 sync/v2
也是一次不錯的改造,解決了不少原有的問題。大家可以小小期待一下。
參考資料
[1]spec: add generic programming using type parameters: https://github.com/golang/go/issues/43651