Skip to content
This repository was archived by the owner on Dec 15, 2025. It is now read-only.

Commit 50e4910

Browse files
committed
document how to get best performance
1 parent f29fe74 commit 50e4910

File tree

3 files changed

+64
-18
lines changed

3 files changed

+64
-18
lines changed

example_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,47 @@ func ExampleUnmarshal() {
4545
// Output:
4646
// [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
4747
}
48+
49+
50+
func ExampleMarshalWithBestPerformance() {
51+
type ColorGroup struct {
52+
ID int
53+
Name string
54+
Colors []string
55+
}
56+
group := ColorGroup{
57+
ID: 1,
58+
Name: "Reds",
59+
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
60+
}
61+
stream := jsoniter.ConfigFastest.BorrowStream(nil)
62+
defer jsoniter.ConfigFastest.ReturnStream(stream)
63+
stream.WriteVal(group)
64+
if stream.Error != nil {
65+
fmt.Println("error:", stream.Error)
66+
}
67+
os.Stdout.Write(stream.Buffer())
68+
// Output:
69+
// {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
70+
}
71+
72+
func ExampleUnmarshalWithBestPerformance() {
73+
var jsonBlob = []byte(`[
74+
{"Name": "Platypus", "Order": "Monotremata"},
75+
{"Name": "Quoll", "Order": "Dasyuromorphia"}
76+
]`)
77+
type Animal struct {
78+
Name string
79+
Order string
80+
}
81+
var animals []Animal
82+
iter := jsoniter.ConfigFastest.BorrowIterator(jsonBlob)
83+
defer jsoniter.ConfigFastest.ReturnIterator(iter)
84+
iter.ReadVal(&animals)
85+
if iter.Error != nil {
86+
fmt.Println("error:", iter.Error)
87+
}
88+
fmt.Printf("%+v", animals)
89+
// Output:
90+
// [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
91+
}

feature_config.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ func (cfg *frozenConfig) CleanEncoders() {
149149
}
150150

151151
func (cfg *frozenConfig) MarshalToString(v interface{}) (string, error) {
152-
stream := cfg.borrowStream()
153-
defer cfg.returnStream(stream)
152+
stream := cfg.BorrowStream(nil)
153+
defer cfg.ReturnStream(stream)
154154
stream.WriteVal(v)
155155
if stream.Error != nil {
156156
return "", stream.Error
@@ -159,8 +159,8 @@ func (cfg *frozenConfig) MarshalToString(v interface{}) (string, error) {
159159
}
160160

161161
func (cfg *frozenConfig) Marshal(v interface{}) ([]byte, error) {
162-
stream := cfg.borrowStream()
163-
defer cfg.returnStream(stream)
162+
stream := cfg.BorrowStream(nil)
163+
defer cfg.ReturnStream(stream)
164164
stream.WriteVal(v)
165165
if stream.Error != nil {
166166
return nil, stream.Error
@@ -174,8 +174,8 @@ func (cfg *frozenConfig) Marshal(v interface{}) ([]byte, error) {
174174
func (cfg *frozenConfig) UnmarshalFromString(str string, v interface{}) error {
175175
data := []byte(str)
176176
data = data[:lastNotSpacePos(data)]
177-
iter := cfg.borrowIterator(data)
178-
defer cfg.returnIterator(iter)
177+
iter := cfg.BorrowIterator(data)
178+
defer cfg.ReturnIterator(iter)
179179
iter.ReadVal(v)
180180
if iter.head == iter.tail {
181181
iter.loadMore()
@@ -192,8 +192,8 @@ func (cfg *frozenConfig) UnmarshalFromString(str string, v interface{}) error {
192192
func (cfg *frozenConfig) UnmarshalAnyFromString(str string) (Any, error) {
193193
data := []byte(str)
194194
data = data[:lastNotSpacePos(data)]
195-
iter := cfg.borrowIterator(data)
196-
defer cfg.returnIterator(iter)
195+
iter := cfg.BorrowIterator(data)
196+
defer cfg.ReturnIterator(iter)
197197
any := iter.ReadAny()
198198
if iter.head == iter.tail {
199199
iter.loadMore()
@@ -209,8 +209,8 @@ func (cfg *frozenConfig) UnmarshalAnyFromString(str string) (Any, error) {
209209

210210
func (cfg *frozenConfig) UnmarshalAny(data []byte) (Any, error) {
211211
data = data[:lastNotSpacePos(data)]
212-
iter := cfg.borrowIterator(data)
213-
defer cfg.returnIterator(iter)
212+
iter := cfg.BorrowIterator(data)
213+
defer cfg.ReturnIterator(iter)
214214
any := iter.ReadAny()
215215
if iter.head == iter.tail {
216216
iter.loadMore()
@@ -226,8 +226,8 @@ func (cfg *frozenConfig) UnmarshalAny(data []byte) (Any, error) {
226226

227227
func (cfg *frozenConfig) Unmarshal(data []byte, v interface{}) error {
228228
data = data[:lastNotSpacePos(data)]
229-
iter := cfg.borrowIterator(data)
230-
defer cfg.returnIterator(iter)
229+
iter := cfg.BorrowIterator(data)
230+
defer cfg.ReturnIterator(iter)
231231
typ := reflect.TypeOf(v)
232232
if typ.Kind() != reflect.Ptr {
233233
// return non-pointer error

feature_pool.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package jsoniter
22

3-
func (cfg *frozenConfig) borrowStream() *Stream {
3+
import "io"
4+
5+
func (cfg *frozenConfig) BorrowStream(writer io.Writer) *Stream {
46
select {
57
case stream := <-cfg.streamPool:
6-
stream.Reset(nil)
8+
stream.Reset(writer)
79
return stream
810
default:
9-
return NewStream(cfg, nil, 512)
11+
return NewStream(cfg, writer, 512)
1012
}
1113
}
1214

13-
func (cfg *frozenConfig) returnStream(stream *Stream) {
15+
func (cfg *frozenConfig) ReturnStream(stream *Stream) {
1416
select {
1517
case cfg.streamPool <- stream:
1618
return
@@ -19,7 +21,7 @@ func (cfg *frozenConfig) returnStream(stream *Stream) {
1921
}
2022
}
2123

22-
func (cfg *frozenConfig) borrowIterator(data []byte) *Iterator {
24+
func (cfg *frozenConfig) BorrowIterator(data []byte) *Iterator {
2325
select {
2426
case iter := <- cfg.iteratorPool:
2527
iter.ResetBytes(data)
@@ -29,7 +31,7 @@ func (cfg *frozenConfig) borrowIterator(data []byte) *Iterator {
2931
}
3032
}
3133

32-
func (cfg *frozenConfig) returnIterator(iter *Iterator) {
34+
func (cfg *frozenConfig) ReturnIterator(iter *Iterator) {
3335
select {
3436
case cfg.iteratorPool <- iter:
3537
return

0 commit comments

Comments
 (0)