From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on starla X-Spam-Level: X-Spam-Status: No, score=-1.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 Received: from nue.mailmanlists.eu (nue.mailmanlists.eu [IPv6:2a01:4f8:1c0c:6b10::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id A260C1F44D for ; Tue, 27 Feb 2024 16:42:59 +0000 (UTC) Received: from nue.mailmanlists.eu (localhost [127.0.0.1]) by nue.mailmanlists.eu (Postfix) with ESMTP id 882C87EF64; Tue, 27 Feb 2024 16:42:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ml.ruby-lang.org; s=mail; t=1709052172; bh=1GdhrAYPfHK7lxh//uNLEwKvZMR3RP6xstoTEEoHL8A=; h=Date:References:To:Reply-To:Subject:List-Id:List-Archive: List-Help:List-Owner:List-Post:List-Subscribe:List-Unsubscribe: From:Cc:From; b=BoyP38MLNLK/waCU++NZVFFxV9SP41Nz45S2YgJPCTszedk55qQ58rdVfCFaXUr8j d4aDYREB6ILoEaSWQu86V4Ni802xuA0SbMyP80fQPj+ncOPXXyblJ/wE+BoOG4i47m aNZ930dvFlKJWPSytk2lFBBLgnkkeTb3FGeyZsLI= Received: from s.wfbtzhsw.outbound-mail.sendgrid.net (s.wfbtzhsw.outbound-mail.sendgrid.net [159.183.224.105]) by nue.mailmanlists.eu (Postfix) with ESMTPS id 217E97EF50 for ; Tue, 27 Feb 2024 16:42:49 +0000 (UTC) Authentication-Results: nue.mailmanlists.eu; dkim=pass (2048-bit key; unprotected) header.d=ruby-lang.org header.i=@ruby-lang.org header.a=rsa-sha256 header.s=s1 header.b=Jzg5pnC0; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ruby-lang.org; h=from:references:subject:mime-version:content-type: content-transfer-encoding:list-id:to:cc:content-type:from:subject:to; s=s1; bh=BeVFhpeMoRI96JkSENQEJ+7xDu67nFzqxLc99cnimC8=; b=Jzg5pnC0Zj8v+ZFLoTQJ9PtD8krD89Q8hL1rodaBXqJY68PKcii7gwoFbPIdDDxKD53/ ObWSG9r4staw1/GRP0OnlT6z4AON/C5HqSX1n0IobPQmQKRnFGdZ8MTkiHhtbECzfYt30R SEVDrYLw/PujCtg70UAjonLXP7NM4i3cBJwqY7qhjwLaGyHPPu45+P44I2TOorWHKvncUW wFsx3Y/v6Klo349sKGsynpBswBaxy9q56m/ItfpBQHO+xYa4PkW3ilsRBsLzsLSu8DxvGg DOza5MXDIA6xlgnq0BwdV1aSmb86MGj4Yv56x4qOaP2VhJL6bc/z0gtVuMtA4b+Q== Received: by filterdrecv-canary-595b55ccbd-9dtgh with SMTP id filterdrecv-canary-595b55ccbd-9dtgh-1-65DE1107-25 2024-02-27 16:42:47.949792074 +0000 UTC m=+2859933.040707279 Received: from herokuapp.com (unknown) by geopod-ismtpd-15 (SG) with ESMTP id PJzcoMEKSEa0fN1NQt4Qcw for ; Tue, 27 Feb 2024 16:42:47.911 +0000 (UTC) Date: Tue, 27 Feb 2024 16:42:48 +0000 (UTC) Message-ID: References: Mime-Version: 1.0 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 20300 X-Redmine-Issue-Author: AMomchilov X-Redmine-Issue-Priority: Normal X-Redmine-Sender: matheusrich X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-Redmine-MailingListIntegration-Message-Ids: 93529 X-SG-EID: =?us-ascii?Q?3XqQPhAqyKBaCIJPVBiiUg=2FBJ=2Fawrd+4QUdyOQSMUIJiah9H8yd5K+V9IjPq3O?= =?us-ascii?Q?cuogg4GvmnLnkicsy6d9IWyDWROUWFyUhXhneAc?= =?us-ascii?Q?DDySwu1RoNOVCQLVLggwyCd8L9TSuKfOjJZnO6Y?= =?us-ascii?Q?Ku79uplyahYHnvAY3xiFhLeWnOPjg=2F68K7m6Deo?= =?us-ascii?Q?ridLO+12EIi7=2FIT3IX4g56hyoWcvCAbHxWj7ivO?= =?us-ascii?Q?zN0fBofArHCbEOYhPh9oZejZEB20CjOi2YaJR+e?= =?us-ascii?Q?XbFSRvpPqY9+QQMJAnE5MwhaVTIZjfFiijQDsLl?= =?us-ascii?Q?nYI=3D?= To: ruby-core@ml.ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== Message-ID-Hash: YRTFCEX4HWRIZY36UN2LX2E4UAR7WWOT X-Message-ID-Hash: YRTFCEX4HWRIZY36UN2LX2E4UAR7WWOT X-MailFrom: bounces+313651-b711-ruby-core=ml.ruby-lang.org@em5188.ruby-lang.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.3 Precedence: list Reply-To: Ruby developers Subject: [ruby-core:116973] [Ruby master Feature#20300] Hash: set value and get pre-existing value in one call List-Id: Ruby developers Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: "matheusrich (Matheus Richard) via ruby-core" Cc: "matheusrich (Matheus Richard)" Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Issue #20300 has been updated by matheusrich (Matheus Richard). Rust [calls this method `insert`](https://doc.rust-lang.org/std/collections/struct.HashMap.html#method.insert): > Inserts a key-value pair into the map. > > If the map did not have this key present, None is returned. > > If the map did have this key present, the value is updated, and the old value is returned. > > ```rs > let mut map = HashMap::new(); > assert_eq!(map.insert(37, "a"), None); > assert_eq!(map.is_empty(), false); > > map.insert(37, "b"); > assert_eq!(map.insert(37, "c"), Some("b")); > assert_eq!(map[&37], "c"); > ``` ---------------------------------------- Feature #20300: Hash: set value and get pre-existing value in one call https://bugs.ruby-lang.org/issues/20300#change-107020 * 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#update_value`, which has semantics are similar to this Ruby snippet: ```ruby class Hash # Exact method name TBD. def update_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.update_value(:k, "new value") use(old_value) ``` -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/