gotiny

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 20, 2022 License: MIT Imports: 8 Imported by: 0

README

gotiny Build status License GoDoc Go Report Card

gotiny是一个注重效率的go语言序列化库。gotiny通过预先生成编码机和减少使用reflect库等方式来提高效率,几乎和生成代码的序列化库有同样高的速度。

gotiny is a serialization library for go language that focuses on efficiency. Gotiny improves efficiency by pre-generating the encoder and reducing the use of the reflect library, which is almost as fast as the serialization library that generates the code.

hello word

package main
import (
    "fmt"
    "github.com/niubaoshu/gotiny"
)

func main() {
    src1, src2 := "hello", []byte(" world!")
    ret1, ret2 := "", []byte{}
    gotiny.Unmarshal(gotiny.Marshal(&src1, &src2), &ret1, &ret2)
    fmt.Println(ret1 + string(ret2)) // print "hello world!"
}

特性 characteristic

  • 效率非常的高,是golang自带序列化库gob的3倍以上,和一般的生成代码序列化框架处于同一水平,甚至高于它们。

    The efficiency is very high, more than 3 times that of golang's own serialization library gob, and it is at the same level as the general generated code serialization framework, or even higher than them.

  • 除map类型外0内存申请。

    0 memory allocations except for the map type.

  • 支持编码所有的除func,chan类型外的所有golang内置类型和自定义类型。

    Supports encoding all golang built-in types and custom types except func and chan types.

  • struct 类型会编码非导出字段,可通过golang tag 的方式设置不编码。

    The struct type will encode non-exported fields, which can be set to not be encoded by golang tag.

  • 严格的类型转换。gotiny中只有类型完全相同的才会正确编码和解码。

    Strict type conversion. Only the exact same type in gotiny will be encoded and decoded correctly.

  • 编码带类型的nil值。

    Encodes a typed nil value.

  • 可以处理循环类型,不能编码循环值,会栈溢出。

    It can handle loop types, but cannot encode loop values, which will cause stack overflow.

  • 所有可以编码的类型都会完全的解码,不论原值是什么和目标值是什么。

    All types that can be encoded are fully decoded, regardless of the original value and the target value.

  • 编码生成的字节串不包含类型信息,生成的字节数组非常小。

    The byte string generated by the encoding does not contain type information, and the resulting byte array is very small.

无法处理循环值 不支持循环引用 cannot handle circular values circular references are not supported TODO

type a *a
var b a
b = &b

install

$ go get -u github.com/niubaoshu/gotiny

编码协议 encoding protocol

布尔类型 boolean type

bool类型占用一位,真值编码为1,假值编码为0。当第一次遇到bool类型时会申请一个字节,将值编入最低位,第二次遇到时编入次低位,第九次遇到bool值时再申请一个字节编入最低位,以此类推。

The bool type occupies one bit, and the true value is encoded as 1, and the false value is encoded as 0. When the bool type is encountered for the first time, a byte will be applied, and the value will be programmed into the lowest digit. When the second encounter is encountered, the next lower digit will be applied. When the bool value is encountered for the ninth time, another byte will be applied and programmed into the lowest digit. , and so on.

整数类型

  • uint8和int8 类型作为一个字节编入字符串的下一个字节。The uint8 and int8 types are programmed into the next byte of the string as a byte.
  • uint16,uint32,uint64,uint,uintptr 采用Varints编码方式。uint16, uint32, uint64, uint, uintptr use Varints encoding.
  • int16,int32,int64,int 采用ZigZag转换成一个无符号数后采用Varints编码方式。int16,int32,int64,int use ZigZag to convert to an unsigned number and use Varints encoding method.

浮点数类型

float32和float64采用gob中对浮点类型的编码方式。float32 and float64 use the encoding method for floating point types in gob.

复数类型

  • complex64类型会强转为一个uint64后采用uint64的编码方式。The complex64 type will be cast to a uint64 and then the uint64 encoding will be used.
  • complex128类型分别将虚实部分作为float64类型编码。The complex128 type encodes the virtual and real parts as float64 types, respectively.

字符串类型 String type

字符串类型先将字符串长度强转为uint64类型编码,然后将字符串字节数组自身原样编码。The string type first converts the string length to uint64 type encoding, and then encodes the string byte array itself as it is.

指针类型 pointer type

指针类型判断是否为nil,如果是nil,编入一个bool类型的false值后结束,如果不为nil,编入一个bool类型true值,之后将指针解引用,按解引用后的类型编码。The pointer type is judged whether it is nil. If it is nil, it will end with a bool type false value. If it is not nil, a bool type true value will be added, and then the pointer will be dereferenced and encoded according to the dereferenced type.

