Skip to content

Commit

Permalink
all: optimize
Browse files Browse the repository at this point in the history
detected by go-perfguard
  • Loading branch information
wdvxdr1123 committed Mar 27, 2022
1 parent 868828f commit 9422ce4
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
11 changes: 6 additions & 5 deletions binary/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"compress/zlib"
binary2 "encoding/binary"
"encoding/hex"
"io"
"net"

"github.com/Mrs4s/MiraiGo/utils"
Expand Down Expand Up @@ -34,15 +33,16 @@ func ZlibUncompress(src []byte) []byte {
var out bytes.Buffer
r, _ := zlib.NewReader(b)
defer r.Close()
io.Copy(&out, r)
_, _ = out.ReadFrom(r)
return out.Bytes()
}

func ZlibCompress(data []byte) []byte {
zw := acquireZlibWriter()
_, _ = zw.w.Write(data)
_ = zw.w.Close()
ret := append([]byte(nil), zw.buf.Bytes()...)
ret := make([]byte, len(zw.buf.Bytes()))
copy(ret, zw.buf.Bytes())
releaseZlibWriter(zw)
return ret
}
Expand All @@ -51,7 +51,8 @@ func GZipCompress(data []byte) []byte {
gw := AcquireGzipWriter()
_, _ = gw.Write(data)
_ = gw.Close()
ret := append([]byte(nil), gw.buf.Bytes()...)
ret := make([]byte, len(gw.buf.Bytes()))
copy(ret, gw.buf.Bytes())
ReleaseGzipWriter(gw)
return ret
}
Expand All @@ -61,7 +62,7 @@ func GZipUncompress(src []byte) []byte {
var out bytes.Buffer
r, _ := gzip.NewReader(b)
defer r.Close()
_, _ = io.Copy(&out, r)
_, _ = out.ReadFrom(r)
return out.Bytes()
}

Expand Down
3 changes: 2 additions & 1 deletion binary/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ type Writer bytes.Buffer
func NewWriterF(f func(writer *Writer)) []byte {
w := SelectWriter()
f(w)
b := append([]byte(nil), w.Bytes()...)
b := make([]byte, len(w.Bytes()))
copy(b, w.Bytes())
w.put()
return b
}
Expand Down
3 changes: 2 additions & 1 deletion client/internal/oicq/oicq.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ func (c *Codec) Marshal(m *Message) []byte {
}
w.WriteByte(0x03)

buf := append([]byte(nil), w.Bytes()...)
buf := make([]byte, len(w.Bytes()))
copy(buf, w.Bytes())
goBinary.BigEndian.PutUint16(buf[1:3], uint16(len(buf)))
return buf
}
Expand Down
6 changes: 3 additions & 3 deletions message/forward.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package message

import (
"bytes"
"crypto/md5"
"regexp"
"strings"
"sync"

"github.com/Mrs4s/MiraiGo/binary"
Expand Down Expand Up @@ -79,7 +79,7 @@ func (f *ForwardMessage) AddNode(node *ForwardNode) *ForwardMessage {
func (f *ForwardMessage) Length() int { return len(f.Nodes) }

func (f *ForwardMessage) Brief() string {
var brief bytes.Buffer
var brief strings.Builder
for _, n := range f.Nodes {
brief.WriteString(ToReadableString(n.Message))
if brief.Len() >= 27 {
Expand All @@ -90,7 +90,7 @@ func (f *ForwardMessage) Brief() string {
}

func (f *ForwardMessage) Preview() string {
var pv bytes.Buffer
var pv strings.Builder
for i, node := range f.Nodes {
if i >= 4 {
break
Expand Down
2 changes: 1 addition & 1 deletion utils/sys.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func ComputeMd5AndLength(r io.Reader) ([]byte, int64) {

func (r *multiReadSeeker) Read(p []byte) (int, error) {
if r.multiReader == nil {
var readers []io.Reader
readers := make([]io.Reader, len(r.readers))
for i := range r.readers {
_, _ = r.readers[i].Seek(0, io.SeekStart)
readers = append(readers, r.readers[i])
Expand Down

0 comments on commit 9422ce4

Please sign in to comment.