Skip to content

Commit 0249004

Browse files
authored
Merge pull request #52 from Basekick-Labs/perf/encode-fastpaths
perf(encode): add map[string]string fast path in Encode() type switch
2 parents 5286520 + 9846cb5 commit 0249004

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

encode.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ func (e *Encoder) Encode(v interface{}) error {
246246
return e.encodeInt64Cond(int64(v))
247247
case time.Time:
248248
return e.EncodeTime(v)
249+
case map[string]string:
250+
return e.encodeMapStringString(v)
249251
case map[string]interface{}:
250252
if e.flags&sortMapKeysFlag != 0 {
251253
return e.EncodeMapSorted(v)

encode_map.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,27 @@ func encodeMapStringInterfaceValue(e *Encoder, v reflect.Value) error {
108108
return e.EncodeMap(m)
109109
}
110110

111+
func (e *Encoder) encodeMapStringString(m map[string]string) error {
112+
if m == nil {
113+
return e.EncodeNil()
114+
}
115+
if err := e.EncodeMapLen(len(m)); err != nil {
116+
return err
117+
}
118+
if e.flags&sortMapKeysFlag != 0 {
119+
return e.encodeSortedMapStringString(m)
120+
}
121+
for mk, mv := range m {
122+
if err := e.EncodeString(mk); err != nil {
123+
return err
124+
}
125+
if err := e.EncodeString(mv); err != nil {
126+
return err
127+
}
128+
}
129+
return nil
130+
}
131+
111132
func (e *Encoder) EncodeMap(m map[string]interface{}) error {
112133
if m == nil {
113134
return e.EncodeNil()

0 commit comments

Comments
 (0)