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 D2E3A1F548 for ; Thu, 5 Sep 2024 05:51:13 +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=UZUombrc; 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=KOHnS3ph; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ml.ruby-lang.org; s=mail; t=1725515441; bh=PiozmVynFsom7P0sePTA72X4O2d40pk0gr2iPUzlEcI=; 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=UZUombrcord2/lfk7aB5t/iDjBsjj/zEFC2w+qi7dLCd4WizkGwMyFDthBBTvhd+Z GSCJgWFPMVrQzJNaXkSkuaA/CxUoha1qojWdoZXtOQovRZG5LVm7IKD9t3e/+AVohO AaSpj6OnZ4CVFOKmnEr19H1uaRxgsbOXxQEqH9hk= Received: from nue.mailmanlists.eu (localhost [IPv6:::1]) by nue.mailmanlists.eu (Postfix) with ESMTP id 7C1A143D5A for ; Thu, 5 Sep 2024 05:50:41 +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=KOHnS3ph; 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 3D03343D06 for ; Thu, 5 Sep 2024 05:50: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=keGzrjIN3pWeOX6rFG45Io8LvkoSnLv/wTx+9q/PLWQ=; b=KOHnS3phRRxJLHFDIMy2NqpoYz1k4qiaQFj0naXW/fa2Vir0tfqxtIs/2ufAgSsUc9cQ GnKfuZN/YiNlvowLs8j17VGWnoGfanQSFLWpHIgp6csMGp9ZYdyeL3GtQlZ987ziRkIFzm OK2kR9DUna0/18t5EF4XKkNvDDr7TJ8Y8igSfet2bUIONcKi2e7a9l8lgHMiPKQG3hoGYl jFtX8GHTOGqflCL2jaVHydnGKyXbvm5nPU6WlJzQUmjWh7WT0hifot/kL97bj0i95OiIN2 UOP+zvaDgKziqqEDMXSUrXy2eZEqO8wipvEqyO20ab9PX385RgYkajjhVimb/OWQ== Received: by recvd-84679d98c6-xh2kf with SMTP id recvd-84679d98c6-xh2kf-1-66D946A4-15 2024-09-05 05:50:28.970001353 +0000 UTC m=+1237045.498350714 Received: from herokuapp.com (unknown) by geopod-ismtpd-11 (SG) with ESMTP id A2bUJ5S0TQO-9ROYVmYOiA for ; Thu, 05 Sep 2024 05:50:28.923 +0000 (UTC) Date: Thu, 05 Sep 2024 05:50:29 +0000 (UTC) Message-ID: References: Mime-Version: 1.0 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 20684 X-Redmine-Issue-Author: etienne X-Redmine-Issue-Priority: Normal X-Redmine-Sender: matz 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: 95693 X-SG-EID: =?us-ascii?Q?u001=2Eqca74XLbbBG2T0HhbEZgBN9E1Qhb0c7bGBe1waNAWdTreSqyWPDekZUgq?= =?us-ascii?Q?NYk4+nm=2F0VIqwc8ZBTQxqk7G=2FctsRLtYG2yTl=2FP?= =?us-ascii?Q?2CaGNETxG2RTd+y58wVaaB1mshDQjLFS9GxH4hJ?= =?us-ascii?Q?9fTs++QdWvg9ESQ0WN6tkn7dNSeUNjBA6REP+Tu?= =?us-ascii?Q?FJ9kH=2FGrWxutP4o1i2LlM5Ko=2F=2FEFiFLJp=2FJJUVw?= =?us-ascii?Q?ScJQDXRd3aYV7WoIFnoHzNyHQw6ifyzWTt4Ts0A?= =?us-ascii?Q?C4Fl2PNaO22i1RV3hZtRBteAuA=3D=3D?= To: ruby-core@ml.ruby-lang.org X-Entity-ID: u001.I8uzylDtAfgbeCOeLBYDww== Message-ID-Hash: US4F4GYJWGDSJI3EW2VRCUDJ5GB226TI X-Message-ID-Hash: US4F4GYJWGDSJI3EW2VRCUDJ5GB226TI 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:119054] [Ruby master Feature#20684] Add optimized instructions for frozen literal Hash and Array List-Id: Ruby developers Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: "matz (Yukihiro Matsumoto) via ruby-core" Cc: "matz (Yukihiro Matsumoto)" Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Issue #20684 has been updated by matz (Yukihiro Matsumoto). Basically agreed. But the existence (and side effect) of this optimization = should be implementation defined, especially regarding `object_id`. Matz. ---------------------------------------- Feature #20684: Add optimized instructions for frozen literal Hash and Array https://bugs.ruby-lang.org/issues/20684#change-109631 * Author: etienne (=C9tienne Barri=E9) * Status: Open ---------------------------------------- # Context Methods that take empty arrays or empty hashes as default values allocate a= new object each time the method is called without the argument. Often they= don't mutate the parameter. To prevent an allocation, in performance criti= cal sections, a constant is defined that holds a frozen hash or array, and = the constant is defined as the default value for the parameter. Here are some examples: Rails: https://github.com/rails/rails/blob/607d61e884237c223c24c6f47efa0b56= 1dd8b637/activerecord/lib/active_record/relation/query_methods.rb#L159-L160 Roda: https://github.com/jeremyevans/roda/blob/102926a02dcabc9a31674e3cf98f= 049139c31492/lib/roda/plugins.rb#L9-L10 dry-rb: https://github.com/dry-rb/dry-container/blob/1ee41bb109455d06bf22eb= cbd94b050cc4773733/lib/dry/container/mixin.rb#L68C5-L68C15 and many other gems: https://gist.github.com/casperisfine/47f22243d4ad20385= 5256ef5bfae7979 Additionally when defining a frozen literal constant, we're currently ineff= icient because we store the literal in the bytecode, we dup it just to free= ze it again. It doesn't amount to much but would be nice to avoid. # Proposal Introduce 2 new optimized instructions `opt_ary_freeze` and `opt_hash_freez= e` that behave like `opt_str_freeze` for their respective types. If the fre= eze method hasn't been redefined, they simply push the frozen literal value= on the stack. Like for `opt_str_freeze`, these instructions are added by t= he peephole optimizer when applicable. In the specific case of empty array and empty hash, we use a pre-allocated = global empty frozen object to avoid retaining a distinct empty object each = time. This will allow code like this: https://github.com/ruby/ruby/blob/566f2eb50= 1d94d4047a9aad4af0d74c6a96f34a9/lib/rubygems/resolver/api_set/gem_parser.rb= to be shortened and simplified like this: ```diff diff --git i/lib/rubygems/resolver/api_set/gem_parser.rb w/lib/rubygems/res= olver/api_set/gem_parser.rb index 643b857107..34146fd426 100644 --- i/lib/rubygems/resolver/api_set/gem_parser.rb +++ w/lib/rubygems/resolver/api_set/gem_parser.rb @@ -1,15 +1,12 @@ # frozen_string_literal: true =20 class Gem::Resolver::APISet::GemParser - EMPTY_ARRAY =3D [].freeze - private_constant :EMPTY_ARRAY - def parse(line) version_and_platform, rest =3D line.split(" ", 2) version, platform =3D version_and_platform.split("-", 2) dependencies, requirements =3D rest.split("|", 2).map! {|s| s.split(",= ") } if rest - dependencies =3D dependencies ? dependencies.map! {|d| parse_dependenc= y(d) } : EMPTY_ARRAY - requirements =3D requirements ? requirements.map! {|d| parse_dependenc= y(d) } : EMPTY_ARRAY + dependencies =3D dependencies ? dependencies.map! {|d| parse_dependenc= y(d) } : [].freeze + requirements =3D requirements ? requirements.map! {|d| parse_dependenc= y(d) } : [].freeze [version, platform, dependencies, requirements] end ``` Overall it's a minor optimization but also a very simple patch and makes co= de nicer. PR: https://github.com/ruby/ruby/pull/11406 --=20 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.rub= y-lang.org/