Skip to content

Commit

Permalink
binary: better bce for NewTeaCipher
Browse files Browse the repository at this point in the history
  • Loading branch information
wdvxdr1123 committed Apr 14, 2022
1 parent 6e70533 commit 1b7e3d8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
10 changes: 6 additions & 4 deletions binary/tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
type TEA [4]uint32

// randuint32 returns a lock free uint32 value.
//
//go:linkname randuint32 runtime.fastrand
func randuint32() uint32

Expand Down Expand Up @@ -95,6 +96,7 @@ func (t *TEA) encode(n uint64) uint64 {
}

// 每次8字节
//
//go:nosplit
func (t *TEA) decode(n uint64) uint64 {
v0, v1 := uint32(n>>32), uint32(n)
Expand Down Expand Up @@ -141,9 +143,9 @@ func NewTeaCipher(key []byte) (t TEA) {
if len(key) != 16 {
return TEA{}
}
t[3] = binary.BigEndian.Uint32(key[12:])
t[2] = binary.BigEndian.Uint32(key[8:])
t[1] = binary.BigEndian.Uint32(key[4:])
t[0] = binary.BigEndian.Uint32(key[0:])
t[3] = binary.BigEndian.Uint32(key[12:16])
t[2] = binary.BigEndian.Uint32(key[8:12])
t[1] = binary.BigEndian.Uint32(key[4:8])
t[0] = binary.BigEndian.Uint32(key[:4])
return t
}
20 changes: 11 additions & 9 deletions client/internal/highway/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ import (
var etx = []byte{0x29}

// newFrame 包格式
// * STX: 0x28(40)
// * head length
// * body length
// * head data
// * body data
// * ETX: 0x29(41)
//
// - STX: 0x28(40)
// - head length
// - body length
// - head data
// - body data
// - ETX: 0x29(41)
//
// 节省内存, 可被go runtime优化为writev操作
func newFrame(head []byte, body []byte) net.Buffers {
buffers := make(net.Buffers, 4)
// buffer0 format:
// * STX
// * head length
// * body length
// - STX
// - head length
// - body length
buffer0 := make([]byte, 9)
buffer0[0] = 0x28
binary.BigEndian.PutUint32(buffer0[1:], uint32(len(head)))
Expand Down

0 comments on commit 1b7e3d8

Please sign in to comment.