From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by sourceware.org (Postfix) with ESMTPS id D3B923856DCD for ; Mon, 5 Sep 2022 07:26:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D3B923856DCD Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: by mail-wr1-x432.google.com with SMTP id k9so10118631wri.0 for ; Mon, 05 Sep 2022 00:26:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :from:to:cc:subject:date; bh=6ukTYtsnqUygQlsQkdNz5ee1696bi9zW5jVnuYdfX0I=; b=WbbStVEMFuhK4Sei+9RnD6oTRkj6exem7MxSVv6mywdhp0NhSQ7xhUeroQ+DRZL02b 2/YhqognKeqcUazRAzc/xjNXmRhJjJ30QC9UPlEKlIswP+FMVRy+RfRagofNpPKcjSpt QyN0rCZ1j0MfuPSKEI+TQrdlhjsDO713ZitSon7bwjaHMkx1q2eJgCaVsyZeowWwiHgj Paec9530iJ0JRQe6wt7YSjz3crXMj7/W/Prepp00Syhz2gB50lHH4+yScqfgAZLYdukJ LsF/dyYVPRKzS24/2uSa0yWziIEQwHuDYHlO48QYOr10e706E/Q/EyLe3wKjPQ5BkYMZ EhUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date; bh=6ukTYtsnqUygQlsQkdNz5ee1696bi9zW5jVnuYdfX0I=; b=Da8LjNGNxl2CH2z3tmRdx9/LQZGct7GMyTzOAamAIzgLOqoY9dTU9tClkQIxGqsb1/ s1oWnjxsNDRWiWlO65KJrqyGfF3HpjGkh3JHUYf1FaVbjH8B0FweuVP5Rr5YDyj4FfN+ uIIIZaAHdoIMFzwpILiHxaJmXLPpFUfbaF8VWW6y0OhaEUbJ2KbI+37cN2K5Q1xZSFS5 zxq784FwXwTbkhQrQzEF+XwCAnpkqEGvu95b48lGpZbAXcBEAZAzalSYWs0HcSiKEesc bX1ARWg05j2aU5WmzYgIP170ajH5KpXFmyB6rjlTsXJ9KpF4AO9+0m53i9Mvlvpuj8OH S5vQ== X-Gm-Message-State: ACgBeo3fkOdfmbl9NtBEZPqcoXSKGiHUKIUa9/9hgTX2M/ZSJARtNyDN +c0u/LYlguTzXMsN31jvv7a1Ye3esXZfLA== X-Google-Smtp-Source: AA6agR5eTzzJvgUbdl0jFtqeQPe14lBTw/34hcBxONGmdSJKw7azDIKvJl+23oq++8UfIl8qT6eTGw== X-Received: by 2002:adf:f2c5:0:b0:226:e838:3ed8 with SMTP id d5-20020adff2c5000000b00226e8383ed8mr13964014wrp.545.1662362774494; Mon, 05 Sep 2022 00:26:14 -0700 (PDT) Received: from poulhies-Precision-5550 (static-176-191-105-132.ftth.abo.bbox.fr. [176.191.105.132]) by smtp.gmail.com with ESMTPSA id by8-20020a056000098800b0021d6dad334bsm7839542wrb.4.2022.09.05.00.26.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Sep 2022 00:26:14 -0700 (PDT) Date: Mon, 5 Sep 2022 09:26:13 +0200 From: Marc =?iso-8859-1?Q?Poulhi=E8s?= To: gcc-patches@gcc.gnu.org Cc: Steve Baird Subject: [Ada] Single-element Append performance improvement Message-ID: <20220905072613.GA1174821@poulhies-Precision-5550> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="mP3DRpeJDSE+ciuQ" Content-Disposition: inline X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --mP3DRpeJDSE+ciuQ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Ada.Containers.Vectors has two Append procedures that take an Element value; one takes a Count parameter and one does not (the count is implicitly one for the latter). For the former version, there was code that took a faster path if certain conditions were met and otherwise took a slower path; one of the prerequisite conditions for this was Count = 1. For the latter version, no such special-case detection was performed; the more general code was always executed. Move the special-case detection/handling code from the former version into the latter and change the former version to simply call the latter version if Count = 1. Also apply same change to Ada.Containers.Indefinite_Vectors. Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * libgnat/a-coinve.adb, libgnat/a-convec.adb (Append): If the Append that takes an Element and a Count is called with Count = 1, then call the Append that does not take a Count parameter; otherwise call the code that handles the general case. Move the special case detection/handling code that was formerly in that version of Append into the version that does not take a Count parameter, so that now both versions get the performance benefit. --mP3DRpeJDSE+ciuQ Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="patch.diff" diff --git a/gcc/ada/libgnat/a-coinve.adb b/gcc/ada/libgnat/a-coinve.adb --- a/gcc/ada/libgnat/a-coinve.adb +++ b/gcc/ada/libgnat/a-coinve.adb @@ -197,12 +197,29 @@ is Count : Count_Type) is begin - -- In the general case, we pass the buck to Insert, but for efficiency, - -- we check for the usual case where Count = 1 and the vector has enough - -- room for at least one more element. + -- In the general case, we take the slow path; for efficiency, + -- we check for the common case where Count = 1 . - if Count = 1 - and then Container.Elements /= null + if Count = 1 then + Append (Container, New_Item); + else + Append_Slow_Path (Container, New_Item, Count); + end if; + end Append; + + ------------ + -- Append -- + ------------ + + procedure Append (Container : in out Vector; + New_Item : Element_Type) + is + begin + -- For performance, check for the common special case where the + -- container already has room for at least one more element. + -- In the general case, pass the buck to Insert. + + if Container.Elements /= null and then Container.Last /= Container.Elements.Last then TC_Check (Container.TC); @@ -223,23 +240,11 @@ is Container.Elements.EA (New_Last) := new Element_Type'(New_Item); Container.Last := New_Last; end; - else - Append_Slow_Path (Container, New_Item, Count); + Insert (Container, Last_Index (Container) + 1, New_Item, 1); end if; end Append; - ------------ - -- Append -- - ------------ - - procedure Append (Container : in out Vector; - New_Item : Element_Type) - is - begin - Insert (Container, Last_Index (Container) + 1, New_Item, 1); - end Append; - ---------------------- -- Append_Slow_Path -- ---------------------- diff --git a/gcc/ada/libgnat/a-convec.adb b/gcc/ada/libgnat/a-convec.adb --- a/gcc/ada/libgnat/a-convec.adb +++ b/gcc/ada/libgnat/a-convec.adb @@ -173,27 +173,11 @@ is Count : Count_Type) is begin - -- In the general case, we pass the buck to Insert, but for efficiency, - -- we check for the usual case where Count = 1 and the vector has enough - -- room for at least one more element. - - if Count = 1 - and then Container.Elements /= null - and then Container.Last /= Container.Elements.Last - then - TC_Check (Container.TC); - - -- Increment Container.Last after assigning the New_Item, so we - -- leave the Container unmodified in case Finalize/Adjust raises - -- an exception. - - declare - New_Last : constant Index_Type := Container.Last + 1; - begin - Container.Elements.EA (New_Last) := New_Item; - Container.Last := New_Last; - end; + -- In the general case, we take the slow path; for efficiency, + -- we check for the common case where Count = 1 . + if Count = 1 then + Append (Container, New_Item); else Append_Slow_Path (Container, New_Item, Count); end if; @@ -222,7 +206,28 @@ is New_Item : Element_Type) is begin - Insert (Container, Last_Index (Container) + 1, New_Item, 1); + -- For performance, check for the common special case where the + -- container already has room for at least one more element. + -- In the general case, pass the buck to Insert. + + if Container.Elements /= null + and then Container.Last /= Container.Elements.Last + then + TC_Check (Container.TC); + + -- Increment Container.Last after assigning the New_Item, so we + -- leave the Container unmodified in case Finalize/Adjust raises + -- an exception. + + declare + New_Last : constant Index_Type := Container.Last + 1; + begin + Container.Elements.EA (New_Last) := New_Item; + Container.Last := New_Last; + end; + else + Insert (Container, Last_Index (Container) + 1, New_Item, 1); + end if; end Append; ---------------------- --mP3DRpeJDSE+ciuQ--