去二进制编码变体与固定切片长度
I need to encode integer keys as byte slices for a KV database. I want to make the encoding smaller and cut the zero padding. I thought the variant encoding from the binary package would be the way to go.
But in both cases, variant and fixed, the byte slice length is the same. Just different bits arrangement since first bit is used as a flag. I assumed the variant encoding would cut the "extra fat". No.
package main
import (
"encoding/binary"
"fmt"
)
func main() {
x := 16
y := 106547
fmt.Println(x)
fmt.Println(y)
// Variant
bvx := make([]byte, 8)
bvy := make([]byte, 8)
xbts := binary.PutUvarint(bvx, uint64(x))
ybts := binary.PutUvarint(bvy, uint64(y))
fmt.Println("Variant bytes written x: ", xbts)
fmt.Println("Variant bytes written y: ", ybts)
fmt.Println(bvx)
fmt.Println(bvy)
fmt.Println("bvx length: ", len(bvx))
fmt.Println("bvy length: ", len(bvy))
// Fixed
bfx := make([]byte, 8)
bfy := make([]byte, 8)
binary.LittleEndian.PutUint64(bfx, uint64(x))
binary.LittleEndian.PutUint64(bfy, uint64(y))
fmt.Println(bfx)
fmt.Println(bfy)
fmt.Println("bfx length: ", len(bfx))
fmt.Println("bfy length: ", len(bfy))
}
My question is. Do I have to splice the byte slice manually with variant encoding to get rid of the extra bytes?
Since put PutUvariant
returns the number of bytes written, I can just splice the byte slice.
Is this the right way to do it? If not, what is the correct way to make the slices smaller?
Thanks
我需要将整数键编码为KV数据库的字节片。 我想缩小编码并削减 我认为二进制包的变体编码将是解决问题的方法。 p>
但是在这两种情况下,变体和固定字节字节长度都是相同的。\ n由于第一位被用作标志,因此位的排列方式不同。 我假设变体编码将减少“多余的脂肪”。 p>
包main
import(
“ encoding / binary”
“ fmt”
)
func main(){
x := 16
y:= 106547
fmt.Println(x)
fmt.Println(y)
//变体
bvx:= make([] byte,8)
bvy: = make([] byte,8)
xbts:= binary.PutUvarint(bvx,uint64(x))
ybts:= binary.PutUvarint(bvy,uint64(y))
fmt.Println (“写入x:的变量字节,xbts)
fmt.Println(“写入y:的变量字节,ybts)
fmt.Println(bvx)
fmt.Println(bvy)
fmt .Println(“ bvx length:”,len(bvx))
fmt.Println(“ bvy length:”,len(bvy))
//固定
bfx:= make([] byte,8 )
bfy:= make([] byte,8)
binary.LittleEndian.PutUint64(bfx,uint64(x))
binary.LittleEndian.PutUint64(bfy,uint64(y))
fmt.Println(bfx)
fmt.Println(bfy)
fmt.Println(“ bfx length:”,len(bfx))
fmt.Println(“ bfy length:”,len(bfy))
}
code> pre>
我的问题是。 我是否必须使用变体编码手动拼接字节片以消除多余的字节?
由于放 PutUvariant code>返回写入的字节数,所以我可以拼接字节片。 p>
这是正确的方法吗?
如果不是,缩小切片的正确方法是什么? p>
谢谢 p>
import "encoding/binary"
func PutUvarint(buf []byte, x uint64) int
PutUvarint encodes a uint64 into buf and returns the number of bytes written. If the buffer is too small, PutUvarint will panic.
Fix your code:
bvx := make([]byte, binary.MaxVarintLen64)
bvy := make([]byte, binary.MaxVarintLen64)
bvx = bvx[:binary.PutUvarint(bvx[:cap(bvx)], uint64(x))]
bvy = bvy[:binary.PutUvarint(bvy[:cap(bvy)], uint64(y))]
package main
import (
"encoding/binary"
"fmt"
)
func main() {
x := 16
y := 106547
fmt.Println(x)
fmt.Println(y)
// Variant
bvx := make([]byte, binary.MaxVarintLen64)
bvy := make([]byte, binary.MaxVarintLen64)
bvx = bvx[:binary.PutUvarint(bvx[:cap(bvx)], uint64(x))]
bvy = bvy[:binary.PutUvarint(bvy[:cap(bvy)], uint64(y))]
fmt.Println("Variant bytes written x: ", len(bvx))
fmt.Println("Variant bytes written y: ", len(bvy))
fmt.Println(bvx)
fmt.Println(bvy)
fmt.Println("bvx length: ", len(bvx))
fmt.Println("bvy length: ", len(bvy))
// Fixed
bfx := make([]byte, 8)
bfy := make([]byte, 8)
binary.LittleEndian.PutUint64(bfx, uint64(x))
binary.LittleEndian.PutUint64(bfy, uint64(y))
fmt.Println(bfx)
fmt.Println(bfy)
fmt.Println("bfx length: ", len(bfx))
fmt.Println("bfy length: ", len(bfy))
}
Playground: https://play.golang.org/p/XN46KafMY23
Output:
16
106547
Variant bytes written x: 1
Variant bytes written y: 3
[16]
[179 192 6]
bvx length: 1
bvy length: 3
[16 0 0 0 0 0 0 0]
[51 160 1 0 0 0 0 0]
bfx length: 8
bfy length: 8