Skip to content

Commit

Permalink
perf(mcache): no alloc (#227)
Browse files Browse the repository at this point in the history
goos: darwin
goarch: arm64
pkg: github.com/bytedance/gopkg/lang/mcache
                      │   ./old.out   │              ./new.out              │
                      │    sec/op     │    sec/op     vs base               │
MCache4096-12           26.270n ±  2%   9.533n ±  1%  -63.71% (p=0.002 n=6)
MCache10M-12            25.885n ±  2%   9.486n ±  0%  -63.36% (p=0.002 n=6)
MCache4096Parallel-12    105.3µ ± 17%   113.4µ ±  7%        ~ (p=0.065 n=6)
MCache10MParallel-12     123.7µ ±  5%   120.8µ ± 17%        ~ (p=0.310 n=6)
geomean                  1.725µ         1.055µ        -38.83%

                      │    ./old.out    │               ./new.out                │
                      │      B/op       │    B/op     vs base                    │
MCache4096-12               24.00 ±  0%    0.00 ± 0%  -100.00% (p=0.002 n=6)
MCache10M-12                27.50 ±  5%    0.00 ± 0%  -100.00% (p=0.002 n=6)
MCache4096Parallel-12     234.7Ki ± 23%   0.0Ki ± 0%  -100.00% (p=0.002 n=6)
MCache10MParallel-12    804424.50 ±  3%   74.50 ±  ?   -99.99% (p=0.002 n=6)
geomean                   3.282Ki                     ?                      ¹ ²
¹ summaries must be >0 to compute geomean
² ratios must be >0 to compute geomean

                      │  ./old.out   │               ./new.out                │
                      │  allocs/op   │ allocs/op   vs base                    │
MCache4096-12            1.000 ±  0%   0.000 ± 0%  -100.00% (p=0.002 n=6)
MCache10M-12             1.000 ±  0%   0.000 ± 0%  -100.00% (p=0.002 n=6)
MCache4096Parallel-12   10.00k ± 23%   0.00k ± 0%  -100.00% (p=0.002 n=6)
MCache10MParallel-12    32.48k ±  4%   0.00k ± 0%  -100.00% (p=0.002 n=6)
geomean                  134.3                     ?                      ¹ ²
¹ summaries must be >0 to compute geomean
² ratios must be >0 to compute geomean
  • Loading branch information
xiaost committed Aug 22, 2024
1 parent 3042f73 commit ff3e2ed
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
24 changes: 19 additions & 5 deletions lang/mcache/mcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package mcache

import (
"sync"
"unsafe"

"github.com/bytedance/gopkg/lang/dirtmake"
)
Expand All @@ -25,12 +26,19 @@ const maxSize = 46
// index contains []byte which cap is 1<<index
var caches [maxSize]sync.Pool

type bytesHeader struct {
Data *byte
Len int
Cap int
}

func init() {
for i := 0; i < maxSize; i++ {
size := 1 << i
caches[i].New = func() interface{} {
buf := dirtmake.Bytes(0, size)
return buf
h := (*bytesHeader)(unsafe.Pointer(&buf))
return h.Data
}
}
}
Expand All @@ -57,8 +65,14 @@ func Malloc(size int, capacity ...int) []byte {
if len(capacity) > 0 && capacity[0] > size {
c = capacity[0]
}
var ret = caches[calcIndex(c)].Get().([]byte)
ret = ret[:size]

i := calcIndex(c)

ret := []byte{}
h := (*bytesHeader)(unsafe.Pointer(&ret))
h.Len = size
h.Cap = 1 << i
h.Data = caches[i].Get().(*byte)
return ret
}

Expand All @@ -68,6 +82,6 @@ func Free(buf []byte) {
if !isPowerOfTwo(size) {
return
}
buf = buf[:0]
caches[bsr(size)].Put(buf)
h := (*bytesHeader)(unsafe.Pointer(&buf))
caches[bsr(size)].Put(h.Data)
}
2 changes: 1 addition & 1 deletion lang/mcache/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ func bsr(x int) int {
}

func isPowerOfTwo(x int) bool {
return (x & (-x)) == x
return (x != 0) && ((x & (-x)) == x)
}

0 comments on commit ff3e2ed

Please sign in to comment.