-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Use Intersect to Narrow Iterate Range and Reduce Memory Allocation #9271
base: main
Are you sure you want to change the base?
Changes from all commits
3b23c92
25a8814
a5576a1
17d0349
2121ec2
5516de8
f25dfee
0d023b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1672,10 +1672,8 @@ func (l *List) Uids(opt ListOptions) (*pb.List, error) { | |
if opt.First == 0 { | ||
opt.First = math.MaxInt32 | ||
} | ||
// Pre-assign length to make it faster. | ||
l.RLock() | ||
// Use approximate length for initial capacity. | ||
res := make([]uint64, 0, l.mutationMap.len()+codec.ApproxLen(l.plist.Pack)) | ||
|
||
out := &pb.List{} | ||
if l.mutationMap.len() == 0 && opt.Intersect != nil && len(l.plist.Splits) == 0 { | ||
if opt.ReadTs < l.minTs { | ||
|
@@ -1687,16 +1685,62 @@ func (l *List) Uids(opt ListOptions) (*pb.List, error) { | |
return out, nil | ||
} | ||
|
||
absFirst := opt.First | ||
if opt.First < 0 { | ||
absFirst = -opt.First | ||
} | ||
preAllowcateLength := min(absFirst, l.mutationMap.len()+codec.ApproxLen(l.plist.Pack)) | ||
if opt.Intersect != nil { | ||
preAllowcateLength = min(preAllowcateLength, len(opt.Intersect.Uids)) | ||
} | ||
// Pre-assign length to make it faster. | ||
res := make([]uint64, 0, preAllowcateLength) | ||
|
||
checkLimit := func() bool { | ||
// We need the last N. | ||
// TODO: This could be optimized by only considering some of the last UidBlocks. | ||
if opt.First < 0 { | ||
if len(res) > -opt.First { | ||
res = res[1:] | ||
} | ||
} else if len(res) > opt.First { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
if opt.Intersect != nil && len(opt.Intersect.Uids) < l.mutationMap.len()+codec.ApproxLen(l.plist.Pack) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same: use exact length. |
||
start := 0 | ||
if opt.AfterUid > 0 { | ||
start = sort.Search(len(opt.Intersect.Uids), func(i int) bool { | ||
return opt.Intersect.Uids[i] > opt.AfterUid | ||
}) | ||
} | ||
|
||
for i := start; i < len(opt.Intersect.Uids); i++ { | ||
uid := opt.Intersect.Uids[i] | ||
|
||
found, _, err := l.findPosting(opt.ReadTs, uid) | ||
if err != nil { | ||
l.RUnlock() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of manually calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When implementing this code, I also considered making this change, but I referred to the previous implementation and believe it was designed to minimize the time spent holding the read lock. Therefore, I chose not to change this implementation. |
||
return out, errors.Wrapf(err, "While find posting for UIDs") | ||
} | ||
if found { | ||
res = append(res, uid) | ||
if checkLimit() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of checking for negative first here like this, why not just check the ids in reverse. |
||
break | ||
} | ||
} | ||
} | ||
out.Uids = res | ||
l.RUnlock() | ||
return out, nil | ||
} | ||
|
||
err := l.iterate(opt.ReadTs, opt.AfterUid, func(p *pb.Posting) error { | ||
if p.PostingType == pb.Posting_REF { | ||
res = append(res, p.Uid) | ||
if opt.First < 0 { | ||
// We need the last N. | ||
// TODO: This could be optimized by only considering some of the last UidBlocks. | ||
if len(res) > -opt.First { | ||
res = res[1:] | ||
} | ||
} else if len(res) > opt.First { | ||
if checkLimit() { | ||
return ErrStopIteration | ||
} | ||
} | ||
|
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 think we have a function for it (getting approx len)
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.
Also the actual length could be way more for some postings (split postings). Lets use the exact length function here. It would only hurt in case of split postings. We can resolve that later.