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) server-digest SHA256) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id B5DA11F44D for ; Mon, 26 Feb 2024 02:42:45 +0000 (UTC) Received: from nue.mailmanlists.eu (localhost [127.0.0.1]) by nue.mailmanlists.eu (Postfix) with ESMTP id 68A2982C0B; Mon, 26 Feb 2024 02:42:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ml.ruby-lang.org; s=mail; t=1708915355; bh=iRO1LT1hppTfJEzf9vtNlnDNjLWLtkVSZJ8L9G7B100=; 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=MTcngYv2+KRcC87ALqZgzFv66IvfJS/vgfYllRuHssgIkAvBc0fa6ud5uDDR5yRRB xRkNWMfJF+L4RuNAcSVLFh+2VKRNOtZFEA984GEcyCKH0ENFYH8l9imuW6hKHbkRtV 6dQAsQ0x+miOl9SvWT+CdyoQmkFXQYyV8DJXIOiE= Received: from s.csnrwnwx.outbound-mail.sendgrid.net (s.csnrwnwx.outbound-mail.sendgrid.net [198.37.146.154]) by nue.mailmanlists.eu (Postfix) with ESMTPS id E9C7282BEF for ; Mon, 26 Feb 2024 02:42:31 +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=L4dd0gAQ; 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=sfz/V4ETVXQpMnD9piwkT2Bfb83hxR42DNu4hY2JbOs=; b=L4dd0gAQ546Ez01DFRjBGuJY7il0BTRsLaWO3saEKF7EinGsVQkJdiyFwtn9H4cW86ah gNewL8TE2loDuhAQ2/fnBMEqM68AYiP1NpUhgK3s+9wYFv0xvfDtdiYFYZIg/jcBIBCbFB wCS0qY9IwHVPynKh59HMeKqbgiIUvVN7FnmG5aTCxPU9a1grseKv+1VoICkVezGlRjXDBy h5OBei5DQNMwMsT8ixWnHwfywYumEXuwx3HG4OCfdMkuStH2ZmwXCfaGKN5Mo2XYijrsnm ShR6naBcIeFtgAwIkuOAitFvFBmO0RJjTEd4tZIoQUG9IhmMe8adM1G7CGPMwENw== Received: by filterdrecv-5bbdbb56cd-hp2wm with SMTP id filterdrecv-5bbdbb56cd-hp2wm-1-65DBFA95-15 2024-02-26 02:42:29.83731384 +0000 UTC m=+1491943.495259379 Received: from herokuapp.com (unknown) by geopod-ismtpd-23 (SG) with ESMTP id xnq5Q2OVSkWhdifm_wWxRA for ; Mon, 26 Feb 2024 02:42:29.750 +0000 (UTC) Date: Mon, 26 Feb 2024 02:42:29 +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: 93496 X-SG-EID: =?us-ascii?Q?TXH2usWcWp=2FzFcOxYN6Di8zjS0LJPVcRDxMs1qy9L7d4+k8AQ49BDHpIjwSVMz?= =?us-ascii?Q?9YntVpiMPH+tfADRBOhubj+a4G1rD7MQwpbBSUi?= =?us-ascii?Q?RNRHcHdHtKCadKQ7MT6IDq6xeleGcNp5Ms=2F6F8t?= =?us-ascii?Q?MAI3fi8G4bsPTV4wkFQB8KsA7r1YRsD+iZ5DfAQ?= =?us-ascii?Q?Uv05zjXDjlNDeo4OjJWf+HFS1OJmfSzBVfwGChn?= =?us-ascii?Q?PdQn1Ah7=2FW1J4pwG1FeZ2MisORCW66lvh5TAzR8?= =?us-ascii?Q?LdZ8kjb56DTHafFItjj3THqzQhssaJ0n4C2=2F8vM?= =?us-ascii?Q?3rA=3D?= To: ruby-core@ml.ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== Message-ID-Hash: IYSVN3RDHF5EB2DAIFQMIBJHC5CIQUGJ X-Message-ID-Hash: IYSVN3RDHF5EB2DAIFQMIBJHC5CIQUGJ 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:116940] [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 reported by AMomchilov (Alexander Momchilov). ---------------------------------------- Feature #20300: Hash: set value and get pre-existing value in one call https://bugs.ruby-lang.org/issues/20300 * 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. I propose adding `Hash#update_value`, which has semantics are similar to this Ruby snippet: ```ruby class Hash 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. Here's a PR with a PoC implementation: https://github.com/ruby/ruby/pull/10092 -- 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/