Skip to content

Commit 998a55a

Browse files
committed
Revert "cache: use malloc instead of calloc"
This reverts commit dd58b84. The pebble ycsb benchmarks show a regression around the time this commit was merged. A comparison of the CPU profiles before and after show ~5.32% time in _Cfunc_calloc before and ~12.10% time in _cgo_cmalloc after.
1 parent 3c70d62 commit 998a55a

File tree

3 files changed

+6
-54
lines changed

3 files changed

+6
-54
lines changed

internal/cache/value.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func Alloc(n int) *Value {
5959

6060
if valueEntryCanBeGoAllocated && valueEntryGoAllocated {
6161
// Note: if cgo is not enabled, manual.New will do a regular Go allocation.
62-
b := manual.NewUninitialized(manual.BlockCacheData, uintptr(n))
62+
b := manual.New(manual.BlockCacheData, uintptr(n))
6363
v := &Value{buf: b.Slice()}
6464
v.ref.init(1)
6565
// Note: this is a no-op if invariants and tracing are disabled or race is
@@ -77,10 +77,7 @@ func Alloc(n int) *Value {
7777
// When we're not performing leak detection, the lifetime of the returned
7878
// Value is exactly the lifetime of the backing buffer and we can manually
7979
// allocate both.
80-
b := manual.NewUninitialized(manual.BlockCacheData, ValueMetadataSize+uintptr(n))
81-
// We must clear the slice because Value contains pointers. See
82-
// manual.NewUninitialized.
83-
clear(b.Slice()[:ValueMetadataSize])
80+
b := manual.New(manual.BlockCacheData, ValueMetadataSize+uintptr(n))
8481
v := (*Value)(b.Data())
8582
v.buf = b.Slice()[ValueMetadataSize:]
8683
v.ref.init(1)

internal/manual/manual_cgo.go

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ var useGoAllocation = invariants.RaceEnabled && rand.Uint32()%2 == 0
3939
// runtime page allocator and allocate large chunks of memory using mmap or
4040
// similar.
4141

42-
// New allocates a slice of size n. The returned slice is from manually managed
43-
// memory and MUST be released by calling Free. Failure to do so will result in
44-
// a memory leak.
42+
// New allocates a slice of size n. The returned slice is from manually
43+
// managed memory and MUST be released by calling Free. Failure to do so will
44+
// result in a memory leak.
4545
func New(purpose Purpose, n uintptr) Buf {
4646
if n == 0 {
4747
return Buf{}
@@ -67,8 +67,6 @@ func New(purpose Purpose, n uintptr) Buf {
6767
// passing uninitialized C memory to Go code if the Go code is going to
6868
// store pointer values in it. Zero out the memory in C before passing it
6969
// to Go.
70-
//
71-
// See https://github.com/golang/go/issues/19928
7270
ptr := C.calloc(C.size_t(n), 1)
7371
if ptr == nil {
7472
// NB: throw is like panic, except it guarantees the process will be
@@ -79,36 +77,6 @@ func New(purpose Purpose, n uintptr) Buf {
7977
return Buf{data: ptr, n: n}
8078
}
8179

82-
// NewUninitialized allocates a slice of size n without zeroing it out. The
83-
// returned slice is from manually managed memory and MUST be released by
84-
// calling Free. Failure to do so will result in a memory leak (see
85-
// https://github.com/golang/go/issues/19928).
86-
//
87-
// If the caller does an unsafe cast from the slice to any type containing
88-
// pointers, the relevant part of the slice *must* be zeroed out.
89-
func NewUninitialized(purpose Purpose, n uintptr) Buf {
90-
if n == 0 {
91-
return Buf{}
92-
}
93-
recordAlloc(purpose, n)
94-
95-
// In race-enabled builds, we sometimes make allocations using Go to allow
96-
// the race detector to observe concurrent memory access to memory allocated
97-
// by this package. See the definition of useGoAllocation for more details.
98-
if invariants.RaceEnabled && useGoAllocation {
99-
b := make([]byte, n)
100-
return Buf{data: unsafe.Pointer(&b[0]), n: n}
101-
}
102-
ptr := C.malloc(C.size_t(n))
103-
if ptr == nil {
104-
// NB: throw is like panic, except it guarantees the process will be
105-
// terminated. The call below is exactly what the Go runtime invokes when
106-
// it cannot allocate memory.
107-
throw("out of memory")
108-
}
109-
return Buf{data: ptr, n: n}
110-
}
111-
11280
// Free frees the specified slice. It has to be exactly the slice that was
11381
// returned by New.
11482
func Free(purpose Purpose, b Buf) {

internal/manual/manual_nocgo.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ import (
1515
// Provides versions of New and Free when cgo is not available (e.g. cross
1616
// compilation).
1717

18-
// New allocates a slice of size n. The returned slice is from manually managed
19-
// memory and MUST be released by calling Free. Failure to do so will result in
20-
// a memory leak.
18+
// New allocates a slice of size n.
2119
func New(purpose Purpose, n uintptr) Buf {
2220
if n == 0 {
2321
return Buf{}
@@ -30,17 +28,6 @@ func New(purpose Purpose, n uintptr) Buf {
3028
}
3129
}
3230

33-
// NewUninitialized allocates a slice of size n without zeroing it out. The
34-
// returned slice is from manually managed memory and MUST be released by
35-
// calling Free. Failure to do so will result in a memory leak (see
36-
// https://github.com/golang/go/issues/19928).
37-
//
38-
// If the caller does an unsafe cast from the slice to any type containing
39-
// pointers, the relevant part of the slice *must* be zeroed out.
40-
func NewUninitialized(purpose Purpose, n uintptr) Buf {
41-
return New(purpose, n)
42-
}
43-
4431
// Free frees the specified slice. It has to be exactly the slice that was
4532
// returned by New.
4633
func Free(purpose Purpose, b Buf) {

0 commit comments

Comments
 (0)