Issue #20300 has been updated by AMomchilov (Alexander Momchilov).
mame (Yusuke Endoh) wrote in #note-24:
It was originally intended as a method to improve the
efficiency of `Set#add?`, but the use case was shifted to a method for thread safety. This
history makes the use case less persuasive.
The immediate need and use case is for that performance improvement to Set.
The discussion of thread safety is just an incidental benefit of this approach, as
compared to the count-based alternative that was proposed.
If it is just an internal method for efficiency, a
long and verbose name may be preferred.
I would be okay with this, if there’s no appetite for a public, short-named API for this.
Perhaps some underscored name that communicates that this is “library internal” to the
stdlib.
You may want to explain the concrete example of the
use case of thread safety for Hash value exchange, this proposed API is really sufficient
for that example use case,
I don’t think I can make a better case than the concrete implementation I’ve already
proposed, and its immediate application to the Set performance problem I’ve identified and
fixed, using this API.
If others want to chime in with the use cases in they have in mind, I’d love to hear
those.
and what API and name are given to similar feature in
other languages.
Let’s first decide if we want this API, in *any* form. Once we agree on that, I’d be happy
to dive into details like naming, and comparisons to other languages.
----------------------------------------
Feature #20300: Hash: set value and get pre-existing value in one call
https://bugs.ruby-lang.org/issues/20300#change-108239
* Author: AMomchilov (Alexander Momchilov)
* Status: Open
----------------------------------------
When using a Hash, sometimes you want to set a new value, **and** see what was already
there. Today, you **have** to do this in two steps:
```ruby
h = { k: "old value" }
# 1. Do a look-up for `:k`.
old_value = h[:k]
# 2. Do another look-up for `:k`, even though we just did that!
h[:k] = "new value"
use(old_value)
```
This requires two separate `Hash` look-ups for `:k`. This is fine for symbols, but is
expensive if computing `#hash` or `#eql?` is expensive for the key. It's impossible to
work around this today from pure Ruby code.
One example use case is `Set#add?`. See
https://bugs.ruby-lang.org/issues/20301 for more
details.
I propose adding `Hash#exchange_value`, which has semantics are similar to this Ruby
snippet:
```ruby
class Hash
# Exact method name TBD.
def exchange_value(key, new_value)
old_value = self[key]
self[key] = new_value
old_value
end
end
```
... except it'll be implemented in C, with modifications to `tbl_update` that achieves
this with a hash-lookup.
I'm opening to alternative name suggestions. @nobu came up with `exchange_value`,
which I think is great.
Here's a PR with a PoC implementation:
https://github.com/ruby/ruby/pull/10092
```ruby
h = { k: "old value" }
# Does only a single hash look-up
old_value = h.exchange_value(:k, "new value")
use(old_value)
```
--
https://bugs.ruby-lang.org/