Skip to content

Commit d66c8cf

Browse files
authored
Merge pull request #33 from yuuki/fix/sync-pool-allocation-optimization
fix: optimize sync.Pool usage to avoid allocation during Put() calls
2 parents 211f21e + c51251b commit d66c8cf

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

server.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ const (
2020
)
2121

2222
var serveMsgBuf = sync.Pool{
23-
New: func() any { return make([]byte, TCPBufferSize) },
23+
New: func() any {
24+
buf := make([]byte, TCPBufferSize)
25+
return &buf
26+
},
2427
}
2528

2629
var bufUDPPool = sync.Pool{
@@ -136,8 +139,8 @@ func (s *Server) serveTCP(ctx context.Context) error {
136139
func handleConnection(conn net.Conn) error {
137140
defer conn.Close()
138141

139-
buf := serveMsgBuf.Get().([]byte)
140-
defer serveMsgBuf.Put(buf)
142+
buf := *serveMsgBuf.Get().(*[]byte)
143+
defer serveMsgBuf.Put(&buf)
141144

142145
for {
143146
n, err := conn.Read(buf)

0 commit comments

Comments
 (0)