-
Notifications
You must be signed in to change notification settings - Fork 207
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
Avoid allocating channels unnecessarily on map inserts #3246
Avoid allocating channels unnecessarily on map inserts #3246
Conversation
src/cmap/cmap.go
Outdated
s.m[key] = awaitableValue[V]{Val: f()} | ||
close(existing.Wait) | ||
existing.Wait = nil | ||
return existing.Val, true |
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.
Should this be s.m[key].Val
? IIUC, existing.Val
will be zero if existing.Wait
is non-nil.
It looks like the existing tests don't actually check the return value of Set
(and I question whether it actually needs to return the value at all)
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.
Set
was supposed to return the old value; there'd be no point in returning s.m[key].Val
there since that's what the caller supplied.
I think you're right that nothing actually cares about this any more though, so maybe it can all just be simplified 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.
Okay right, I've removed it from Set where it was no longer needed. This now returns whatever is set now which is more useful (the callers of it never look at the value in that code path but it still makes more sense this way).
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.
plz fix lint, otherwise lgtm
ce80d36
to
05abb37
Compare
We don't always insert, but we were always allocating a new slice just in case we did. This makes it lazy.
I don't love duplicating the Set function but I don't see a great alternative (it also hurts a bit to add the extra non-inlineable function call on every insert when we do have a value).
Before:
After: