Avoid allocating channels unnecessarily on map inserts#3246
Avoid allocating channels unnecessarily on map inserts#3246peterebden merged 5 commits intothought-machine:masterfrom
Conversation
| s.m[key] = awaitableValue[V]{Val: f()} | ||
| close(existing.Wait) | ||
| existing.Wait = nil | ||
| return existing.Val, true |
There was a problem hiding this comment.
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.
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.
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).
toastwaffle
left a comment
There was a problem hiding this comment.
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: