Skip to content

Commit 9c71810

Browse files
authored
Merge pull request #152 from jpbetz/reflect-nopool-with-marshaller
Optimize conversion to unstructured
2 parents e6d9472 + 016ce3b commit 9c71810

File tree

7 files changed

+563
-290
lines changed

7 files changed

+563
-290
lines changed

value/fields.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type Field struct {
2727
Value Value
2828
}
2929

30-
// FieldList is a list of key-value pairs. Each field is expectUpdated to
30+
// FieldList is a list of key-value pairs. Each field is expected to
3131
// have a different name.
3232
type FieldList []Field
3333

value/listreflect.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (r listReflect) Length() int {
3232

3333
func (r listReflect) At(i int) Value {
3434
val := r.Value
35-
return mustWrapValueReflect(val.Index(i))
35+
return mustWrapValueReflect(val.Index(i), nil, nil)
3636
}
3737

3838
func (r listReflect) Unstructured() interface{} {
@@ -58,13 +58,15 @@ func (r listReflect) Range() ListRange {
5858
rr := lrrPool.Get().(*listReflectRange)
5959
rr.list = r.Value
6060
rr.i = -1
61+
rr.entry = nil
6162
return rr
6263
}
6364

6465
type listReflectRange struct {
65-
list reflect.Value
66-
vr *valueReflect
67-
i int
66+
list reflect.Value
67+
vr *valueReflect
68+
i int
69+
entry *TypeReflectCacheEntry
6870
}
6971

7072
func (r *listReflectRange) Next() bool {
@@ -79,7 +81,11 @@ func (r *listReflectRange) Item() (index int, value Value) {
7981
if r.i >= r.list.Len() {
8082
panic("Item() called on ListRange with no more items")
8183
}
82-
return r.i, r.vr.reuse(r.list.Index(r.i))
84+
v := r.list.Index(r.i)
85+
if r.entry == nil {
86+
r.entry = TypeReflectEntryOf(v.Type())
87+
}
88+
return r.i, r.vr.mustReuse(v, r.entry, nil, nil)
8389
}
8490

8591
func (r *listReflectRange) Recycle() {

value/mapreflect.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (r mapReflect) Get(key string) (Value, bool) {
3434
if !ok {
3535
return nil, false
3636
}
37-
return mustWrapValueReflectMapItem(&r.Value, &k, v), true
37+
return mustWrapValueReflect(v, &r.Value, &k), true
3838
}
3939

4040
func (r mapReflect) get(k string) (key, value reflect.Value, ok bool) {
@@ -68,21 +68,29 @@ func (r mapReflect) toMapKey(key string) reflect.Value {
6868
}
6969

7070
func (r mapReflect) Iterate(fn func(string, Value) bool) bool {
71+
if r.Value.Len() == 0 {
72+
return true
73+
}
7174
vr := reflectPool.Get().(*valueReflect)
7275
defer vr.Recycle()
73-
return eachMapEntry(r.Value, func(s string, value reflect.Value) bool {
74-
return fn(s, vr.reuse(value))
76+
return eachMapEntry(r.Value, func(e *TypeReflectCacheEntry, key reflect.Value, value reflect.Value) bool {
77+
// TODO: Track cache entry
78+
return fn(key.String(), vr.mustReuse(value, nil, &r.Value, &key))
7579
})
7680
}
7781

78-
func eachMapEntry(val reflect.Value, fn func(string, reflect.Value) bool) bool {
82+
func eachMapEntry(val reflect.Value, fn func(*TypeReflectCacheEntry, reflect.Value, reflect.Value) bool) bool {
7983
iter := val.MapRange()
84+
var entry *TypeReflectCacheEntry
8085
for iter.Next() {
8186
next := iter.Value()
8287
if !next.IsValid() {
8388
continue
8489
}
85-
if !fn(iter.Key().String(), next) {
90+
if entry == nil {
91+
entry = TypeReflectEntryOf(next.Type())
92+
}
93+
if !fn(entry, iter.Key(), next) {
8694
return false
8795
}
8896
}
@@ -114,6 +122,7 @@ func (r mapReflect) Equals(m Map) bool {
114122
if !ok {
115123
return false
116124
}
117-
return Equals(vr.reuse(lhsVal), value)
125+
// TODO: Track cache entry
126+
return Equals(vr.mustReuse(lhsVal, nil, nil, nil), value)
118127
})
119128
}

0 commit comments

Comments
 (0)