Skip to content
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

blockservice: optimize getBlocks filter by not rescanning the already valid leading elements #558

Merged
1 commit merged into from
Jan 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions blockservice/blockservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,18 +321,19 @@ func getBlocks(ctx context.Context, ks []cid.Cid, blockservice BlockService, fet

allowlist := grabAllowlistFromBlockservice(blockservice)

allValid := true
for _, c := range ks {
var lastAllValidIndex int
var c cid.Cid
for lastAllValidIndex, c = range ks {
if err := verifcid.ValidateCid(allowlist, c); err != nil {
allValid = false
break
}
}

if !allValid {
if lastAllValidIndex != len(ks) {
// can't shift in place because we don't want to clobber callers.
ks2 := make([]cid.Cid, 0, len(ks))
for _, c := range ks {
ks2 := make([]cid.Cid, lastAllValidIndex, len(ks))
copy(ks2, ks[:lastAllValidIndex]) // fast path for already filtered elements
for _, c := range ks[lastAllValidIndex:] { // don't rescan already scanned elements
// hash security
if err := verifcid.ValidateCid(allowlist, c); err == nil {
ks2 = append(ks2, c)
Expand Down
Loading