-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pass cids instead of nodes around in EnumerateChildrenAsync #3598
Conversation
Fixes #3588 |
merkledag/merkledag.go
Outdated
} | ||
|
||
if next == nil { | ||
next = nc.Node | ||
send = feed | ||
if len(n.Links()) > 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be cleaned up a little bit, Lets add all the new links to the array first, then check for next being nil and handle that case separately.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this approach better than mine. One small comment, and then i'd also like @Kubuxu to review.
merkledag/merkledag.go
Outdated
send = feed | ||
if len(n.Links()) > 0 { | ||
next = n.Links()[0].Cid | ||
for _, l := range n.Links()[1:] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be beneficial to: preallocate array for Cids in .Links()
call, write the Cids to that array and do just one append call.
This way the todobuffer will be expanded just once instead of N times.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general this isnt an issue in go, slices will optmistically allocate extra space under the hood for you, so sequential appends are generally just as performant as preallocating.
merkledag/merkledag.go
Outdated
} else { | ||
todobuffer = append(todobuffer, nc.Node) | ||
for _, l := range n.Links() { | ||
todobuffer = append(todobuffer, l.Cid) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same (https://github.com/ipfs/go-ipfs/pull/3598/files#r96632953) applies here.
The allocation of the additional array will probably be removed by optimization step in golang.
c978321
to
71da359
Compare
Addressed both reviews. |
Can you also change the base of the PR to master, as the branch you are currently pointing to was merged already. |
done |
merkledag/merkledag.go
Outdated
feed := make(chan node.Node) | ||
out := make(chan *NodeOption) | ||
feed := make(chan *cid.Cid) | ||
out := make(chan *cid.Cid) | ||
done := make(chan struct{}) | ||
|
||
var setlk sync.Mutex | ||
|
||
for i := 0; i < FetchGraphConcurrency; i++ { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we spawning multiple of these goroutines? Looking at it, we should only need one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you mean FetchGraphConcurrency ones? You're right, looks like I accidentally removed concurrency altogether :) Fixed version incoming
merkledag/merkledag.go
Outdated
todobuffer = append(todobuffer, nc.Node) | ||
links := n.Links() | ||
if len(links) > 0 { | ||
cids := make([]*cid.Cid, len(links), len(links)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to specify capacity if youre already specifying length to be the same
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, you can rewrite this whole loop to avoid allocating an extra slice. Just append to the todobuffer
71da359
to
a63b4c2
Compare
Modified PR to actually be concurrent. Is second changes' request obsolete now? |
merkledag/merkledag.go
Outdated
|
||
if unseen { | ||
cids := make([]*cid.Cid, len(n.Links())) | ||
for i, l := range n.Links() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems unecessary, why not just send the node down through out?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my worry is that its an extra allocation per node that we could avoid by simply moving this for loop down into the processing below.
merkledag/merkledag.go
Outdated
send = feed | ||
} else { | ||
todobuffer = append(todobuffer, nc.Node) | ||
case cids := <-out: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if out was changed to be a channel of nodes, this could look like:
case nd := <-out:
for _, lnk := range nd.Links() {
if next == nil {
next = lnk.Cid
send = feed
continue
}
todobuffer = append(todobuffer, lnk.Cid)
}
|
||
|
||
errChan := make(chan error) | ||
fetchersCtx, cancel := context.WithCancel(ctx) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
put a defer cancel()
after this call and then remove the other call youre making to cancel
License: MIT Signed-off-by: Iaroslav Gridin <[email protected]>
License: MIT Signed-off-by: Iaroslav Gridin <[email protected]>
a63b4c2
to
6618932
Compare
Implemented suggested changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, Tested locally and can confirm its much faster already :)
No idea why this was changed this was introduced in: 08f342e (part of #3598) License: MIT Signed-off-by: Steven Allen <[email protected]>
Pass cids instead of nodes around in EnumerateChildrenAsync
No idea why this was changed this was introduced in: 08f342e (part of ipfs#3598) License: MIT Signed-off-by: Steven Allen <[email protected]>
Saves a whole lot of RAM on todobuffer