Skip to content

Commit

Permalink
malloc: add mimalloc allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
reusee committed Jan 17, 2025
1 parent e9a5458 commit 3757c29
Show file tree
Hide file tree
Showing 43 changed files with 17,117 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pkg/common/malloc/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,26 @@ func newDefault(delta *Config) (allocator Allocator) {
},
)

case "mimalloc":
// mimalloc allocator
return NewShardedAllocator(
runtime.GOMAXPROCS(0),
func() Allocator {
var ret Allocator
ret = NewClassAllocator(NewFixedSizeMimallocAllocator)
if config.EnableMetrics != nil && *config.EnableMetrics {
ret = NewMetricsAllocator(
ret,
metric.MallocCounter.WithLabelValues("allocate"),
metric.MallocGauge.WithLabelValues("inuse"),
metric.MallocCounter.WithLabelValues("allocate-objects"),
metric.MallocGauge.WithLabelValues("inuse-objects"),
)
}
return ret
},
)

default:
panic("unknown allocator: " + *config.Allocator)
}
Expand Down
110 changes: 110 additions & 0 deletions pkg/common/malloc/fixed_size_mimalloc_allocator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright 2024 Matrix Origin
//
// 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 malloc

/*
#include "./mimalloc/static.c"
#cgo CFLAGS: -I./mimalloc -Wstringop-overflow=0
*/
import "C"

import (
"unsafe"
)

func init() {
// mimalloc tunings
C.mi_option_set(C.mi_option_allow_large_os_pages, 1) // allow large/huge pages
C.mi_option_set(C.mi_option_purge_delay, 0) // no purge delay
}

type fixedSizeMimallocAllocator struct {
size C.size_t
// buffer1 buffers objects
buffer1 chan unsafe.Pointer

deallocatorPool *ClosureDeallocatorPool[fixedSizeMimallocDeallocatorArgs, *fixedSizeMimallocDeallocatorArgs]
}

type fixedSizeMimallocDeallocatorArgs struct {
length C.size_t
ptr unsafe.Pointer
}

func (f fixedSizeMimallocDeallocatorArgs) As(trait Trait) bool {
return false
}

func NewFixedSizeMimallocAllocator(
size uint64,
) (ret *fixedSizeMimallocAllocator) {

// if size is larger than smallClassCap, num1 will be zero, buffer1 will be empty
num1 := smallClassCap / size
if num1 > maxBuffer1Cap {
// don't buffer too much, since chans with larger buffer consume more memory
num1 = maxBuffer1Cap
}

ret = &fixedSizeMimallocAllocator{
size: C.size_t(size),
buffer1: make(chan unsafe.Pointer, num1),

deallocatorPool: NewClosureDeallocatorPool(
func(hints Hints, args *fixedSizeMimallocDeallocatorArgs) {

select {

case ret.buffer1 <- args.ptr:
// buffer in buffer1

default:
C.mi_free(args.ptr)

}

},
),
}

return ret
}

var _ FixedSizeAllocator = new(fixedSizeMimallocAllocator)

func (f *fixedSizeMimallocAllocator) Allocate(hints Hints, clearSize uint64) (slice []byte, dec Deallocator, err error) {

select {

case ptr := <-f.buffer1:
// from buffer1
slice = unsafe.Slice((*byte)(ptr), f.size)

default:
// allocate new
ptr := C.mi_malloc(f.size)
slice = unsafe.Slice((*byte)(ptr), f.size)

}

if hints&NoClear == 0 {
clear(slice[:clearSize])
}

return slice, f.deallocatorPool.Get(fixedSizeMimallocDeallocatorArgs{
ptr: unsafe.Pointer(unsafe.SliceData(slice)),
length: f.size,
}), nil
}
55 changes: 55 additions & 0 deletions pkg/common/malloc/fixed_size_mimalloc_allocator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2024 Matrix Origin
//
// 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 malloc

import (
"runtime"
"testing"
)

func TestMimallocAllocator(t *testing.T) {
testAllocator(t, func() Allocator {
return NewShardedAllocator(
runtime.GOMAXPROCS(0),
func() Allocator {
return NewClassAllocator(NewFixedSizeMimallocAllocator)
},
)
})
}

func BenchmarkMimallocAllocator(b *testing.B) {
for _, n := range benchNs {
benchmarkAllocator(b, func() Allocator {
return NewShardedAllocator(
runtime.GOMAXPROCS(0),
func() Allocator {
return NewClassAllocator(NewFixedSizeMimallocAllocator)
},
)
}, n)
}
}

func FuzzMimallocAllocator(f *testing.F) {
fuzzAllocator(f, func() Allocator {
return NewShardedAllocator(
runtime.GOMAXPROCS(0),
func() Allocator {
return NewClassAllocator(NewFixedSizeMimallocAllocator)
},
)
})
}
Loading

0 comments on commit 3757c29

Please sign in to comment.