github.com/RomiChan/protobuf@v0.1.1-0.20230204044148-2ed269a2e54d/proto/size.go (about)

     1  package proto
     2  
     3  import (
     4  	"math/bits"
     5  	"unsafe"
     6  )
     7  
     8  type sizeFunc = func(unsafe.Pointer, *structField) int
     9  
    10  func sizeOfVarint(v uint64) int {
    11  	// This computes 1 + (bits.Len64(v)-1)/7.
    12  	// 9/64 is a good enough approximation of 1/7
    13  	// see https://github.com/protocolbuffers/protobuf-go/commit/a30b571f93edc9b3bd5df1dd61ceaeb17aa7f7c5
    14  	return int(9*uint32(bits.Len64(v))+64) / 64
    15  }
    16  
    17  func sizeOfVarlen(n int) int {
    18  	return sizeOfVarint(uint64(n)) + n
    19  }