From patchwork Mon Sep 5 07:26:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Marc_Poulhi=C3=A8s?= X-Patchwork-Id: 985 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5044:0:0:0:0:0 with SMTP id h4csp59591wrt; Mon, 5 Sep 2022 00:39:54 -0700 (PDT) X-Google-Smtp-Source: AA6agR7H7BzN3XNEma+sb1XPRf5sJCU4RDg79j+9bk5TBNmgdFd2L9HwmA6WAknzTgDWT4r8inmi X-Received: by 2002:a17:906:b2d4:b0:731:4594:8ba1 with SMTP id cf20-20020a170906b2d400b0073145948ba1mr34399005ejb.288.1662363594560; Mon, 05 Sep 2022 00:39:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662363594; cv=none; d=google.com; s=arc-20160816; b=sdFNpEV0kqcMbkukZri2NnFNl5qrzclQb6hbyfdMDsfHVVhKXSEuf4MV1lL2KaMmZa A8VSv4cbaxOGCUk6UxCrifyvFCFM2zdI1nXHqEd+bq/Yd/HUhk85KMBi2FTN8yz5hfy3 WCiV0liLk0gBuAvTjPuabIot16EVhaN0H1AR9Z7tTf4eA/p1keMkXh1l9BCZfqVGJw44 NgEevp9thH2q+HJYZ69BaCWn0KWETuhCoVo4V/jMnBNnMw8xB6mZqPwc7QMrjNSsNWEm j9Nd5y+xyrfrfB7hwWGR7gK8T4t8RPe9u6dnK35em+Xxr9QckS1cEjvZmRm5G+bEHECO egBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=6ukTYtsnqUygQlsQkdNz5ee1696bi9zW5jVnuYdfX0I=; b=VSkB8l3NwRbaX/sZgDBA1jmTuk3sgiDr+Z0bbPduxvifvKC1Kx1AbEed7frfpYVPyc rChZT1fL9upNrx63QgzjW2u7+LFleaEWMydwFRQXCGKhx12PSXZGXHCHYreA7/z/EM82 vkgvxcFtAPdOwS8cmNEE4NCJ/z3gB4EKuFO5gaOCRMtc2bbKozvaWvjTwQfUf4aY2taq F1iFYS1qkUkcDNVlfe3vD0Mf1TdMrBtPT5X8aIsWv8wROi1dsd5Sl0uCnXaJEarQ8tlo EuYwk2ARTVRYztwjdtbML6FizIM+5sX7arvHQT/ccZYGf7La7bULG1ZIXRm3Ptw98Y1a e5HA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Wucg+Pp9; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id hb10-20020a170907160a00b0073d80d8b631si7716791ejc.268.2022.09.05.00.39.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Sep 2022 00:39:54 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Wucg+Pp9; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7FB82383817E for ; Mon, 5 Sep 2022 07:33:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7FB82383817E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1662363224; bh=6ukTYtsnqUygQlsQkdNz5ee1696bi9zW5jVnuYdfX0I=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=Wucg+Pp9OX6nGNAKKyE74SmJBfgfRv7JWw+IqqSzAWTNxOQM54Tc9x97K+LbZB/uP wWLef7X187VtWpt+Lx7d7rVne11sQtTaO0wW3aYI5a+MJ11Nog0MuZcobDrvGnD/nQ oX4UZcOIfU7FfanDvo951LC4588qvJawvh7YFPbs= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 Received: by mail-wr1-x432.google.com with SMTP id k9so10118631wri.0 for ; Mon, 05 Sep 2022 00:26:14 -0700 (PDT) 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-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 To: gcc-patches@gcc.gnu.org Subject: [Ada] Single-element Append performance improvement Message-ID: <20220905072613.GA1174821@poulhies-Precision-5550> MIME-Version: 1.0 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: =?utf-8?q?Marc_Poulhi=C3=A8s_via_Gcc-patches?= From: =?utf-8?q?Marc_Poulhi=C3=A8s?= Reply-To: Marc =?iso-8859-1?q?Poulhi=E8s?= Cc: Steve Baird Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1743114568629187075?= X-GMAIL-MSGID: =?utf-8?q?1743114568629187075?= 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. 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; ----------------------