0doc.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright (c) 2012, 2013 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license found in the LICENSE file.
  3. /*
  4. High Performance and Feature-Rich Idiomatic Go Library providing
  5. encode/decode support for different serialization formats.
  6. Supported Serialization formats are:
  7. - msgpack: [http://wiki.msgpack.org/display/MSGPACK/Format+specification]
  8. - binc: [http://www.ugorji.net/project/binc]
  9. To install:
  10. go get github.com/ugorji/go/codec
  11. The idiomatic Go support is as seen in other encoding packages in
  12. the standard library (ie json, xml, gob, etc).
  13. Rich Feature Set includes:
  14. - Simple but extremely powerful and feature-rich API
  15. - Very High Performance.
  16. Our extensive benchmarks show us outperforming Gob, Json and Bson by 2-4X.
  17. This was achieved by taking extreme care on:
  18. - managing allocation
  19. - stack frame size (important due to Go's use of split stacks),
  20. - reflection use
  21. - recursion implications
  22. - zero-copy mode (encoding/decoding to byte slice without using temp buffers)
  23. - Correct.
  24. Care was taken to precisely handle corner cases like:
  25. overflows, nil maps and slices, nil value in stream, etc.
  26. - Efficient zero-copying into temporary byte buffers
  27. when encoding into or decoding from a byte slice.
  28. - Standard field renaming via tags
  29. - Encoding from any value
  30. (struct, slice, map, primitives, pointers, interface{}, etc)
  31. - Decoding into pointer to any non-nil typed value
  32. (struct, slice, map, int, float32, bool, string, reflect.Value, etc)
  33. - Supports extension functions to handle the encode/decode of custom types
  34. - Schema-less decoding
  35. (decode into a pointer to a nil interface{} as opposed to a typed non-nil value).
  36. Includes Options to configure what specific map or slice type to use
  37. when decoding an encoded list or map into a nil interface{}
  38. - Provides a RPC Server and Client Codec for net/rpc communication protocol.
  39. - Msgpack Specific:
  40. - Provides extension functions to handle spec-defined extensions (binary, timestamp)
  41. - Options to resolve ambiguities in handling raw bytes (as string or []byte)
  42. during schema-less decoding (decoding into a nil interface{})
  43. - RPC Server/Client Codec for msgpack-rpc protocol defined at:
  44. http://wiki.msgpack.org/display/MSGPACK/RPC+specification
  45. Extension Support
  46. Users can register a function to handle the encoding or decoding of
  47. their custom types.
  48. There are no restrictions on what the custom type can be. Extensions can
  49. be any type: pointers, structs, custom types off arrays/slices, strings,
  50. etc. Some examples:
  51. type BisSet []int
  52. type BitSet64 uint64
  53. type UUID string
  54. type MyStructWithUnexportedFields struct { a int; b bool; c []int; }
  55. type GifImage struct { ... }
  56. Typically, MyStructWithUnexportedFields is encoded as an empty map because
  57. it has no exported fields, while UUID will be encoded as a string,
  58. etc. However, with extension support, you can encode any of these
  59. however you like.
  60. We provide implementations of these functions where the spec has defined
  61. an inter-operable format. For msgpack, these are Binary and
  62. time.Time. Library users will have to explicitly configure these as seen
  63. in the usage below.
  64. Usage
  65. Typical usage model:
  66. var (
  67. mapStrIntfTyp = reflect.TypeOf(map[string]interface{}(nil))
  68. sliceByteTyp = reflect.TypeOf([]byte(nil))
  69. timeTyp = reflect.TypeOf(time.Time{})
  70. )
  71. // create and configure Handle
  72. var (
  73. bh codec.BincHandle
  74. mh codec.MsgpackHandle
  75. )
  76. mh.MapType = mapStrIntfTyp
  77. // configure extensions for msgpack, to enable Binary and Time support for tags 0 and 1
  78. mh.AddExt(sliceByteTyp, 0, mh.BinaryEncodeExt, mh.BinaryDecodeExt)
  79. mh.AddExt(timeTyp, 1, mh.TimeEncodeExt, mh.TimeDecodeExt)
  80. // create and use decoder/encoder
  81. var (
  82. r io.Reader
  83. w io.Writer
  84. b []byte
  85. h = &bh // or mh to use msgpack
  86. )
  87. dec = codec.NewDecoder(r, h)
  88. dec = codec.NewDecoderBytes(b, h)
  89. err = dec.Decode(&v)
  90. enc = codec.NewEncoder(w, h)
  91. enc = codec.NewEncoderBytes(&b, h)
  92. err = enc.Encode(v)
  93. //RPC Server
  94. var rpcH codec.GoRpc // or codec.MsgpackSpecRpc
  95. go func() {
  96. for {
  97. conn, err := listener.Accept()
  98. rpcCodec := rpcH.ServerCodec(conn, h)
  99. rpc.ServeCodec(rpcCodec)
  100. }
  101. }()
  102. //RPC Communication (client side)
  103. conn, err = net.Dial("tcp", "localhost:5555")
  104. rpcCodec := rpcH.ClientCodec(conn, h)
  105. client := rpc.NewClientWithCodec(rpcCodec)
  106. Representative Benchmark Results
  107. A sample run of benchmark using "go test -bi -bench=.":
  108. ..............................................
  109. Benchmark:
  110. Struct recursive Depth: 1
  111. ApproxDeepSize Of benchmark Struct: 4758
  112. Benchmark One-Pass Run:
  113. msgpack: len: 1504
  114. binc: len: 1508
  115. gob: len: 1908
  116. json: len: 2402
  117. v-msgpack: len: 1536
  118. bson: len: 3009
  119. ..............................................
  120. Benchmark__Msgpack__Encode 50000 60824 ns/op
  121. Benchmark__Msgpack__Decode 10000 115119 ns/op
  122. Benchmark__Binc_____Encode 50000 55140 ns/op
  123. Benchmark__Binc_____Decode 10000 112132 ns/op
  124. Benchmark__Gob______Encode 10000 143350 ns/op
  125. Benchmark__Gob______Decode 5000 434248 ns/op
  126. Benchmark__Json_____Encode 10000 157298 ns/op
  127. Benchmark__Json_____Decode 5000 303729 ns/op
  128. Benchmark__Bson_____Encode 10000 174250 ns/op
  129. Benchmark__Bson_____Decode 10000 223602 ns/op
  130. Benchmark__VMsgpack_Encode 20000 80438 ns/op
  131. Benchmark__VMsgpack_Decode 10000 157330 ns/op
  132. To run full benchmark suite (including against vmsgpack and bson),
  133. see notes in ext_dep_test.go
  134. */
  135. package codec