mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-11 20:20:27 +08:00
optimize(all): drop lstnq & impl. orbyte
This commit is contained in:
@@ -1,45 +1,10 @@
|
||||
package helper
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"sync"
|
||||
"github.com/fumiama/orbyte/pbuf"
|
||||
)
|
||||
|
||||
// https://github.com/Mrs4s/MiraiGo/blob/master/binary/pool.go
|
||||
|
||||
var bufferPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return new(Writer)
|
||||
},
|
||||
}
|
||||
|
||||
func MakeBytes(sz int) []byte {
|
||||
w := SelectWriter()
|
||||
b := w.Bytes()
|
||||
if cap(b) >= sz {
|
||||
return b[:sz]
|
||||
}
|
||||
return make([]byte, sz)
|
||||
}
|
||||
|
||||
func PutBytes(b []byte) {
|
||||
PutWriter((*Writer)(bytes.NewBuffer(b)))
|
||||
}
|
||||
|
||||
// SelectWriter 从池中取出一个 Writer
|
||||
func SelectWriter() *Writer {
|
||||
// 因为 bufferPool 定义有 New 函数
|
||||
// 所以 bufferPool.Get() 永不为 nil
|
||||
// 不用判空
|
||||
return bufferPool.Get().(*Writer)
|
||||
}
|
||||
|
||||
// PutWriter 将 Writer 放回池中
|
||||
func PutWriter(w *Writer) {
|
||||
// See https://golang.org/issue/23199
|
||||
const maxSize = 1 << 16
|
||||
if (*bytes.Buffer)(w).Cap() < maxSize { // 对于大Buffer直接丢弃
|
||||
w.Reset()
|
||||
bufferPool.Put(w)
|
||||
}
|
||||
return (*Writer)(pbuf.NewBuffer(nil))
|
||||
}
|
||||
|
||||
136
helper/writer.go
136
helper/writer.go
@@ -6,52 +6,22 @@ import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
"unsafe"
|
||||
|
||||
"github.com/fumiama/orbyte"
|
||||
"github.com/fumiama/orbyte/pbuf"
|
||||
)
|
||||
|
||||
// Writer 写入
|
||||
type Writer bytes.Buffer
|
||||
type Writer orbyte.Item[bytes.Buffer]
|
||||
|
||||
func NewWriterF(f func(writer *Writer)) []byte {
|
||||
func NewWriterF(f func(writer *Writer)) pbuf.Bytes {
|
||||
w := SelectWriter()
|
||||
f(w)
|
||||
b := append([]byte(nil), w.Bytes()...)
|
||||
w.put()
|
||||
return b
|
||||
}
|
||||
|
||||
// OpenWriterF must call func cl to close
|
||||
func OpenWriterF(f func(*Writer)) (b []byte, cl func()) {
|
||||
w := SelectWriter()
|
||||
f(w)
|
||||
return w.Bytes(), w.put
|
||||
}
|
||||
|
||||
func (w *Writer) FillUInt16() (pos int) {
|
||||
pos = w.Len()
|
||||
(*bytes.Buffer)(w).Write([]byte{0, 0})
|
||||
return
|
||||
}
|
||||
|
||||
func (w *Writer) WriteUInt16At(pos int, v uint16) {
|
||||
newdata := (*bytes.Buffer)(w).Bytes()[pos:]
|
||||
binary.LittleEndian.PutUint16(newdata, v)
|
||||
}
|
||||
|
||||
func (w *Writer) FillUInt32() (pos int) {
|
||||
pos = w.Len()
|
||||
(*bytes.Buffer)(w).Write([]byte{0, 0, 0, 0})
|
||||
return
|
||||
}
|
||||
|
||||
func (w *Writer) WriteUInt32At(pos int, v uint32) {
|
||||
newdata := (*bytes.Buffer)(w).Bytes()[pos:]
|
||||
binary.LittleEndian.PutUint32(newdata, v)
|
||||
return pbuf.BufferItemToBytes((*orbyte.Item[bytes.Buffer])(w).Trans())
|
||||
}
|
||||
|
||||
func (w *Writer) Write(b []byte) (n int, err error) {
|
||||
return (*bytes.Buffer)(w).Write(b)
|
||||
return (*orbyte.Item[bytes.Buffer])(w).Pointer().Write(b)
|
||||
}
|
||||
|
||||
func (w *Writer) WriteHex(h string) {
|
||||
@@ -60,7 +30,7 @@ func (w *Writer) WriteHex(h string) {
|
||||
}
|
||||
|
||||
func (w *Writer) WriteByte(b byte) error {
|
||||
return (*bytes.Buffer)(w).WriteByte(b)
|
||||
return (*orbyte.Item[bytes.Buffer])(w).Pointer().WriteByte(b)
|
||||
}
|
||||
|
||||
func (w *Writer) WriteUInt16(v uint16) {
|
||||
@@ -83,95 +53,25 @@ func (w *Writer) WriteUInt64(v uint64) {
|
||||
|
||||
func (w *Writer) WriteString(v string) {
|
||||
//w.WriteUInt32(uint32(len(v) + 4))
|
||||
(*bytes.Buffer)(w).WriteString(v)
|
||||
}
|
||||
|
||||
func (w *Writer) WriteStringShort(v string) {
|
||||
w.WriteUInt16(uint16(len(v)))
|
||||
(*bytes.Buffer)(w).WriteString(v)
|
||||
}
|
||||
|
||||
func (w *Writer) WriteBool(b bool) {
|
||||
if b {
|
||||
w.WriteByte(0x01)
|
||||
} else {
|
||||
w.WriteByte(0x00)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Writer) WriteBytesShort(data []byte) {
|
||||
w.WriteUInt16(uint16(len(data)))
|
||||
w.Write(data)
|
||||
(*orbyte.Item[bytes.Buffer])(w).Pointer().WriteString(v)
|
||||
}
|
||||
|
||||
func (w *Writer) Len() int {
|
||||
return (*bytes.Buffer)(w).Len()
|
||||
return (*orbyte.Item[bytes.Buffer])(w).Pointer().Len()
|
||||
}
|
||||
|
||||
func (w *Writer) Bytes() []byte {
|
||||
return (*bytes.Buffer)(w).Bytes()
|
||||
func (w *Writer) UnsafeBytes() []byte {
|
||||
return (*orbyte.Item[bytes.Buffer])(w).Pointer().Bytes()
|
||||
}
|
||||
|
||||
func (w *Writer) TransBytes() []byte {
|
||||
return (*orbyte.Item[bytes.Buffer])(w).Trans().Pointer().Bytes()
|
||||
}
|
||||
|
||||
func (w *Writer) Reset() {
|
||||
(*bytes.Buffer)(w).Reset()
|
||||
(*orbyte.Item[bytes.Buffer])(w).Pointer().Reset()
|
||||
}
|
||||
|
||||
func (w *Writer) Grow(n int) {
|
||||
(*bytes.Buffer)(w).Grow(n)
|
||||
}
|
||||
|
||||
func (w *Writer) Skip(n int) (int, error) {
|
||||
b := (*buffer)(unsafe.Pointer(w))
|
||||
b.lastRead = opInvalid
|
||||
if len(b.buf) <= b.off {
|
||||
// Buffer is empty, reset to recover space.
|
||||
w.Reset()
|
||||
if n == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
return 0, io.EOF
|
||||
}
|
||||
n = minnum(n, len(b.buf[b.off:]))
|
||||
b.off += n
|
||||
if n > 0 {
|
||||
b.lastRead = opRead
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (w *Writer) put() {
|
||||
PutWriter(w)
|
||||
}
|
||||
|
||||
// A Buffer is a variable-sized buffer of bytes with Read and Write methods.
|
||||
// The zero value for Buffer is an empty buffer ready to use.
|
||||
type buffer struct {
|
||||
buf []byte // contents are the bytes buf[off : len(buf)]
|
||||
off int // read at &buf[off], write at &buf[len(buf)]
|
||||
lastRead readOp // last read operation, so that Unread* can work correctly.
|
||||
}
|
||||
|
||||
// The readOp constants describe the last action performed on
|
||||
// the buffer, so that UnreadRune and UnreadByte can check for
|
||||
// invalid usage. opReadRuneX constants are chosen such that
|
||||
// converted to int they correspond to the rune size that was read.
|
||||
type readOp int8
|
||||
|
||||
// Don't use iota for these, as the values need to correspond with the
|
||||
// names and comments, which is easier to see when being explicit.
|
||||
const (
|
||||
opRead readOp = -1 // Any other read operation.
|
||||
opInvalid readOp = 0 // Non-read operation.
|
||||
opReadRune1 readOp = 1 // Read rune of size 1.
|
||||
opReadRune2 readOp = 2 // Read rune of size 2.
|
||||
opReadRune3 readOp = 3 // Read rune of size 3.
|
||||
opReadRune4 readOp = 4 // Read rune of size 4.
|
||||
)
|
||||
|
||||
// minnum 返回两数最小值,该函数将被内联
|
||||
func minnum[T int | int8 | uint8 | int16 | uint16 | int32 | uint32 | int64 | uint64](a, b T) T {
|
||||
if a > b {
|
||||
return b
|
||||
}
|
||||
return a
|
||||
(*orbyte.Item[bytes.Buffer])(w).Pointer().Grow(n)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user