In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to understand golang Escape Analysis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Background
Recently, I want to integrate protobuf variables with previously designed data objects and maintain them in memory to reduce memory request and GC performance loss.
Feature or bug,gogoproto Decoding confusion
Because gogoproto does not guarantee input and output consistency when unmarshal, the pointer variable as a result may not be consistent with the input byte slice (for example, there is no reset operation in unmarshal slice). We need to reset this pointer variable, and the reset implementation of the pb generated file is as follows.
Func (m * Data) Reset () {* m = Data {}}
I was confused when I saw Data {}. As far as I understand it, this step requires applying for memory. In this way, it is inevitable that we need to apply for memory when we throw a pb variable into memory, so this R & D requirement seems to be meaningless.
My first reaction was that this was gogoproto's problem, maybe the official goproto was not like this. However, after regeneration, it is found that the implementation of the reset method is no different. It's just that the official go proto will take the initiative to reset during unmarshal.
So, was it the wrong direction in the first place? Ah, bald.
Past dark willows and flowers in bloom lies another village
Not giving up, I began to read all kinds of documents, including various plug-ins for gogoproto, but unfortunately I couldn't find anything useful. Then I started looking at the official proto documents.
Then I found some clues.
In the daily use of protobuf, if we do not reuse the old variables, we will generally
Declare pointer variables, data: = & pb.Data {}
Decoding, proto.Unmarshal (bytes, data).
Obviously, the first step is to apply for memory. According to the source code of go proto, the reset operation of unmarshal will apply for memory again, will Google allow this kind of performance loss?
Really? I don't believe it.
Introduction to escape analysis
If you think too much, you might as well write a benchmark and try it. (be careful of some pits in microbenchmark)
Benchmarkpackage mainimport ("testing") type boy struct {name string age int} var b1 = & boy {} var b2 = & boy {} func Benchmark_1 (b * testing.B) {for I: = 0; I
< b.N; i++ { temp := &boy{} b1 = temp }}func Benchmark_2(b *testing.B) { for i := 0; i < b.N; i++ { temp := &boy{} *b1 = *temp }}func Benchmark_3(b *testing.B) { for i := 0; i < b.N; i++ { temp := &boy{} *b1 = *temp b2 = temp }} 结果如下。 goos: linuxgoarch: amd64pkg: bibleBenchmark_1-4 29142411 42.2 ns/op 32 B/op 1 allocs/opBenchmark_2-4 1000000000 0.711 ns/op 0 B/op 0 allocs/opBenchmark_3-4 28474614 39.5 ns/op 32 B/op 1 allocs/opPASSok bible 3.258s 结果是一目了然的,temp := &boy{} 确实没有重复申请内存。 编译报告 到此,只需要逐个分析就好。首先打开编译报告看一下。 go build -gcflags "-m -m" var b1 = &boy{}//go:noinlinefunc main() { temp := &boy{} // &boy literal escapes to heap: // flow: temp = &{storage for &boy literal}: // from &boy literal (spill) at ./main.go:12:10 // from temp := &boy literal (assign) at ./main.go:12:7 // flow: {heap} = temp: // from b1 = temp (assign) at ./main.go:13:5 // &boy literal escapes to heap b1 = temp} 新创建的 &boy{} 被全局变量引用,于是逃逸到堆上,成为动态变量,无法被重复利用。 var b1 = &boy{}//go:noinlinefunc main() { temp := &boy{} // &boy literal does not escape *b1 = *temp} *b1 = *temp 仅仅是赋值操作,新创建的 &boy{} 没有被引用,留在栈上,后续被重复利用。 汇编分析This is the end of "how to understand golang Escape Analysis". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.