偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Go 下一步計(jì)劃,新標(biāo)準(zhǔn)庫(kù) sync/v2!

開(kāi)發(fā) 前端
通過(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)型。

大家好,我是煎魚(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#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 包提供了 MapPool 類(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.Mapsync.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

責(zé)任編輯:武曉燕 來(lái)源: 腦子進(jìn)煎魚(yú)了
相關(guān)推薦

2013-05-07 09:45:53

微軟Bing

2011-06-15 09:42:50

FoursquareLBS

2018-05-18 10:18:20

云計(jì)算云廠商物聯(lián)網(wǎng)

2017-01-17 15:57:47

大數(shù)據(jù)特朗普數(shù)據(jù)湖泊

2013-08-12 14:42:20

UI設(shè)計(jì)UX設(shè)計(jì)設(shè)計(jì)

2022-09-27 09:31:27

谷歌Angular

2019-05-21 05:09:23

物聯(lián)網(wǎng)數(shù)據(jù)IOT

2017-01-18 12:16:37

OpenFlowSDNONF

2023-11-13 11:10:16

2024-10-15 15:42:50

2018-09-13 12:51:52

云計(jì)算私有云公共云

2015-05-19 11:31:11

LTELTE網(wǎng)絡(luò)

2013-11-25 13:30:47

微信開(kāi)發(fā)

2009-11-04 15:52:16

Windows Emb服務(wù)導(dǎo)向設(shè)備

2011-05-05 10:25:35

Windows Ser

2023-11-23 15:12:17

2017-01-03 08:26:11

大數(shù)據(jù)AIML

2013-10-15 09:36:23

SUSE CloudOpenStack

2015-10-23 14:20:43

2013-11-08 16:19:17

HTML5
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)