Skip to content

Commit 4412324

Browse files
committed
perf(reader): 使用unsafe.Slice优化ReadU8性能
通过使用unsafe.Slice直接读取数据到变量中,避免了额外的内存分配和拷贝操作,从而提高了ReadU8函数的性能。
1 parent f30d881 commit 4412324

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

utils/binary/reader.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,12 @@ func (r *Reader) ReadAll() []byte {
8181

8282
func (r *Reader) ReadU8() (v uint8) {
8383
if r.reader != nil {
84-
buf := make([]byte, 1)
85-
n, err := r.reader.Read(buf)
84+
n, err := r.reader.Read(unsafe.Slice(&v, 1))
8685
if err != nil || n < 1 {
8786
// 读取失败或读取的数据不足,返回零值
8887
return 0
8988
}
90-
v = buf[0]
89+
//v已经被Read调用填充
9190
return
9291
}
9392
// 确保缓冲区有足够的数据

0 commit comments

Comments
 (0)