io: use optimized Read/WriteUXX for Read/WriteVarUint()

This commit is contained in:
Roman Khimov 2019-12-12 19:20:03 +03:00
parent 54d888ba70
commit 0b14916d79
2 changed files with 11 additions and 19 deletions

View file

@ -169,23 +169,16 @@ func (r *BinReader) ReadVarUint() uint64 {
return 0
}
var b uint8
r.Err = binary.Read(r.r, binary.LittleEndian, &b)
var b = r.ReadByte()
if b == 0xfd {
var v uint16
r.Err = binary.Read(r.r, binary.LittleEndian, &v)
return uint64(v)
return uint64(r.ReadU16LE())
}
if b == 0xfe {
var v uint32
r.Err = binary.Read(r.r, binary.LittleEndian, &v)
return uint64(v)
return uint64(r.ReadU32LE())
}
if b == 0xff {
var v uint64
r.Err = binary.Read(r.r, binary.LittleEndian, &v)
return v
return r.ReadU64LE()
}
return uint64(b)

View file

@ -123,24 +123,23 @@ func (w *BinWriter) WriteVarUint(val uint64) {
}
if val < 0xfd {
w.Err = binary.Write(w.w, binary.LittleEndian, uint8(val))
w.WriteByte(byte(val))
return
}
if val < 0xFFFF {
w.Err = binary.Write(w.w, binary.LittleEndian, byte(0xfd))
w.Err = binary.Write(w.w, binary.LittleEndian, uint16(val))
w.WriteByte(byte(0xfd))
w.WriteU16LE(uint16(val))
return
}
if val < 0xFFFFFFFF {
w.Err = binary.Write(w.w, binary.LittleEndian, byte(0xfe))
w.Err = binary.Write(w.w, binary.LittleEndian, uint32(val))
w.WriteByte(byte(0xfe))
w.WriteU32LE(uint32(val))
return
}
w.Err = binary.Write(w.w, binary.LittleEndian, byte(0xff))
w.Err = binary.Write(w.w, binary.LittleEndian, val)
w.WriteByte(byte(0xff))
w.WriteU64LE(val)
}
// WriteBytes writes a variable byte into the underlying io.Writer without prefix.