我想回收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 對象的引用。這樣做會破壞很多代碼,因為這樣的庫無法知道何時可以安全地重用。
因此,只要您確保自己的代碼在將對象放入池中后不使用它們,就應該很好。
- 1 回答
- 0 關注
- 176 瀏覽
添加回答
舉報
0/150
提交
取消