Skip to content

Commit 016d1e1

Browse files
authored
optimize outBuf
1 parent 700c01a commit 016d1e1

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

std/crypto/tls/conn.go

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ func (a *NativeAllocator) Realloc(buf []byte, size int) []byte {
4646
if size <= cap(buf) {
4747
return buf[:size]
4848
}
49-
newBuf := make([]byte, size)
50-
copy(newBuf, buf)
51-
return newBuf
49+
return append(buf, make([]byte, size)...)
5250
}
5351

5452
// Append .
@@ -1093,22 +1091,20 @@ var outBufPool = sync.Pool{
10931091
// writeRecordLocked writes a TLS record with the given type and payload to the
10941092
// connection and updates the record layer state.
10951093
func (c *Conn) writeRecordLocked(typ recordType, data []byte) (int, error) {
1096-
// outBufPtr := outBufPool.Get().(*[]byte)
1097-
// outBuf := *outBufPtr
1098-
// defer func() {
1099-
// // You might be tempted to simplify this by just passing &outBuf to Put,
1100-
// // but that would make the local copy of the outBuf slice header escape
1101-
// // to the heap, causing an allocation. Instead, we keep around the
1102-
// // pointer to the slice header returned by Get, which is already on the
1103-
// // heap, and overwrite and return that.
1104-
// *outBufPtr = outBuf
1105-
// outBufPool.Put(outBufPtr)
1106-
// }()
1094+
outBufPtr := outBufPool.Get().(*[]byte)
1095+
outBuf := *outBufPtr
1096+
defer func() {
1097+
// You might be tempted to simplify this by just passing &outBuf to Put,
1098+
// but that would make the local copy of the outBuf slice header escape
1099+
// to the heap, causing an allocation. Instead, we keep around the
1100+
// pointer to the slice header returned by Get, which is already on the
1101+
// heap, and overwrite and return that.
1102+
*outBufPtr = outBuf
1103+
outBufPool.Put(outBufPtr)
1104+
}()
11071105

11081106
var n int
1109-
var outBuf = make([]byte, recordHeaderLen)
11101107
var maxPayload = c.maxPayloadSizeForWrite(typ)
1111-
// var outBuf = c.allocator.Malloc(recordHeaderLen + len(data))[0:0]
11121108
for len(data) > 0 {
11131109
m := len(data)
11141110
if m > maxPayload {
@@ -1135,13 +1131,9 @@ func (c *Conn) writeRecordLocked(typ recordType, data []byte) (int, error) {
11351131
var err error
11361132
outBuf, err = c.out.encrypt(c, outBuf, data[:m], c.config.rand())
11371133
if err != nil {
1138-
c.allocator.Free(outBuf)
11391134
return n, err
11401135
}
11411136
_, err = c.write(outBuf)
1142-
// if !c.buffering {
1143-
// c.allocator.Free(outBuf)
1144-
// }
11451137
if err != nil {
11461138
return n, err
11471139
}

0 commit comments

Comments
 (0)