feat(backend): reader maintains multibyte type

This commit is contained in:
Alex Kaminskii 2023-02-14 14:04:31 +01:00
parent d0563d3fcb
commit fc499aabd1
2 changed files with 14 additions and 2 deletions

View file

@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io"
"math"
)
var (
@ -50,6 +51,15 @@ func WriteUint(v uint64, buf []byte, p int) int {
return p + 1
}
func ByteSizeUint(v uint64) int {
if v == 0 {
return 1
}
nBits := math.Floor(math.Log2(float64(v))) + 1
nBytes := math.Ceil(nBits / 7)
return int(nBytes)
}
func ReadInt(reader io.Reader) (int64, error) {
ux, err := ReadUint(reader)
x := int64(ux >> 1)

View file

@ -70,13 +70,15 @@ func (m *messageReaderImpl) Parse() (err error) {
}
// Dirty hack to avoid extra memory allocation
m.data[curr-1] = uint8(m.msgType)
mTypeByteSize := ByteSizeUint(m.msgType)
from := int(curr) - mTypeByteSize
WriteUint(m.msgType, m.data, from)
// Add message meta to list
m.list = append(m.list, &MessageMeta{
msgType: m.msgType,
msgSize: m.msgSize + 1,
msgFrom: uint64(curr - 1),
msgFrom: uint64(from),
})
// Update data pointer