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