0doc.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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: [https://github.com/msgpack/msgpack]
  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. - Support Go 1.2 encoding.BinaryMarshaler/BinaryUnmarshaler
  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. go func() {
  95. for {
  96. conn, err := listener.Accept()
  97. rpcCodec := codec.GoRpc.ServerCodec(conn, h)
  98. //OR rpcCodec := codec.MsgpackSpecRpc.ServerCodec(conn, h)
  99. rpc.ServeCodec(rpcCodec)
  100. }
  101. }()
  102. //RPC Communication (client side)
  103. conn, err = net.Dial("tcp", "localhost:5555")
  104. rpcCodec := codec.GoRpc.ClientCodec(conn, h)
  105. //OR rpcCodec := codec.MsgpackSpecRpc.ClientCodec(conn, h)
  106. client := rpc.NewClientWithCodec(rpcCodec)
  107. Representative Benchmark Results
  108. A sample run of benchmark using "go test -bi -bench=.":
  109. ..............................................
  110. BENCHMARK INIT: 2013-10-04 14:36:50.381959842 -0400 EDT
  111. To run full benchmark comparing encodings (MsgPack, Binc, JSON, GOB, etc), use: "go test -bench=."
  112. Benchmark:
  113. Struct recursive Depth: 1
  114. ApproxDeepSize Of benchmark Struct: 4694 bytes
  115. Benchmark One-Pass Run:
  116. v-msgpack: len: 1600 bytes
  117. bson: len: 3025 bytes
  118. msgpack: len: 1560 bytes
  119. binc: len: 1187 bytes
  120. gob: len: 1972 bytes
  121. json: len: 2538 bytes
  122. ..............................................
  123. PASS
  124. Benchmark__Msgpack__Encode 50000 62774 ns/op 16336 B/op 93 allocs/op
  125. Benchmark__Msgpack__Decode 10000 113152 ns/op 16195 B/op 434 allocs/op
  126. Benchmark__Binc_____Encode 50000 73546 ns/op 18515 B/op 98 allocs/op
  127. Benchmark__Binc_____Decode 10000 112489 ns/op 16906 B/op 315 allocs/op
  128. Benchmark__Gob______Encode 10000 139114 ns/op 21143 B/op 237 allocs/op
  129. Benchmark__Gob______Decode 5000 412988 ns/op 82900 B/op 1840 allocs/op
  130. Benchmark__Json_____Encode 20000 80286 ns/op 13866 B/op 102 allocs/op
  131. Benchmark__Json_____Decode 10000 249694 ns/op 14153 B/op 493 allocs/op
  132. Benchmark__Bson_____Encode 10000 123965 ns/op 27739 B/op 514 allocs/op
  133. Benchmark__Bson_____Decode 10000 157703 ns/op 16441 B/op 789 allocs/op
  134. Benchmark__VMsgpack_Encode 50000 67791 ns/op 12358 B/op 343 allocs/op
  135. Benchmark__VMsgpack_Decode 10000 151476 ns/op 20264 B/op 571 allocs/op
  136. ok ugorji.net/codec 27.609s
  137. To run full benchmark suite (including against vmsgpack and bson),
  138. see notes in ext_dep_test.go
  139. */
  140. package codec