Skip to content

Commit

Permalink
feat: optimize make bytes to reduce memclr cost
Browse files Browse the repository at this point in the history
  • Loading branch information
jayantxie committed Mar 27, 2024
1 parent e9c77ec commit 6274f8a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lang/dirtmake/bytes.go
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]
}
19 changes: 19 additions & 0 deletions lang/dirtmake/bytes_test.go
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)
}
}

0 comments on commit 6274f8a

Please sign in to comment.