-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: optimize make bytes to reduce memclr cost
- Loading branch information
Showing
2 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package dirtmake | ||
|
||
import _ "unsafe" | ||
|
||
//go:linkname rawstring runtime.rawstring | ||
func rawstring(size int) (s string, b []byte) | ||
|
||
// Bytes allocates a byte slice but does not clean up the memory it references. | ||
// NOTE: MUST set any byte element before it's read. | ||
func Bytes(len, cap int) (b []byte) { | ||
_, b = rawstring(cap) | ||
return b[:len] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package dirtmake | ||
|
||
import "testing" | ||
|
||
var data []byte | ||
|
||
const testsize = 1024 | ||
|
||
func BenchmarkFastMake(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
data = Bytes(testsize, testsize) | ||
} | ||
} | ||
|
||
func BenchmarkOriginMake(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
data = make([]byte, testsize) | ||
} | ||
} |