Skip to content

Commit

Permalink
perf: use dirtmake to reduce memclr cost
Browse files Browse the repository at this point in the history
  • Loading branch information
jayantxie committed Apr 1, 2024
1 parent bb9c3f7 commit cba4296
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ go 1.15

require (
github.com/bytedance/gopkg v0.0.0-20220413063733-65bf48ffb3a7
golang.org/x/sys v0.0.0-20220110181412-a018aaa089fe
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10
)

replace github.com/bytedance/gopkg => github.com/jayantxie/gopkg v0.0.0-20240329103716-4974474d9ec7
13 changes: 9 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
github.com/bytedance/gopkg v0.0.0-20220413063733-65bf48ffb3a7 h1:PtwsQyQJGxf8iaPptPNaduEIu9BnrNms+pcRdHAxZaM=
github.com/bytedance/gopkg v0.0.0-20220413063733-65bf48ffb3a7/go.mod h1:2ZlV9BaUH4+NXIBF0aMdKKAnHTzqH+iMU4KUjAbL23Q=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jayantxie/gopkg v0.0.0-20240329103716-4974474d9ec7 h1:iV0g1cYrtYsFr+Vg8tG1MCPyq9fVDrdhhHjN7a7gsEo=
github.com/jayantxie/gopkg v0.0.0-20240329103716-4974474d9ec7/go.mod h1:FtQG3YbQG9L/91pbKSw787yBQPutC+457AvDW77fgUQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20220110181412-a018aaa089fe h1:W8vbETX/n8S6EmY0Pu4Ix7VvpsJUESTwl0oCK8MJOgk=
golang.org/x/sys v0.0.0-20220110181412-a018aaa089fe/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 h1:WIoqL4EROvwiPdUtaip4VcDdpZ4kha7wBWZrbVKCIZg=
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
Expand Down
11 changes: 6 additions & 5 deletions nocopy_linkbuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"sync/atomic"
"unsafe"

"github.com/bytedance/gopkg/lang/dirtmake"
"github.com/bytedance/gopkg/lang/mcache"
)

Expand Down Expand Up @@ -98,7 +99,7 @@ func (b *LinkBuffer) Next(n int) (p []byte, err error) {
p = malloc(n, n)
b.caches = append(b.caches, p)
} else {
p = make([]byte, n)
p = dirtmake.Bytes(n, n)
}
var l int
for ack := n; ack > 0; ack = ack - l {
Expand Down Expand Up @@ -135,7 +136,7 @@ func (b *LinkBuffer) Peek(n int) (p []byte, err error) {
p = malloc(n, n)
b.caches = append(b.caches, p)
} else {
p = make([]byte, n)
p = dirtmake.Bytes(n, n)
}
var node = b.read
var l int
Expand Down Expand Up @@ -224,7 +225,7 @@ func (b *LinkBuffer) readBinary(n int) (p []byte) {
b.recalLen(-n) // re-cal length

// single node
p = make([]byte, n)
p = dirtmake.Bytes(n, n)
if b.isSingleNode(n) {
copy(p, b.read.Next(n))
return p
Expand Down Expand Up @@ -546,7 +547,7 @@ func (b *LinkBuffer) Bytes() []byte {
return node.buf[node.off:]
}
n := 0
p := make([]byte, b.Len())
p := dirtmake.Bytes(b.Len(), b.Len())
for ; node != flush; node = node.next {
if node.Len() > 0 {
n += copy(p[n:], node.buf[node.off:])
Expand Down Expand Up @@ -834,7 +835,7 @@ const mallocMax = block8k * block1k
// malloc limits the cap of the buffer from mcache.
func malloc(size, capacity int) []byte {
if capacity > mallocMax {
return make([]byte, size, capacity)
return dirtmake.Bytes(size, capacity)
}
return mcache.Malloc(size, capacity)
}
Expand Down
11 changes: 6 additions & 5 deletions nocopy_linkbuffer_race.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"sync/atomic"
"unsafe"

"github.com/bytedance/gopkg/lang/dirtmake"
"github.com/bytedance/gopkg/lang/mcache"
)

Expand Down Expand Up @@ -101,7 +102,7 @@ func (b *LinkBuffer) Next(n int) (p []byte, err error) {
p = malloc(n, n)
b.caches = append(b.caches, p)
} else {
p = make([]byte, n)
p = dirtmake.Bytes(n, n)
}
var l int
for ack := n; ack > 0; ack = ack - l {
Expand Down Expand Up @@ -140,7 +141,7 @@ func (b *LinkBuffer) Peek(n int) (p []byte, err error) {
p = malloc(n, n)
b.caches = append(b.caches, p)
} else {
p = make([]byte, n)
p = dirtmake.Bytes(n, n)
}
var node = b.read
var l int
Expand Down Expand Up @@ -252,7 +253,7 @@ func (b *LinkBuffer) readBinary(n int) (p []byte) {
b.recalLen(-n) // re-cal length

// single node
p = make([]byte, n)
p = dirtmake.Bytes(n, n)
if b.isSingleNode(n) {
copy(p, b.read.Next(n))
return p
Expand Down Expand Up @@ -588,7 +589,7 @@ func (b *LinkBuffer) Bytes() []byte {
return node.buf[node.off:]
}
n := 0
p := make([]byte, b.Len())
p := dirtmake.Bytes(b.Len(), b.Len())
for ; node != flush; node = node.next {
if node.Len() > 0 {
n += copy(p[n:], node.buf[node.off:])
Expand Down Expand Up @@ -883,7 +884,7 @@ const mallocMax = block8k * block1k
// malloc limits the cap of the buffer from mcache.
func malloc(size, capacity int) []byte {
if capacity > mallocMax {
return make([]byte, size, capacity)
return dirtmake.Bytes(size, capacity)
}
return mcache.Malloc(size, capacity)
}
Expand Down

0 comments on commit cba4296

Please sign in to comment.