亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

golang中如何安全回收protobuf對象

golang中如何安全回收protobuf對象

Go
慕娘9325324 2022-10-24 15:20:19
我想回收protobuf的message對象來減少運行時的GC消耗,但不確定是否安全。測試示例代碼如下:test.protomessage Hello{  uint32  id   = 1;}測試.pb.gotype Hello struct {    state         protoimpl.MessageState    sizeCache     protoimpl.SizeCache    unknownFields protoimpl.UnknownFields    ID  uint32 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`}func (x *Hello) Reset() {    *x = Hello{}    if protoimpl.UnsafeEnabled {        mi := &file_proto_login_api_login_proto_msgTypes[0]        ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))        ms.StoreMessageInfo(mi)    }}// other codesmain.gofunc main() {    // Disable GC to test re-acquire the same data    gc := debug.SetGCPercent(-1)    // As a protobuf object pool    cache := sync.Pool{New: func() interface{} { return &test.Hello{} }}        // Take out an object and use it    m1 := cache.Get().(*test.Hello)    m1.ID = 999    fmt.Println(&m1.ID) // print 999    // Empty the data and put it back into the object pool    m1.Reset()    cache.Put(m1)    // Take out an object again and use it    m2 := cache.Get().(*test.Hello)    fmt.Println(&m2.ID) // print 0    debug.SetGCPercent(gc)}
查看完整描述

1 回答

?
當年話下

TA貢獻1890條經驗 獲得超9個贊

您顯示的代碼是安全的。當對象的引用在放入池后被持有時,這樣的池化變得“不安全”。您冒著競爭條件或奇怪錯誤的風險。因此,它還取決于使用您的對象的代碼。

據我所知,協議緩沖區庫和 gRPC 庫不保留對 protobuf 對象的引用。這樣做會破壞很多代碼,因為這樣的庫無法知道何時可以安全地重用。

因此,只要您確保自己的代碼在將對象放入池中后不使用它們,就應該很好。


查看完整回答
反對 回復 2022-10-24
  • 1 回答
  • 0 關注
  • 176 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號