Skip to content
This repository has been archived by the owner on Apr 30, 2023. It is now read-only.

Commit

Permalink
memory waste when extend slice
Browse files Browse the repository at this point in the history
  • Loading branch information
lysu committed Nov 16, 2015
1 parent fc5ec5c commit 4cb8b08
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
13 changes: 9 additions & 4 deletions node.go
Expand Up @@ -197,10 +197,15 @@ func (vr *variableResolver) resolve(target interface{}) (*Value, error) {
return nil, fmt.Errorf("Index out of range: %d (variable %s)", idxVal.Integer(), vr.String())
}
idxInt := int(idxVal.Integer())
nav := reflect.MakeSlice(current.Type(), idxInt+1, 2*(idxInt+1)+1)
reflect.Copy(nav, current)
current.Set(nav)
current.SetLen(idxInt + 1)
wantLen := idxInt + 1
if wantLen > current.Cap() {
nav := reflect.MakeSlice(current.Type(), wantLen, wantLen*2)
reflect.Copy(nav, current)
current.Set(nav)
current.SetLen(wantLen)
} else {
current.SetLen(wantLen)
}
current = current.Index(idxInt)
keySetter = &KeySetter{
prev: &Value{val: current},
Expand Down
2 changes: 1 addition & 1 deletion patcher.go
Expand Up @@ -18,7 +18,7 @@ func (p *Patcher) PatchIt(target interface{}, patch Patch) error {
return err
}

if targetValue.IsNil() {
if targetValue.IsNil() && targetValue.keySetter == nil {
return fmt.Errorf("path: %s doesn't match any property in target", path)
}

Expand Down

0 comments on commit 4cb8b08

Please sign in to comment.