-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add Hash#put
and optimize Set#add?
#8116
Conversation
store is a bit of an overlapping name with "add" etc. hmm...maybe set_if_absent or something like that? |
I borrowed the name |
I think the most common name for the "insert into map" operation is This also brings me to the somewhat weird behavior of |
@jhass Great idea! I like the name I thought about having a That's why I think a single Maybe we can make it return Let's vote for a name:
|
I wouldn't mind |
@jhass Hm, good point. I though |
Looks like everyone's agreed on |
This lets you store a key-value pair and know whether an old value was present and what is it. We might be tempted to use a method that returns the value or `nil` if it wasn't already there but this doesn't work if values allow `nil` as a valid value. So `store` is in a way the equivalent of `fetch` but when storing a value.
6cd8286
to
e14da91
Compare
Hash#store
and optimize Set#add?
Hash#put
and optimize Set#add?
Renamed! If we merge we can squash the commits or I can rewrite the history, but maybe it's not bad to keep the commit about the decision to rename |
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 don't think wee need the history leading to the result. There is still a reference to the PR and the discussion behind it.
I'm having second thoughts about this, since I was confused by the name and thought this was a "put this value if it doesn't exist, else do nothing" call, which This method should probably be |
Java has |
* Add `Hash#store` This lets you store a key-value pair and know whether an old value was present and what is it. We might be tempted to use a method that returns the value or `nil` if it wasn't already there but this doesn't work if values allow `nil` as a valid value. So `store` is in a way the equivalent of `fetch` but when storing a value. * Set: optimize `add?` by avoiding a double hash lookup * Rename `Hash#store` to `Hash#put`
Right now
Set#add?
is implemented doing:That's a bit inefficient because there's a double hash lookup.
It would be cool to be be able to insert a key-value pair in a Hash and know if that was a new value or not (well, what
Set#add?
does) but that method doesn't exist. So here we introduce it asHash#store
.Then we rewrite
Set#add?
using that new method.Benchmark code:
Results:
Not a big difference but I still think it's bad if this can't be implemented efficiently, and I also think
Hash#put
might be a generally useful method because you can't do that efficiently right now.