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 [94.130.110.93]) (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 682D61F44D for ; Fri, 15 Mar 2024 02:46:28 +0000 (UTC) Authentication-Results: dcvr.yhbt.net; dkim=pass (1024-bit key; secure) header.d=ml.ruby-lang.org header.i=@ml.ruby-lang.org header.a=rsa-sha256 header.s=mail header.b=pERqdNbc; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=ruby-lang.org header.i=@ruby-lang.org header.a=rsa-sha256 header.s=s1 header.b=gZci2Ten; dkim-atps=neutral Received: from nue.mailmanlists.eu (localhost [127.0.0.1]) by nue.mailmanlists.eu (Postfix) with ESMTP id 6698F832F6; Fri, 15 Mar 2024 02:46:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ml.ruby-lang.org; s=mail; t=1710470778; bh=zjrDXiXE4aKTnjdtd1INH/Oruihk0sA174eD9HhZ7wA=; 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=pERqdNbcOoxNcTWs1/ZC+hTpP0ErNY5P3CZMDt+sowuxp5SljGB80miI3JMFdadSM 5yobwhFZD9yp7nfINh7hx+ZLHXQ1+pR86m/jzWggzXS8Y9oASGvcrm0BIN2k4h4+fC 2q+VAkhf2Cf2F3HeABUQcDH1EW9ituJnwPr+RUfg= Received: from s.wfbtzhsv.outbound-mail.sendgrid.net (s.wfbtzhsv.outbound-mail.sendgrid.net [159.183.224.104]) by nue.mailmanlists.eu (Postfix) with ESMTPS id 59731832D3 for ; Fri, 15 Mar 2024 02:46:14 +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=gZci2Ten; 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=/BySGFePPFJiI9c0oJDV2+19qYhAcOpStcKLBgmWSK8=; b=gZci2TenAL22E02rIQ6Ga2QgNvLn4iG1+DBk3aCBiM1FlkFdu1L+7T2L4rpWRLh2MvlD pHgQve2emZBlvVkMMJkEYKpyMEeSohCVQS/3nk3kqqohttT5dsNR+N0dDrmbV7mPi96YOx Rsua36OqBQcMlU/qM7WtwNxQBguUeI0+JyT1TypdmhinwE8tOW2FtlQ9kM64MYlwWX909m VV2QSNig/dLmggiAuuTaV7fnBbXiO4srI+Fl7X7LhhaGNehJhMi88AWdJzom4YW2+LDdW8 R4uGkNMAeHwbb4uvnilfB56Se2JghFkieOri4v2HoZUscqKAAhgnfDRW5u27BKLQ== Received: by recvd-7ffbd89f59-2pj9c with SMTP id recvd-7ffbd89f59-2pj9c-1-65F3B674-E 2024-03-15 02:46:12.34685465 +0000 UTC m=+1313779.227545582 Received: from herokuapp.com (unknown) by geopod-ismtpd-9 (SG) with ESMTP id rRn6EOMAQe6gi6hnCvUlgg for ; Fri, 15 Mar 2024 02:46:12.207 +0000 (UTC) Date: Fri, 15 Mar 2024 02:46:12 +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: shyouhei 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: 93760 X-SG-EID: =?us-ascii?Q?u001=2EYb+gxajettXtIRRbovldwe1q9pj4BbUVLgkglfGACSriEdKOm=2FiYoqOLj?= =?us-ascii?Q?03lPXujlro4oiTXwkRMLtmbf5mY+2Uv33JJiwU+?= =?us-ascii?Q?ZJeKxsg2=2FpbHuH5pX5XHk87Mue9pBqGS1QSX04a?= =?us-ascii?Q?yVEozrccpDJTic2uEqjE3HbeughBYmryHwclMUf?= =?us-ascii?Q?S5kPWhrfD9lH72Egc7HaQDiAUGhvSsxj0Eep4hp?= =?us-ascii?Q?88tUKx3CXIyWFXgdIB2qWuebfnFtH13T87ILefU?= =?us-ascii?Q?xx2c4v4gznOlHnB5VurWZg8nnQ=3D=3D?= To: ruby-core@ml.ruby-lang.org X-Entity-ID: u001.I8uzylDtAfgbeCOeLBYDww== Message-ID-Hash: IGL2MMJGUHGPTNJCMZTBQZJQ3KZUB6SU X-Message-ID-Hash: IGL2MMJGUHGPTNJCMZTBQZJQ3KZUB6SU 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:117194] [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: "shyouhei (Shyouhei Urabe) via ruby-core" Cc: "shyouhei (Shyouhei Urabe)" Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Issue #20300 has been updated by shyouhei (Shyouhei Urabe). I'm neutral to this particular method (could be handy). If thread safety is in mind perhaps atomic compare-and-exchange could be better than just exchange, though. ---------------------------------------- Feature #20300: Hash: set value and get pre-existing value in one call https://bugs.ruby-lang.org/issues/20300#change-107283 * 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/ ______________________________________________ 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/