Skip to content

Commit

Permalink
Merge pull request ipfs#11 from ipfs/fast-remove
Browse files Browse the repository at this point in the history
perf: avoid allocations when filtering nodes
  • Loading branch information
Stebalien authored Aug 30, 2018
2 parents 582545b + 9e4c9b9 commit 319d880
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,24 @@ func (n *ProtoNode) AddRawLink(name string, l *ipld.Link) error {
// RemoveNodeLink removes a link on this node by the given name.
func (n *ProtoNode) RemoveNodeLink(name string) error {
n.encoded = nil
good := make([]*ipld.Link, 0, len(n.links))
var found bool

for _, l := range n.links {
if l.Name != name {
good = append(good, l)
ref := n.links[:0]
found := false

for _, v := range n.links {
if v.Name != name {
ref = append(ref, v)
} else {
found = true
}
}
n.links = good

if !found {
return ipld.ErrNotFound
}

n.links = ref

return nil
}

Expand Down

0 comments on commit 319d880

Please sign in to comment.