-
Notifications
You must be signed in to change notification settings - Fork 645
Improve *deduplicatingSlice.ingest
performance
#4037
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 | ||||
---|---|---|---|---|---|---|
|
@@ -254,7 +254,7 @@ func (s *deduplicatingSlice[M, K, H]) Size() uint64 { | |||||
|
||||||
func (s *deduplicatingSlice[M, K, H]) ingest(elems []M, rewriter *rewriter) { | ||||||
var ( | ||||||
rewritingMap = make(map[int64]int64) | ||||||
rewritingMap = make(map[int64]int64, len(elems)) | ||||||
missing = int64SlicePool.Get() | ||||||
) | ||||||
missing = missing[:0] | ||||||
|
@@ -279,6 +279,7 @@ func (s *deduplicatingSlice[M, K, H]) ingest(elems []M, rewriter *rewriter) { | |||||
if len(missing) > 0 { | ||||||
s.lock.Lock() | ||||||
posSlice := int64(len(s.slice)) | ||||||
misSlice := make([]M, 0, len(missing)) | ||||||
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. Rather than using the extra slice you can also just grow the underlying slice, you might overgrow it but you can get rid of misSlice entirely.
Suggested change
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. Ah, that's probably even better! I'll refactor to use this and see what the benchmark says. 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. Did you mean
Surprisingly, growing 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. No |
||||||
for _, pos := range missing { | ||||||
// check again if element exists | ||||||
k := s.helper.key(elems[pos]) | ||||||
|
@@ -288,12 +289,13 @@ func (s *deduplicatingSlice[M, K, H]) ingest(elems []M, rewriter *rewriter) { | |||||
} | ||||||
|
||||||
// add element to slice/map | ||||||
s.slice = append(s.slice, s.helper.clone(elems[pos])) | ||||||
misSlice = append(misSlice, s.helper.clone(elems[pos])) | ||||||
s.lookup[k] = posSlice | ||||||
rewritingMap[int64(s.helper.setID(uint64(pos), uint64(posSlice), &elems[pos]))] = posSlice | ||||||
posSlice++ | ||||||
s.size.Add(s.helper.size(elems[pos])) | ||||||
} | ||||||
s.slice = append(s.slice, misSlice...) | ||||||
s.lock.Unlock() | ||||||
} | ||||||
|
||||||
|
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.
Good find, this will give us a right sized map every time.