Просмотр исходного кода

Cleanup pools from tlstypes

tags/v1.0.6^2^2
9seconds 5 лет назад
Родитель
Сommit
95b16cd805
2 измененных файлов: 5 добавлений и 29 удалений
  1. 5
    6
      tlstypes/handshake.go
  2. 0
    23
      tlstypes/pools.go

+ 5
- 6
tlstypes/handshake.go Просмотреть файл

1
 package tlstypes
1
 package tlstypes
2
 
2
 
3
 import (
3
 import (
4
+	"bytes"
4
 	"io"
5
 	"io"
5
 
6
 
6
 	"github.com/9seconds/mtg/utils"
7
 	"github.com/9seconds/mtg/utils"
15
 }
16
 }
16
 
17
 
17
 func (h *Handshake) WriteBytes(writer io.Writer) {
18
 func (h *Handshake) WriteBytes(writer io.Writer) {
18
-	packetBuf := acquireBytesBuffer()
19
-	defer releaseBytesBuffer(packetBuf)
19
+	packetBuf := bytes.Buffer{}
20
 
20
 
21
 	writer.Write([]byte{byte(h.Type)}) // nolint: errcheck
21
 	writer.Write([]byte{byte(h.Type)}) // nolint: errcheck
22
 
22
 
24
 	packetBuf.Write(h.Random[:])
24
 	packetBuf.Write(h.Random[:])
25
 	packetBuf.WriteByte(byte(len(h.SessionID)))
25
 	packetBuf.WriteByte(byte(len(h.SessionID)))
26
 	packetBuf.Write(h.SessionID)
26
 	packetBuf.Write(h.SessionID)
27
-	h.Tail.WriteBytes(packetBuf)
27
+	h.Tail.WriteBytes(&packetBuf)
28
 
28
 
29
 	sizeUint24 := utils.ToUint24(uint32(packetBuf.Len()))
29
 	sizeUint24 := utils.ToUint24(uint32(packetBuf.Len()))
30
 	sizeUint24Bytes := sizeUint24[:]
30
 	sizeUint24Bytes := sizeUint24[:]
35
 }
35
 }
36
 
36
 
37
 func (h *Handshake) Len() int {
37
 func (h *Handshake) Len() int {
38
-	buf := acquireBytesBuffer()
39
-	defer releaseBytesBuffer(buf)
38
+	buf := bytes.Buffer{}
40
 
39
 
41
-	h.WriteBytes(buf)
40
+	h.WriteBytes(&buf)
42
 
41
 
43
 	return buf.Len()
42
 	return buf.Len()
44
 }
43
 }

+ 0
- 23
tlstypes/pools.go Просмотреть файл

1
-package tlstypes
2
-
3
-import (
4
-	"bytes"
5
-	"sync"
6
-)
7
-
8
-var (
9
-	poolBytesBuffer = sync.Pool{
10
-		New: func() interface{} {
11
-			return &bytes.Buffer{}
12
-		},
13
-	}
14
-)
15
-
16
-func acquireBytesBuffer() *bytes.Buffer {
17
-	return poolBytesBuffer.Get().(*bytes.Buffer)
18
-}
19
-
20
-func releaseBytesBuffer(buf *bytes.Buffer) {
21
-	buf.Reset()
22
-	poolBytesBuffer.Put(buf)
23
-}

Загрузка…
Отмена
Сохранить