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 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)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id A8A4A1F44D for ; Wed, 17 Apr 2024 10:57:21 +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=DK9XuElh; 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=SNobm1hM; dkim-atps=neutral Received: from nue.mailmanlists.eu (localhost [127.0.0.1]) by nue.mailmanlists.eu (Postfix) with ESMTP id B8F91843D0; Wed, 17 Apr 2024 10:57:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ml.ruby-lang.org; s=mail; t=1713351433; bh=dqNmqyrXwZuiyMmRHktngiC8NjZa8khegbigsiTDXjc=; 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=DK9XuElhnOSLgaIDMM4OfaGHmyN3surP8sMCJoS1T7yNEcY1UUTffisdrowwMC/t2 QG3nc/wj8p9tzylRQmkBqTFK8ra7o5K6n87ou+vThzjGLQDnAxVJnOtmHXKvMQJMAf sXb2+qW8Kq2q+7AjJfLT307eaK/KJaAi6GXtdo0I= 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 1DFB1843D1 for ; Wed, 17 Apr 2024 10:57:10 +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=SNobm1hM; 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=uJZaDZ0uFD7ozly2dIRBdym3147/xNqBGGSK5TBUJmY=; b=SNobm1hM2t/LKpIUpfIqKF/84T+LWrLVm2RzihYXJiESKVSv9UBQlRrFwUfWeCsiSm/m 6y+x0ChYDiLrSjuTpZXLVd3tbnhW5LXI6Y7oRCsZB1xRWNfEOCzVcWjyPNTLvzCzAh2jYD OVhWLIvhZghzyIez/hYwb15qquwzENh79DbwxTTcAOfrrxcS15DbEx8oTtcsG5uSJmYZCU ko9nJkxN1UNWy5w9Wbgb2AZR3ty0O0FFwrDLx0QZ7ESbT6Qb2HiccmNZ25RFR0nhdTBo/1 mFkOdlI5oT+WPPU8IaRA6Kbvkoj9z/X7tU8qbVijzbM5zco6QStMuqeR0y9AiFDw== Received: by recvd-547d99795c-zvjzv with SMTP id recvd-547d99795c-zvjzv-1-661FAB05-1 2024-04-17 10:57:09.001239574 +0000 UTC m=+392159.872718106 Received: from herokuapp.com (unknown) by geopod-ismtpd-10 (SG) with ESMTP id e947LhqjQmaXZ3irXR5thA for ; Wed, 17 Apr 2024 10:57:08.958 +0000 (UTC) Date: Wed, 17 Apr 2024 10:57:09 +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: mame 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: 94156 X-SG-EID: =?us-ascii?Q?u001=2Ep+ckLDtT+4Y5c+H0YCkEnsuWiCQmn3OZA=2F9FzjoR6ZZlPaMv54M7EFoSM?= =?us-ascii?Q?CX5Trc79ep2R5F+0oYS4n23jq1cvzkTMjex5qCq?= =?us-ascii?Q?7xdQ0mTNFZ8buxwtDTdzcmEKR9oF92AasSahnAp?= =?us-ascii?Q?97piK7jf2MVSi1xywBFxcE9GREaHNvFZ7Wt5nYc?= =?us-ascii?Q?nCRM7FOzjfcoGt4pvvfdjG=2FFKAlt9AT+siSjgCF?= =?us-ascii?Q?pu+6Iug4q7LiTeqGWp0MHl7uIJ1Ky8TSSkxBsS8?= =?us-ascii?Q?vo8=2F?= To: ruby-core@ml.ruby-lang.org X-Entity-ID: u001.I8uzylDtAfgbeCOeLBYDww== Message-ID-Hash: YM3AJ5LV5JJN53WZNQ757PQ4MTWQH3NM X-Message-ID-Hash: YM3AJ5LV5JJN53WZNQ757PQ4MTWQH3NM 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:117560] [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: "mame (Yusuke Endoh) via ruby-core" Cc: "mame (Yusuke Endoh)" Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Issue #20300 has been updated by mame (Yusuke Endoh). @matz said he was not sure what a name is good for this method because its true motivation is unclear. 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. If the main purpose is thread safety, we want to respect the terminology of the parallel computing area. If it is just an internal method for efficiency, a long and verbose name may be preferred. (If it is a daily-use method, a short and convenient name may be preferred.) 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, and what API and name are given to similar feature in other languages. ---------------------------------------- Feature #20300: Hash: set value and get pre-existing value in one call https://bugs.ruby-lang.org/issues/20300#change-107959 * 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/