"pin verify": don't use a pointer to a slice.
License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>
Kevin Atkinson committed
Mar 29, 2017 at 17:44 UTC
6e898ea678f1483a403088363dfb6f8e98f5ecca
1 file changed
+6
-23
core/commands/pin.go
+6
-23
@@ -515,9 +515,7 @@ func pinLsAll(typeStr string, ctx context.Context, n *core.IpfsNode) (map[string
515
}
516
517
type pinStatus struct {
518
- // use pointer to array slice to reduce memory usage
519
- // the value should not be nil unless uninitialized
520
- badNodes *[]badNode
518
+ badNodes []badNode
519
}
520
521
type badNode struct {
@@ -533,44 +531,29 @@ type pinVerifyRes struct {
531
func pinVerify(ctx context.Context, n *core.IpfsNode) <-chan pinVerifyRes {
532
visited := make(map[string]pinStatus)
533
getLinks := n.DAG.GetOfflineLinkService().GetLinks
536
- emptySlice := &[]badNode{}
534
recPins := n.Pinning.RecursiveKeys()
535
536
var checkPin func(root *cid.Cid) pinStatus
537
checkPin = func(root *cid.Cid) pinStatus {
538
key := root.String()
539
if status, ok := visited[key]; ok {
543
- if status.badNodes == nil {
544
- return pinStatus{&[]badNode{badNode{
545
- cid: root,
546
- err: fmt.Errorf("Cycle Detected.")}}}
547
- }
540
return status
541
}
542
543
links, err := getLinks(ctx, root)
544
if err != nil {
553
- status := pinStatus{&[]badNode{badNode{cid: root, err: err}}}
545
+ status := pinStatus{[]badNode{badNode{cid: root, err: err}}}
546
visited[key] = status
547
return status
548
}
549
550
status := pinStatus{}
559
- visited[key] = status // paranoid mode cycle detection
551
for _, lnk := range links {
552
res := checkPin(lnk.Cid)
562
- if len(*res.badNodes) > 0 {
563
- if status.badNodes == nil {
564
- status.badNodes = res.badNodes
565
- } else {
566
- slice := append(*status.badNodes, *res.badNodes...)
567
- status.badNodes = &slice
568
- }
553
+ if len(res.badNodes) > 0 {
554
+ status.badNodes = append(status.badNodes, res.badNodes...)
555
}
556
}
571
- if status.badNodes == nil {
572
- status.badNodes = emptySlice // prevent special cases
573
- }
557
558
visited[key] = status
559
return status
@@ -589,11 +572,11 @@ func pinVerify(ctx context.Context, n *core.IpfsNode) <-chan pinVerifyRes {
572
}
573
574
func (r pinVerifyRes) Format(out io.Writer) {
592
- if len(*r.badNodes) == 0 {
575
+ if len(r.badNodes) == 0 {
576
fmt.Fprintf(out, "%s ok\n", r.cid)
577
} else {
578
fmt.Fprintf(out, "%s broken\n", r.cid)
596
- for _, e := range *r.badNodes {
579
+ for _, e := range r.badNodes {
580
fmt.Fprintf(out, " %s: %s\n", e.cid, e.err)
581
}
582
}