-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/gc: optimize memset idiom #5373
Comments
I am just test some snappy libs and python(just wrapper for C++ implementation) is twice faster than pure go implementation. I used profiler and see that significant time spending on allocating empty array of 1<<14 size. So I am tried write memory pool and request "golang memset" led me here) |
Really, a way to zero any type, would be ideal. If for i := range b { b[i] = MyStruct{} } were recognized as a way to zero a slice of `MyStruct`, then s := MyStruct{...} s = MyStruct{} should probably also do no allocations (in line 2). But then, for a struct like type MyStruct struct { slice []int } there would suddenly be a no-loop way to zero an int-slice. I don't know what to make of this, other than that zeroing types seems like a natural thing to do, given Go's great idiom of zeroed types being (transitively) useful. |
memclr only: https://golang.org/cl/137880043 memset requires an efficient runtime memset implementation and a discussion of pattern length: repeating bytes? repeating words? As a data point, a pass through the stdlib yields three non-test uses of this memset idiom: strings/replace.go NewReplacer strconv/decimal.go digitZero httputil/dump.go neverending.Read All are slices or arrays of bytes. Owner changed to @josharian. Status changed to Started. |
josharian
added a commit
to josharian/go
that referenced
this issue
Jan 8, 2015
Recognize loops of the form for i := range a { a[i] = zero } in which the evaluation of a is free from side effects. Replace these loops with calls to memclr. This occurs in the stdlib in 18 places. The motivating example is clearing a byte slice: benchmark old ns/op new ns/op delta BenchmarkGoMemclr5 3.31 3.26 -1.51% BenchmarkGoMemclr16 13.7 3.28 -76.06% BenchmarkGoMemclr64 50.8 4.14 -91.85% BenchmarkGoMemclr256 157 6.02 -96.17% Update golang#5373. Change-Id: I99d3e6f5f268e8c6499b7e661df46403e5eb83e4
josharian
added a commit
that referenced
this issue
Jan 9, 2015
Recognize loops of the form for i := range a { a[i] = zero } in which the evaluation of a is free from side effects. Replace these loops with calls to memclr. This occurs in the stdlib in 18 places. The motivating example is clearing a byte slice: benchmark old ns/op new ns/op delta BenchmarkGoMemclr5 3.31 3.26 -1.51% BenchmarkGoMemclr16 13.7 3.28 -76.06% BenchmarkGoMemclr64 50.8 4.14 -91.85% BenchmarkGoMemclr256 157 6.02 -96.17% Update #5373. Change-Id: I99d3e6f5f268e8c6499b7e661df46403e5eb83e4 Reviewed-on: https://go-review.googlesource.com/2520 Reviewed-by: Keith Randall <khr@golang.org>
I consider this fixed. Optimizing non-zero cases isn't very interesting. We can open another bug if people feel strongly about doing more. |
juniorz
added a commit
to twstrike/AwESome
that referenced
this issue
Jul 2, 2015
It is optimized to use "memclr". See: golang/go#5373
el15066
referenced
this issue
in el15066/erigon
Feb 16, 2022
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
The text was updated successfully, but these errors were encountered: