Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: use dirtmake to reduce memclr cost #321

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/cloudwego/netpoll
go 1.15

require (
github.com/bytedance/gopkg v0.0.0-20220413063733-65bf48ffb3a7
golang.org/x/sys v0.0.0-20220110181412-a018aaa089fe
github.com/bytedance/gopkg v0.0.0-20240507064146-197ded923ae3
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10
)
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/bytedance/gopkg v0.0.0-20240507064146-197ded923ae3 h1:ZKUHguI38SRQJkq7hhmwn8lAv3xM6B5qkj1IneS15YY=
github.com/bytedance/gopkg v0.0.0-20240507064146-197ded923ae3/go.mod h1:FtQG3YbQG9L/91pbKSw787yBQPutC+457AvDW77fgUQ=
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/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
3 changes: 2 additions & 1 deletion nocopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"reflect"
"unsafe"

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

Expand Down Expand Up @@ -285,7 +286,7 @@ func unsafeStringToSlice(s string) (b []byte) {
// 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
12 changes: 7 additions & 5 deletions nocopy_linkbuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"fmt"
"sync"
"sync/atomic"

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

// BinaryInplaceThreshold marks the minimum value of the nocopy slice length,
Expand Down Expand Up @@ -91,7 +93,7 @@ func (b *UnsafeLinkBuffer) 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 @@ -128,7 +130,7 @@ func (b *UnsafeLinkBuffer) 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 @@ -232,11 +234,11 @@ func (b *UnsafeLinkBuffer) readBinary(n int) (p []byte) {
}
}
// if the underlying buffer too large, we shouldn't use no-copy mode
p = make([]byte, n)
p = dirtmake.Bytes(n, n)
copy(p, b.read.Next(n))
return p
}
p = make([]byte, n)
p = dirtmake.Bytes(n, n)
// multiple nodes
var pIdx int
var l int
Expand Down Expand Up @@ -560,7 +562,7 @@ func (b *UnsafeLinkBuffer) 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
3 changes: 3 additions & 0 deletions nocopy_linkbuffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -668,9 +668,12 @@ func TestLinkBufferIndexByte(t *testing.T) {
trigger := make(chan struct{}, 16)

lb := NewLinkBuffer()
empty := make([]byte, 1002)
go func() {
for i := 0; i < loopSize; i++ {
buf, err := lb.Malloc(1002)
// need clear buffer
copy(buf, empty)
buf[500] = '\n'
buf[1001] = '\n'
MustNil(t, err)
Expand Down
Loading