-
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 (#209)
- Loading branch information
Showing
4 changed files
with
109 additions
and
2 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,43 @@ | ||
// Copyright 2024 ByteDance Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package dirtmake | ||
|
||
import ( | ||
"unsafe" | ||
) | ||
|
||
type slice struct { | ||
data unsafe.Pointer | ||
len int | ||
cap int | ||
} | ||
|
||
//go:linkname mallocgc runtime.mallocgc | ||
func mallocgc(size uintptr, typ unsafe.Pointer, needzero bool) unsafe.Pointer | ||
|
||
// Bytes allocates a byte slice but does not clean up the memory it references. | ||
// Throw a fatal error instead of panic if cap is greater than runtime.maxAlloc. | ||
// NOTE: MUST set any byte element before it's read. | ||
func Bytes(len, cap int) (b []byte) { | ||
if len < 0 || len > cap { | ||
panic("dirtmake.Bytes: len out of range") | ||
} | ||
p := mallocgc(uintptr(cap), nil, false) | ||
sh := (*slice)(unsafe.Pointer(&b)) | ||
sh.data = p | ||
sh.len = len | ||
sh.cap = cap | ||
return | ||
} |
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,61 @@ | ||
// Copyright 2024 ByteDance Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package dirtmake | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
var data []byte | ||
|
||
const block1kb = 1024 | ||
|
||
func BenchmarkParallelDirtBytes(b *testing.B) { | ||
src := make([]byte, block1kb) | ||
for i := range src { | ||
src[i] = byte(i) | ||
} | ||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
bs := Bytes(block1kb, block1kb) | ||
copy(bs, src) | ||
if !bytes.Equal(bs, src) { | ||
b.Fatalf("bytes not equal") | ||
} | ||
} | ||
}) | ||
} | ||
|
||
func BenchmarkDirtBytes(b *testing.B) { | ||
for size := block1kb; size < block1kb*20; size += block1kb * 2 { | ||
b.Run(fmt.Sprintf("size=%dkb", size/block1kb), func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
data = Bytes(size, size) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func BenchmarkOriginBytes(b *testing.B) { | ||
for size := block1kb; size < block1kb*20; size += block1kb * 2 { | ||
b.Run(fmt.Sprintf("size=%dkb", size/block1kb), func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
data = make([]byte, size) | ||
} | ||
}) | ||
} | ||
} |
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
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