negentropy: slightly faster encodeVarInt()

This commit is contained in:
fiatjaf 2024-09-18 15:50:50 -03:00
parent 6910f391fe
commit b1dd120ec7

View File

@ -125,15 +125,19 @@ func encodeVarInt(n int) []byte {
return []byte{0}
}
var o []byte
result := make([]byte, 8)
idx := 7
for n != 0 {
o = append([]byte{byte(n & 0x7F)}, o...)
result[idx] = byte(n & 0x7F)
n >>= 7
idx--
}
for i := 0; i < len(o)-1; i++ {
o[i] |= 0x80
result = result[idx+1:]
for i := 0; i < len(result)-1; i++ {
result[i] |= 0x80
}
return o
return result
}