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,T_SCC_BODY_TEXT_LINE autolearn=no 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 1DE7C1F548 for ; Thu, 5 Sep 2024 06:08:36 +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=U2MM9bmu; 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=FY7M/IlN; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ml.ruby-lang.org; s=mail; t=1725516513; bh=trg4Jypg8oH5SB6ALUn7MdEuKhtIXUrRLy13TK1tEv8=; 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=U2MM9bmu5vMi54S+fLltV2JNWcqCFBCKDKT8gRYPsd54vENgZ1UqhSVLzDUEuDIqH JtPZ23eX59uGpUQLcOiPMab6R5xcDvdseDGDmodxi0sOgad7M8HPly5zkaeZ57AjHt kWz6PpgLzh1t2ey53XnHO1WFeKzNx3haLpDKT1kQ= Received: from nue.mailmanlists.eu (localhost [IPv6:::1]) by nue.mailmanlists.eu (Postfix) with ESMTP id E62E343E11 for ; Thu, 5 Sep 2024 06:08:33 +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=FY7M/IlN; dkim-atps=neutral Received: from s.wrqvtzvf.outbound-mail.sendgrid.net (s.wrqvtzvf.outbound-mail.sendgrid.net [149.72.126.143]) by nue.mailmanlists.eu (Postfix) with ESMTPS id 9129843D49 for ; Thu, 5 Sep 2024 06:08:21 +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=H8ayE/TN/NmsKUNaJFNIhUzyKLURPd+tXzY+HbaJlrU=; b=FY7M/IlN8HDA+W641rn1rDasvn0NOVTrHWBCT9IELylCmN1/IfsDdL4kiPvIZW/EZqXf cKedzz95f4FNbdsKI5EOuWs63KxfRoqfU7u9BjYjguNwl5S3K5L29WY3fKQefgxsGAwbLy v2N1S3KwDwMzAtMvoFdazKdF90XvLatyrh2tjMU8k1Ll9luN8PkqokxR+huVCjqf4dvrTS GO2V/fT78+3EtKF/Vss9HVeyqDAxvzjbLfADWaaAhn3O/HWXOyJ0N7NkSE3Ndv9s7nJyOH V8tgIlgu6tLOfSKshYbWT10VU1xYZOSfmFnJMGPLHGX+iFpMxR/gbkwzTm+xo65w== Received: by recvd-649fff897d-qnv5j with SMTP id recvd-649fff897d-qnv5j-1-66D94AD4-9 2024-09-05 06:08:20.439703553 +0000 UTC m=+1237587.991184300 Received: from herokuapp.com (unknown) by geopod-ismtpd-37 (SG) with ESMTP id 05PXeo09T2-D-wOksmGJfw for ; Thu, 05 Sep 2024 06:08:20.426 +0000 (UTC) Date: Thu, 05 Sep 2024 06:08:20 +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: byroot 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: 95695 X-SG-EID: =?us-ascii?Q?u001=2EKmNZ1u3n1vIpO8NNTdp+Q9c0ai7potxbEDLMO7SOJO=2F4KkRUz0d23466m?= =?us-ascii?Q?naiq=2F5fmA4hb60MdRMUAwHZnjIWVFu=2FrqiBOz5c?= =?us-ascii?Q?nOvkBudsSThahEgAVIYhrwcLvX=2FLQ38SwmlAB02?= =?us-ascii?Q?C2Av2GJ4zNo04Gc5aXlmwE5m7bpVXL0iEyxVc+2?= =?us-ascii?Q?=2FcwRKTQ4gvTGTxCwZdO1WwHLGKPJkNeF1Pj1o8n?= =?us-ascii?Q?Km8cF+nlAw=2F5iTbP2WvvX5iV9voxF+g0l7fU3TE?= =?us-ascii?Q?xrPz61Fty93VPpEAAh5b27oLyA=3D=3D?= To: ruby-core@ml.ruby-lang.org X-Entity-ID: u001.I8uzylDtAfgbeCOeLBYDww== Message-ID-Hash: H5LZJ2N2K36S72J2Z5MV5R3YSKKG6RWJ X-Message-ID-Hash: H5LZJ2N2K36S72J2Z5MV5R3YSKKG6RWJ 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:119055] [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: "byroot (Jean Boussier) via ruby-core" Cc: "byroot (Jean Boussier)" Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Issue #20684 has been updated by byroot (Jean Boussier). Makes sense, thank you Matz! ---------------------------------------- Feature #20684: Add optimized instructions for frozen literal Hash and Array https://bugs.ruby-lang.org/issues/20684#change-109633 * 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/