Skip to content

brainpicture/gotiny

 
 

Repository files navigation

gotiny is still in the early stages of development and is not recommended.

#gotiny Build status License GoDoc Go Report Card Gotiny is an efficiency-oriented go language serialization library. Gotiny improves efficiency by pre-generating the encoder and reducing the use of the reflect library, 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{}
   coder := gotiny.New(src1, src2)
   coder.Decode(coder.Encode(&src1, &src2), &ret1, &ret2)
   fmt.Println(ret1 + string(ret2)) // print "hello world!"
}

Features

  • The efficiency is very high, which is more than 3 times that of golang's own serialization library gob, which is at the same level and even higher than the general generated code serialization framework.
  • 0 memory application except map type.
  • Supports encoding all golang built-in types and custom types except for func, chan types.
  • The struct type encodes non-exported fields, which can be set without the encoding by way of golang tag.
  • Strict type conversion. Only the same type of gotiny will be correctly encoded and decoded.
  • Encode the nil value of the band type.
  • Can handle loop types, can't encode loop values, stack overflow.
  • All types that can be encoded are completely decoded, regardless of the original value and what the target value is.
  • The byte string generated by the encoding does not contain type information, and the generated byte array is very small. – Threadsafe, once created scheme can be used from different goroutines

Unable to process loop value Circular reference not supported TODO

Type a *a
Var b a
b = &b

install

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

Encoding Protocol

Boolean type

The bool type occupies one bit, 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, it will apply for one byte, the value will be programmed into the lowest position, the second time will be programmed into the next low position, and the ninth time when the bool value is encountered, then apply for one byte to be programmed into the lowest position. And so on.

Integer type

Floating point type

Float32 and float64 use the encoding of floating point types in gob.

plural type

  • The complex64 type will be strongly converted to a uint64 and then encoded using uint64.
  • The complex128 type encodes the virtual real part as a float64 type.

String type

The string type first converts the length of the string to a uint64 [Varints] type encoding, and then encodes the string byte array itself.

pointer type

The pointer type determines whether it is nil. If it is nil, it ends with a false value of bool type. If it is not nil, it encodes a bool type true value, then dereferences the pointer and encodes the type after dereferencing.

array and slice types

First convert the length to a uint64 and then encode it using uint64 encoding, then install each element's own type encoding.

map type

Same as above, first compile the length, then compile a health, followed by the value corresponding to the key, compile a key, then a value, and so on.

struct type

All members of a structure are encoded by their type, and non-exported fields are encoded regardless of whether they are exported or not. The structure will be strictly restored.

Implementing the type of interface

  • The type that implements the encoding package BinaryMarshaler/BinaryUnmarshaler or implements the gob package GobEncoder/GobDecoder interface is encoded with the implementation method.
  • For implementations of the type of gotiny.GoTinySerialize package will be encoded and decoded using the implemented method

Schemes [Experimental] API would likely to change

Schemes allow perform data migrations, deserialise data into objects which has slightly different fields than ones, which was used for data serialisation.

type T1 struct {
	I      uint32
	Str    string
}
type T2 struct { // different order binary format is different
    Str    string
	I      uint32
}
func main() {
    coder1 = gotiny.New(T1{})
    coder2 = gotiny.New(T2{})
    schemeJSON := coder1.GetScheme().AsJSON()
    scheme1, _ := gotiny.SchemeFromJSON(schemeJSON)
    coder2.SetScheme(scheme1)

    // Encoding part
    encoded := coder1.Encode(T1{32, "wow"})
    result := T2{}
    coder2.Decode(encoded, &result)
}

benchmark

benchmark

License

MIT

About

gotiny Is an efficient golang serialization library.

Resources

License

Stars

1 star

Watchers

1 watching

Forks

Packages

 
 
 

Contributors

Languages

  • Go 100.0%