Go 下一步計(jì)劃,新標(biāo)準(zhǔn)庫(kù) sync/v2!
大家好,我是煎魚(yú)。
之前 Go 核心團(tuán)隊(duì)乘機(jī)推廣了標(biāo)準(zhǔn)庫(kù) v2 的更新計(jì)劃,想著把一些老舊的標(biāo)準(zhǔn)庫(kù)給干掉。在上一步已經(jīng)把 math/rand/v2
給做了,官方也認(rèn)為非常成功。
后續(xù)將開(kāi)始計(jì)劃新標(biāo)準(zhǔn)庫(kù) sync/v2
的更新和發(fā)布。今天文章主要分享此提案。
#go/issues/71076
起源
本次新標(biāo)準(zhǔn)庫(kù) sync/v2
的一個(gè)改造重點(diǎn),來(lái)自于一個(gè)一攬子提案《spec: add generic programming using type parameters[1]》:
圖片
核心目的就是:“建議在 Go 語(yǔ)言中添加對(duì)類(lèi)型參數(shù)的支持。這將改變 Go 語(yǔ)言,使其支持一種通用編程形式?!?/p>
簡(jiǎn)單來(lái)講,就是用逐步用泛型重構(gòu)一切適用的標(biāo)準(zhǔn)庫(kù)。
新提案:sync/v2
背景
當(dāng)前 sync
包提供了 Map
和 Pool
類(lèi)型。這些類(lèi)型是在 Go 支持泛型之前設(shè)計(jì)的,其操作的值類(lèi)型均為 any
。
圖片
導(dǎo)致存在兩個(gè)核心問(wèn)題:
- 類(lèi)型不安全:因設(shè)計(jì)于泛型前,依賴(lài)
any
類(lèi)型,無(wú)法保證編譯時(shí)類(lèi)型校驗(yàn),違背 Go 的類(lèi)型安全原則; - 性能損耗:非指針值轉(zhuǎn)換為
any
需額外內(nèi)存分配,導(dǎo)致存儲(chǔ)字符串鍵或切片值時(shí)效率低下。
解決思路
通過(guò)將 sync.Map
和 sync.Pool
改造為泛型類(lèi)型,可以輕松解決和避免這些問(wèn)題。
但是由于 Go1 兼容性保障。也就是兼容性問(wèn)題,考慮 Go 核心團(tuán)隊(duì)無(wú)法直接修改現(xiàn)有 sync v1 包中的 Map 和 Pool 類(lèi)型。
解決思路,將主要采取以下兩點(diǎn)方法:
- 需要通過(guò)泛型重構(gòu)為強(qiáng)類(lèi)型設(shè)計(jì)。
- 新增
sync/v2
包引入泛型版本。例如:Map[K,V]/Pool[T]
,避免命名混亂(例如:PoolOf
的歧義),并支持 v1 到 v2 到平滑遷移。
這樣后續(xù)大家可以直接通過(guò)類(lèi)似 goimports
工具,直接將 v1 版本遷移至 sync/v2
,不需要手動(dòng)處理導(dǎo)入路徑。
同時(shí)可以保留原 v1 包兼容性,可以徹底解決后續(xù)類(lèi)型與性能問(wèn)題,符合 Go 語(yǔ)言長(zhǎng)期演進(jìn)方向和規(guī)范。
具體改造
在 sync/v2
包中,以下類(lèi)型和函數(shù)將與當(dāng)前 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{ ... }
改造點(diǎn)之一:現(xiàn)有的 Map
類(lèi)型將被需要兩個(gè)類(lèi)型參數(shù)的新泛型類(lèi)型取代。需注意,原 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.
改造點(diǎn)之二:現(xiàn)有的 Pool 類(lèi)型將被一個(gè)需要類(lèi)型參數(shù)的新泛型類(lèi)型取代。
同時(shí)新版 Pool 將不再暴露公開(kāi)的 New
字段,改為通過(guò) NewPool
函數(shù)創(chuàng)建實(shí)例。該函數(shù)接受一個(gè)生成新值的函數(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é)
之前標(biāo)準(zhǔn)庫(kù) math/rand/v2
在 Go 核心團(tuán)隊(duì)看來(lái),已經(jīng)取得了不錯(cuò)的成果。泛型在 Go1.18 也輸出了(雖然還不完善),Go1 向前兼容性和向后兼容性保障的方式也確立了。
接下來(lái)想必 Go 就會(huì)每年更新幾個(gè) v2 標(biāo)準(zhǔn)庫(kù)。這次 sync/v2
也是一次不錯(cuò)的改造,解決了不少原有的問(wèn)題。大家可以小小期待一下。
參考資料
[1]spec: add generic programming using type parameters: https://github.com/golang/go/issues/43651