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,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_PASS,SPF_PASS 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)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 63C211F549 for ; Fri, 9 May 2025 03:19:08 +0000 (UTC) Authentication-Results: dcvr.yhbt.net; dkim=pass (1024-bit key; unprotected) header.d=ml.ruby-lang.org header.i=@ml.ruby-lang.org header.a=rsa-sha256 header.s=mail header.b=HoptB33k; 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=uHqHHwnn; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ml.ruby-lang.org; s=mail; t=1746760715; bh=j1fwoNIx23vIapgUFwrjexFq1UUvsPPYOcFy1jqcKaA=; 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=HoptB33kkwIr29xeu8uW4UsFmGVgGNtG+ImaOEXxVpxM/N7jkfXgqinN85EPrJDQc /2kbCxjizHIJ5BARcXh+ElEcjSOFZ5ZbLgtjq3fadq1XUYTVZdpqCt9rQRq7hNblg1 7/If86QH5DL9Gq6/buSaC1BsBGH1iTOh8Zca1KU0= Received: from nue.mailmanlists.eu (localhost [IPv6:::1]) by nue.mailmanlists.eu (Postfix) with ESMTP id 435C347480 for ; Fri, 9 May 2025 03:18:35 +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=uHqHHwnn; dkim-atps=neutral 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 A0C6E47470 for ; Fri, 9 May 2025 03:18:30 +0000 (UTC) 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=xePc22s/+9XIaTA1s0VCrSoqbRcnX2MA28cnAtO27z4=; b=uHqHHwnnkCp8YeGdTRAgaj2Psd6G9cAVwXpDr2xyUagYg05YC8JnwzJJaPbzadlqM6O6 f2jdlwSqJyJc78gYXLnwof+JWNE889cl5inAD3VFxeGElJrdFdzxUtvBU5ntfJtO1tmhJw 3sCpnPhBuiHrwRpDOqkMl6Jx7H5DaR1LS+3l1k9hdHHp2S95X2+5ppUKiCHRslrQJSD0mM eXnV8auMjjHjdxlQPdrJlCwmDDZOC4yKy1DbRLdLqUaK5omiqFxi6BcBjLKJRKfUMLKKJD qcGPopEq3IofOfQeW+hpyLhlwSiI3rsL9XjeyHjS4b585LeEDfFye6KkCXphSeVA== Received: by recvd-84ff9c6c5c-kkh8t with SMTP id recvd-84ff9c6c5c-kkh8t-1-681D7405-5 2025-05-09 03:18:29.349946872 +0000 UTC m=+2610444.923245818 Received: from herokuapp.com (unknown) by geopod-ismtpd-33 (SG) with ESMTP id QYN57T-PRGuSDmpMfPff-g for ; Fri, 09 May 2025 03:18:29.285 +0000 (UTC) Date: Fri, 09 May 2025 03:18:29 +0000 (UTC) Message-ID: References: Mime-Version: 1.0 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 21274 X-Redmine-Issue-Author: jeremyevans0 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: 98678 X-SG-EID: =?us-ascii?Q?u001=2Ep+ckLDtT+4Y5c+H0YCkEnsuWiCQmn3OZA=2F9FzjoR6ZZlPaMv54M7EFoSM?= =?us-ascii?Q?CX5Trc79ep2R5F+0oYS4n23jq1cv5vRtbpZ2jFF?= =?us-ascii?Q?AvsV6NSqql=2F2tRqsFGWIMkz3Em0RHQ7WnZ2HkdZ?= =?us-ascii?Q?1MK9Im+J77Y1xxbdh1XFLodyDComBMWGiHFB=2FFR?= =?us-ascii?Q?BScRdMFfmf5kXtCYvs=2FybAo24SF9oAmrpFHsgfL?= =?us-ascii?Q?fZrHcYZGAqTrbKkDPOT14RfS97oRArINkAbR5pb?= =?us-ascii?Q?B1=2F4?= To: ruby-core@ml.ruby-lang.org X-Entity-ID: u001.I8uzylDtAfgbeCOeLBYDww== Message-ID-Hash: D7X3EUWJQZEPRUCG2XL7MOGXVPATJPPP X-Message-ID-Hash: D7X3EUWJQZEPRUCG2XL7MOGXVPATJPPP 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.9 Precedence: list Reply-To: Ruby developers Subject: [ruby-core:121922] [Ruby Feature#21274] Show performance warnings for easily avoidable unnecessary implicit splat allocations 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 #21274 has been updated by mame (Yusuke Endoh). This was discussed at the dev meeting, but @matz was negative about adding this warning. He prefers fluent code over performance, so from Ruby's perspective, he did not want to encourage rewriting: ```ruby order!(argv, **keywords, &nonopts.method(:<<)) ``` to: ```ruby method = nonopts.method(:<<) order!(argv, **keywords, &method) ``` ---------------------------------------- Feature #21274: Show performance warnings for easily avoidable unnecessary implicit splat allocations https://bugs.ruby-lang.org/issues/21274#change-113030 * Author: jeremyevans0 (Jeremy Evans) * Status: Open ---------------------------------------- In Ruby 3.4, I made many changes to reduce implicit allocations (mostly in method calling). There are still a few cases where Ruby must allocate an array for a positional splat, or a hash for a keyword splat. Some of these allocations are unavoidable, but in other cases, while Ruby cannot avoid the allocation, it is easy for a user to make a small change to their code to avoid the allocation. One example of this is when Ruby allocates to avoid an evaluation order issue. For example: ```ruby def kw = nil ary = [] m(*ary, kw:) ``` Ruby allocates an array for `*ary`, even though it does not need to, because `kw` is a method call, and the method call could potentially modify `ary` (it doesn't in this example, but Ruby's compiler cannot assume that, as the method may be overridden later). It is simple to avoid the allocation by using a local variable: ```ruby def kw = nil ary = [] kw = self.kw m(*ary, kw:) ``` To make it easier for users to find and avoid these unnecessary implicit allocations, I would like to add a performance warning in cases where Ruby allocates solely to avoid an evaluation order issue. I've submitted a pull request to implement this: https://github.com/ruby/ruby/pull/13135 The current warning messages in the pull request are quite verbose: ``` $ ruby -W:performance -e 'def kw; {} end; a = []; p(*a, **kw)' -e: warning: This method call implicitly allocates a potentially unnecessary array for the positional splat, because a keyword, keyword splat, or block pass expression could cause an evaluation order issue if an array is not allocated for the positional splat. You can avoid this allocation by assigning the related keyword, keyword splat, or block pass expression to a local variable and using that local variable. $ ruby -W:performance -e 'def b; ->{} end; h = {}; p(**h, &b)' -e: warning: This method call implicitly allocates a potentially unnecessary hash for the keyword splat, because the block pass expression could cause an evaluation order issue if a hash is not allocated for the keyword splat. You can avoid this allocation by assigning the block pass expression to a local variable, and using that local variable. ``` It may be desirable to shorten these messages, so I would appreciate suggestions. -- 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/lists/ruby-core.ml.ruby-lang.org/