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=0.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_BL_SPAMCOP_NET,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no 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) server-digest SHA256) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 7266A1F44D for ; Tue, 27 Feb 2024 21:30:55 +0000 (UTC) Received: from nue.mailmanlists.eu (localhost [127.0.0.1]) by nue.mailmanlists.eu (Postfix) with ESMTP id 594AC7EF62; Tue, 27 Feb 2024 21:30:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ml.ruby-lang.org; s=mail; t=1709069447; bh=u7OXxiz/gnxOavoZnjAohy0lorhGyu0K5kLimLF8qdA=; 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=HXstQ1gc69ZryrAcGc5dWTe8DnHWqSqhtt31GF3MHu6OAbSYYWlme/JSUPJU1j+uW +8fWap5ZxuMH3FVxqP2opaiz819OJ/FmWEWwLPsBC+C12RLpwyej/leZtPObKPdtx1 aE6kbzikksI84APxx8ykgN+p4sb4iRlO2DXRJISY= Received: from s.wrqvtbkv.outbound-mail.sendgrid.net (s.wrqvtbkv.outbound-mail.sendgrid.net [149.72.123.24]) by nue.mailmanlists.eu (Postfix) with ESMTPS id 37DE87D5D2 for ; Tue, 27 Feb 2024 21:30:43 +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=kfe0AtVi; 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=t7TeEv/BIlX8/Ppzb0ac594/SD1a032gJmio4DcWFmI=; b=kfe0AtVicMz+SDq7T2QVW4vBDfAnJnODvNNFCI5zCtnldyiYtMjOIMuTmKEBHMlmfNoq 5MAX6S23XMmkLlozFqBPjtKxuKYR9PW5/g9lyYbZ+lIX3H51bayG2dGm9EULnMSPI8Ft0c wyAJiwTd6ir2Tm+lXGa4fYlMvYOZFbBogV9AWR1qLVdp2ejNKLVEoNZXfKbZQu7YbDJ6nH q7uffzJSepQXQDmggZ+Gi85g6ycW8HwlJ6FSN10IJwMfifhxNvXJW4SCneOve3dStxD9yI t9dK1NazpfOr2GEs0GAmA91xHYxrR8XKdfKpw7fTIB4c3ZCv2mgPMBnfsXrvLhJg== Received: by recvd-767c65dbff-5kxdr with SMTP id recvd-767c65dbff-5kxdr-1-65DE5481-2F 2024-02-27 21:30:41.990022858 +0000 UTC m=+436505.838649922 Received: from herokuapp.com (unknown) by geopod-ismtpd-7 (SG) with ESMTP id EX7EjDCjTZ2RLWBSonUnew for ; Tue, 27 Feb 2024 21:30:41.870 +0000 (UTC) Date: Tue, 27 Feb 2024 21:30:42 +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: AMomchilov 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: 93537 X-SG-EID: =?us-ascii?Q?TXH2usWcWp=2FzFcOxYN6Di8zjS0LJPVcRDxMs1qy9L7d4+k8AQ49BDHpIjwSVMz?= =?us-ascii?Q?9YntVpiMPH+tfADRBOhubj+a4G1rD7MQwpbBSUi?= =?us-ascii?Q?RNRHcFVS5XZ9gTeQ=2FkTK5TbkR5mCl=2FYqtceLDSZ?= =?us-ascii?Q?xdfy9s94+JNJX3OKCLZ3PSPW73L6LWNTJskqbpT?= =?us-ascii?Q?lq5XBWdm=2FVC0AmueQ=2FEkjOmTZ=2FSLTyRsbyvi+jK?= =?us-ascii?Q?+S89IN03GANunlrrYEfeZN8MXgRLWLK6mHE2qJi?= =?us-ascii?Q?NVMFXwJO0yS2fmxcXGtTQ=3D=3D?= To: ruby-core@ml.ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== Message-ID-Hash: DZIXNYBD6KNDPZDEZW2G623XAGVE64GX X-Message-ID-Hash: DZIXNYBD6KNDPZDEZW2G623XAGVE64GX 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:116981] [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: "AMomchilov (Alexander Momchilov) via ruby-core" Cc: "AMomchilov (Alexander Momchilov)" Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Issue #20300 has been updated by AMomchilov (Alexander Momchilov). @matheusrich Thanks for putting together that list! In general, I like the idea of having this method support an optional block, but we would need to benchmark it to ensure it doesn't have an undesired slow down in the `exchange_value(:key, :new_value)` case. Really, this API's main goal is performance (since it doesn't do anything you couldn't already do with a combination of `#has_key?`, `#[]` and `#[]=`), so if an optional block arg slows that down, it would entirely defeat its benefit. ---------------------------------------- Feature #20300: Hash: set value and get pre-existing value in one call https://bugs.ruby-lang.org/issues/20300#change-107029 * 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/