public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [Ada] Single-element Append performance improvement
@ 2022-09-05  7:26 Marc Poulhiès
  0 siblings, 0 replies; only message in thread
From: Marc Poulhiès @ 2022-09-05  7:26 UTC (permalink / raw)
  To: gcc-patches; +Cc: Steve Baird

[-- Attachment #1: Type: text/plain, Size: 1205 bytes --]

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.

[-- Attachment #2: patch.diff --]
[-- Type: text/x-diff, Size: 4315 bytes --]

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;
 
    ----------------------



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-09-05  7:26 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-05  7:26 [Ada] Single-element Append performance improvement Marc Poulhiès

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).