Skip to content

Commit

Permalink
leverage reflect.MakeMapWithSize introduced in go1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
jmank88 committed Aug 26, 2017
1 parent f3e0a30 commit 57e0933
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
6 changes: 2 additions & 4 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,8 +669,7 @@ func objectIntoStruct(structPtr reflect.Value) func(*ObjectDecoder) error {
func objectIntoMap(mapPtr reflect.Value) func(*ObjectDecoder) error {
return func(o *ObjectDecoder) error {
mapValue := mapPtr.Elem()
//TODO go1.9 - MakeMapWithSize
mapValue.Set(reflect.MakeMap(mapValue.Type()))
mapValue.Set(makeMap(mapValue.Type(), o.Len))
elemType := mapValue.Type().Elem()

for o.NextEntry() {
Expand All @@ -697,8 +696,7 @@ func objectAsInterface(o *ObjectDecoder) (interface{}, error) {
valType := elementTypeFor(o.ValType)
mapType := reflect.MapOf(stringType, valType)

//TODO go1.9 - MakeMapWithSize
mapValue := reflect.MakeMap(mapType)
mapValue := makeMap(mapType, o.Len)
for o.NextEntry() {
k, err := o.DecodeKey()
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions decode_go18.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// +build !go1.9

package ubjson

import "reflect"

func makeMap(typ reflect.Type, _ int) reflect.Value {
return reflect.MakeMap(typ)
}
12 changes: 12 additions & 0 deletions decode_go19.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// +build go1.9

package ubjson

import "reflect"

func makeMap(typ reflect.Type, cap int) reflect.Value {
if cap < 0 {
cap = 0
}
return reflect.MakeMapWithSize(typ, cap)
}

0 comments on commit 57e0933

Please sign in to comment.