Skip to content

Commit

Permalink
bounds-checking elimination
Browse files Browse the repository at this point in the history
  • Loading branch information
andot committed Mar 18, 2022
1 parent d4e3915 commit d29eadc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
29 changes: 15 additions & 14 deletions rpc/socket/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
| |
| rpc/socket/common.go |
| |
| LastModified: May 9, 2021 |
| LastModified: Mar 18, 2022 |
| Author: Ma Bingyao <[email protected]> |
| |
\*________________________________________________________*/
Expand All @@ -28,30 +28,31 @@ type data struct {
}

func makeHeader(length int, index int) (header [12]byte) {
header[4] = byte((length >> 24 & 0xff) | 0x80)
header[5] = byte(length >> 16 & 0xff)
header[6] = byte(length >> 8 & 0xff)
header[7] = byte(length & 0xff)
header[8] = byte(index >> 24 & 0xff)
header[9] = byte(index >> 16 & 0xff)
header[10] = byte(index >> 8 & 0xff)
header[11] = byte(index & 0xff)
header[10] = byte(index >> 8 & 0xff)
header[9] = byte(index >> 16 & 0xff)
header[8] = byte(index >> 24 & 0xff)
header[7] = byte(length & 0xff)
header[6] = byte(length >> 8 & 0xff)
header[5] = byte(length >> 16 & 0xff)
header[4] = byte((length >> 24 & 0xff) | 0x80)
crc := crc32.ChecksumIEEE(header[4:])
header[0] = byte(crc >> 24 & 0xff)
header[1] = byte(crc >> 16 & 0xff)
header[2] = byte(crc >> 8 & 0xff)
header[3] = byte(crc & 0xff)
header[2] = byte(crc >> 8 & 0xff)
header[1] = byte(crc >> 16 & 0xff)
header[0] = byte(crc >> 24 & 0xff)
return
}

func parseHeader(header [12]byte) (length int, index int, ok bool) {
crc := uint32(header[0])<<24 | uint32(header[1])<<16 | uint32(header[2])<<8 | uint32(header[3])
index = int(header[11]) | int(header[10])<<8 | int(header[9])<<16 | int(header[8])<<24
length = int(header[7]) | int(header[6])<<8 | int(header[5])<<16 | int(header[4]&0x7F)<<24
crc := uint32(header[3]) | uint32(header[2])<<8 | uint32(header[1])<<16 | uint32(header[0])<<24
if crc32.ChecksumIEEE(header[4:]) != crc {
index = -1
length = 0
return
}
length = int(header[4]&0x7F)<<24 | int(header[5])<<16 | int(header[6])<<8 | int(header[7])
index = int(header[8])<<24 | int(header[9])<<16 | int(header[10])<<8 | int(header[11])
if ok = (header[8]&0x80 == 0); !ok {
index &= 0x7fffffff
}
Expand Down
21 changes: 11 additions & 10 deletions rpc/udp/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
| |
| rpc/udp/common.go |
| |
| LastModified: May 5, 2021 |
| LastModified: May 18, 2022 |
| Author: Ma Bingyao <[email protected]> |
| |
\*________________________________________________________*/
Expand All @@ -26,26 +26,27 @@ type data struct {
}

func makeHeader(length int, index int) (header [8]byte) {
header[4] = byte(length >> 8 & 0xff)
header[5] = byte(length & 0xff)
header[6] = byte(index >> 8 & 0xff)
header[7] = byte(index & 0xff)
header[6] = byte(index >> 8 & 0xff)
header[5] = byte(length & 0xff)
header[4] = byte(length >> 8 & 0xff)
crc := crc32.ChecksumIEEE(header[4:])
header[0] = byte(crc >> 24 & 0xff)
header[1] = byte(crc >> 16 & 0xff)
header[2] = byte(crc >> 8 & 0xff)
header[3] = byte(crc & 0xff)
header[2] = byte(crc >> 8 & 0xff)
header[1] = byte(crc >> 16 & 0xff)
header[0] = byte(crc >> 24 & 0xff)
return
}

func parseHeader(header []byte) (length int, index int, ok bool) {
crc := uint32(header[0])<<24 | uint32(header[1])<<16 | uint32(header[2])<<8 | uint32(header[3])
index = int(header[7]) | int(header[6])<<8
length = int(header[5]) | int(header[4])<<8
crc := uint32(header[3]) | uint32(header[2])<<8 | uint32(header[1])<<16 | uint32(header[0])<<24
if crc32.ChecksumIEEE(header[4:]) != crc {
index = -1
length = 0
return
}
length = int(header[4])<<8 | int(header[5])
index = int(header[6])<<8 | int(header[7])
if ok = (header[6]&0x80 == 0); !ok {
index &= 0x7fff
}
Expand Down
10 changes: 5 additions & 5 deletions rpc/websocket/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
| |
| rpc/websocket/common.go |
| |
| LastModified: May 5, 2021 |
| LastModified: Mar 18, 2022 |
| Author: Ma Bingyao <[email protected]> |
| |
\*________________________________________________________*/
Expand All @@ -20,15 +20,15 @@ type data struct {
}

func makeHeader(index int) (header [4]byte) {
header[0] = byte(index >> 24 & 0xff)
header[1] = byte(index >> 16 & 0xff)
header[2] = byte(index >> 8 & 0xff)
header[3] = byte(index & 0xff)
header[2] = byte(index >> 8 & 0xff)
header[1] = byte(index >> 16 & 0xff)
header[0] = byte(index >> 24 & 0xff)
return
}

func parseHeader(header []byte) (index int, ok bool) {
index = int(header[0])<<24 | int(header[1])<<16 | int(header[2])<<8 | int(header[3])
index = int(header[3]) | int(header[2])<<8 | int(header[1])<<16 | int(header[0])<<24
if ok = (header[0]&0x80 == 0); !ok {
index &= 0x7fffffff
}
Expand Down

0 comments on commit d29eadc

Please sign in to comment.