aboutsummaryrefslogtreecommitdiff
path: root/libgo/go/encoding/json/encode.go
diff options
context:
space:
mode:
Diffstat (limited to 'libgo/go/encoding/json/encode.go')
-rw-r--r--libgo/go/encoding/json/encode.go38
1 files changed, 22 insertions, 16 deletions
diff --git a/libgo/go/encoding/json/encode.go b/libgo/go/encoding/json/encode.go
index 7d6c71d7a90..4a77ba1cd25 100644
--- a/libgo/go/encoding/json/encode.go
+++ b/libgo/go/encoding/json/encode.go
@@ -241,24 +241,15 @@ type encodeState struct {
scratch [64]byte
}
-// TODO(bradfitz): use a sync.Cache here
-var encodeStatePool = make(chan *encodeState, 8)
+var encodeStatePool sync.Pool
func newEncodeState() *encodeState {
- select {
- case e := <-encodeStatePool:
+ if v := encodeStatePool.Get(); v != nil {
+ e := v.(*encodeState)
e.Reset()
return e
- default:
- return new(encodeState)
- }
-}
-
-func putEncodeState(e *encodeState) {
- select {
- case encodeStatePool <- e:
- default:
}
+ return new(encodeState)
}
func (e *encodeState) marshal(v interface{}) (err error) {
@@ -936,6 +927,9 @@ func (e *encodeState) stringBytes(s []byte) (int, error) {
// A field represents a single field found in a struct.
type field struct {
name string
+ nameBytes []byte // []byte(name)
+ equalFold func(s, t []byte) bool // bytes.EqualFold or equivalent
+
tag bool
index []int
typ reflect.Type
@@ -943,6 +937,12 @@ type field struct {
quoted bool
}
+func fillField(f field) field {
+ f.nameBytes = []byte(f.name)
+ f.equalFold = foldFunc(f.nameBytes)
+ return f
+}
+
// byName sorts field by name, breaking ties with depth,
// then breaking ties with "name came from json tag", then
// breaking ties with index sequence.
@@ -1042,8 +1042,14 @@ func typeFields(t reflect.Type) []field {
if name == "" {
name = sf.Name
}
- fields = append(fields, field{name, tagged, index, ft,
- opts.Contains("omitempty"), opts.Contains("string")})
+ fields = append(fields, fillField(field{
+ name: name,
+ tag: tagged,
+ index: index,
+ typ: ft,
+ omitEmpty: opts.Contains("omitempty"),
+ quoted: opts.Contains("string"),
+ }))
if count[f.typ] > 1 {
// If there were multiple instances, add a second,
// so that the annihilation code will see a duplicate.
@@ -1057,7 +1063,7 @@ func typeFields(t reflect.Type) []field {
// Record new anonymous struct to explore in next round.
nextCount[ft]++
if nextCount[ft] == 1 {
- next = append(next, field{name: ft.Name(), index: index, typ: ft})
+ next = append(next, fillField(field{name: ft.Name(), index: index, typ: ft}))
}
}
}