array和slice类型 array and slice types

先将长度强转为一个uint64后采用uint64的编码方式编入,然后将每一个元素安装自身的类型编码。 First convert the length to a uint64, and then use the uint64 encoding method to encode it, and then install each element with its own type encoding.

map类型 map type

同上,先编入长度,然后编入一个健,后面跟健对应的值,在编入一个健,接着是值,以此类推。Same as above, first program the length, then program a key, followed by the value corresponding to the health, then program a key, then the value, and so on.

struct类型 struct type

将结构体的所有成员按其类型编码,无论是否导出,非导出的字段也会编码。结构体会严格还原。Encodes all members of the struct by their type, whether exported or not, non-exported fields are also encoded. Structural experience is strictly reduced.

实现接口的类型 the type that implements the interface

  • 对于实现encoding包BinaryMarshaler/BinaryUnmarshaler 或 实现 gob包GobEncoder/GobDecoder 接口的类型会用实现的方法编码。For types that implement the encoding package BinaryMarshaler/BinaryUnmarshaler or the gob package GobEncoder/GobDecoder interface, the implemented method will be used for encoding.
  • 对于实现了gotiny.GoTinySerialize包的类型将采用实现的方法编码和解码 For types that implement the gotiny.GoTinySerialize package, the implemented methods will be used to encode and decode

benchmark

benchmark

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetName

func GetName(obj interface{}) string

func GetNameByType

func GetNameByType(rt reflect.Type) string

func Marshal

func Marshal(is ...interface{}) []byte

func Register

func Register(i interface{}) string

func RegisterName

func RegisterName(name string, rt reflect.Type)

func Unmarshal

func Unmarshal(buf []byte, is ...interface{}) int

func UnusedUnixNanoEncodeTimeType

func UnusedUnixNanoEncodeTimeType()

Types

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

func NewDecoder

func NewDecoder(is ...interface{}) *Decoder

func NewDecoderWithPtr

func NewDecoderWithPtr(is ...interface{}) *Decoder

func NewDecoderWithType

func NewDecoderWithType(ts ...reflect.Type) *Decoder

func (*Decoder) Decode

func (d *Decoder) Decode(buf []byte, is ...interface{}) int

is is pointer of variable

func (*Decoder) DecodePtr

func (d *Decoder) DecodePtr(buf []byte, ps ...unsafe.Pointer) int

ps is a unsafe.Pointer of the variable

func (*Decoder) DecodeValue

func (d *Decoder) DecodeValue(buf []byte, vs ...reflect.Value) int

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

func NewEncoder

func NewEncoder(is ...interface{}) *Encoder

NewEncoder 创建一个编码is 类型的编码器 Create an encoder that encodes the is type

func NewEncoderWithPtr

func NewEncoderWithPtr(ps ...interface{}) *Encoder

NewEncoderWithPtr 创建一个编码ps 指向类型的编码器 Create an encoder that encodes the type pointed to by ps

func NewEncoderWithType

func NewEncoderWithType(ts ...reflect.Type) *Encoder

NewEncoderWithType ...

func (*Encoder) AppendTo

func (e *Encoder) AppendTo(buf []byte)

AppendTo 编码产生的数据将append到buf上 The data generated by encoding will be appended to buf

func (*Encoder) Encode

func (e *Encoder) Encode(is ...interface{}) []byte

Encode 入参是要编码值的指针 The input parameter is a pointer to the value to be encoded

func (*Encoder) EncodePtr

func (e *Encoder) EncodePtr(ps ...unsafe.Pointer) []byte

EncodePtr 入参是要编码的值得unsafe.Pointer 指针 The input parameter is an unsafe.Pointer pointer worth encoding to

func (*Encoder) EncodeValue

func (e *Encoder) EncodeValue(vs ...reflect.Value) []byte

EncodeValue vs 是持有要编码的值 is the value that holds the value to be encoded

type GoTinySerializer

type GoTinySerializer interface {
	// GotinyEncode 编码方法,将对象的序列化结果append到入参数并返回,方法不应该修改入参数值原有的值
	//
	// Encoding method, append the serialization result of the object to the
	// input parameter and return it, the method should not modify the
	// original value of the input parameter value
	GotinyEncode([]byte) []byte
	// GotinyDecode 解码方法,将入参解码到对象里并返回使用的长度。方法从入参的第0个字节开始使用,并且不应该修改入参中的任何数据
	//
	// The decoding method, which decodes the input parameters into the
	// object and returns the length used. The method is used from the 0th
	// byte of the input parameter and should not modify any data in the
	// input parameter
	GotinyDecode([]byte) int
}

GoTinySerializer 只应该由指针来实现该接口 This interface should only be implemented by pointers

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL