From: Nicolas Roche <ro...@adacore.com>

Improve performance of iteration using Element function.
Improve performance of Append.

gcc/ada/ChangeLog:

        * libgnat/a-stzunb__shared.adb: Restructure code to inline only
        the most common cases. Remove whenever possible runtime checks.
        * libgnat/a-stzunb__shared.ads: Add Inline => True to Append
        variants and Element.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/libgnat/a-stzunb__shared.adb | 165 ++++++++++++++++++++-------
 gcc/ada/libgnat/a-stzunb__shared.ads |  12 +-
 2 files changed, 130 insertions(+), 47 deletions(-)

diff --git a/gcc/ada/libgnat/a-stzunb__shared.adb 
b/gcc/ada/libgnat/a-stzunb__shared.adb
index 39dd7b94283..e5045c866b6 100644
--- a/gcc/ada/libgnat/a-stzunb__shared.adb
+++ b/gcc/ada/libgnat/a-stzunb__shared.adb
@@ -36,7 +36,24 @@ package body Ada.Strings.Wide_Wide_Unbounded is
 
    use Ada.Strings.Wide_Wide_Maps;
 
-   Growth_Factor : constant := 32;
+   procedure Non_Inlined_Append
+     (Source   : in out Unbounded_Wide_Wide_String;
+      New_Item : Unbounded_Wide_Wide_String);
+
+   procedure Non_Inlined_Append
+     (Source   : in out Unbounded_Wide_Wide_String;
+      New_Item : Wide_Wide_String);
+
+   procedure Non_Inlined_Append
+      (Source   : in out Unbounded_Wide_Wide_String;
+       New_Item : Wide_Wide_Character);
+   --  Non_Inlined_Append are part of the respective Append method that
+   --  should not be inlined. The idea is that the code of Append is inlined.
+   --  In order to make inlining efficient it is better to have the inlined
+   --  code as small as possible. Thus most common cases are inlined and less
+   --  common cases are deferred in these functions.
+
+   Growth_Factor : constant := 2;
    --  The growth factor controls how much extra space is allocated when
    --  we have to increase the size of an allocated unbounded string. By
    --  allocating extra space, we avoid the need to reallocate on every
@@ -526,10 +543,12 @@ package body Ada.Strings.Wide_Wide_Unbounded is
      (Source   : in out Unbounded_Wide_Wide_String;
       New_Item : Unbounded_Wide_Wide_String)
    is
+      pragma Suppress (All_Checks);
+      --  Suppress checks as they are redundant with the checks done in that
+      --  function.
+
       SR  : constant Shared_Wide_Wide_String_Access := Source.Reference;
       NR  : constant Shared_Wide_Wide_String_Access := New_Item.Reference;
-      DL  : constant Natural              := SR.Last + NR.Last;
-      DR  : Shared_Wide_Wide_String_Access;
 
    begin
       --  Source is an empty string, reuse New_Item data
@@ -546,19 +565,17 @@ package body Ada.Strings.Wide_Wide_Unbounded is
 
       --  Try to reuse existent shared string
 
-      elsif Can_Be_Reused (SR, DL) then
-         SR.Data (SR.Last + 1 .. DL) := NR.Data (1 .. NR.Last);
-         SR.Last := DL;
+      elsif System.Atomic_Counters.Is_One (SR.Counter)
+         and then NR.Last <= SR.Max_Length
+         and then SR.Max_Length - NR.Last >= SR.Last
+      then
+         SR.Data (SR.Last + 1 .. SR.Last + NR.Last) := NR.Data (1 .. NR.Last);
+         SR.Last := SR.Last + NR.Last;
 
       --  Otherwise, allocate new one and fill it
 
       else
-         DR := Allocate (DL + DL / Growth_Factor);
-         DR.Data (1 .. SR.Last) := SR.Data (1 .. SR.Last);
-         DR.Data (SR.Last + 1 .. DL) := NR.Data (1 .. NR.Last);
-         DR.Last := DL;
-         Source.Reference := DR;
-         Unreference (SR);
+         Non_Inlined_Append (Source, New_Item);
       end if;
    end Append;
 
@@ -566,31 +583,34 @@ package body Ada.Strings.Wide_Wide_Unbounded is
      (Source   : in out Unbounded_Wide_Wide_String;
       New_Item : Wide_Wide_String)
    is
-      SR : constant Shared_Wide_Wide_String_Access := Source.Reference;
-      DL : constant Natural := SR.Last + New_Item'Length;
-      DR : Shared_Wide_Wide_String_Access;
+      pragma Suppress (All_Checks);
+      --  Suppress checks as they are redundant with the checks done in that
+      --  function.
 
+      New_Item_Length : constant Natural := New_Item'Length;
+      SR : constant Shared_Wide_Wide_String_Access := Source.Reference;
    begin
-      --  New_Item is an empty string, nothing to do
 
       if New_Item'Length = 0 then
+         --  New_Item is an empty string, nothing to do
          null;
 
-      --  Try to reuse existing shared string
-
-      elsif Can_Be_Reused (SR, DL) then
-         SR.Data (SR.Last + 1 .. DL) := New_Item;
-         SR.Last := DL;
-
-      --  Otherwise, allocate new one and fill it
+      elsif System.Atomic_Counters.Is_One (SR.Counter)
+         --  The following test checks in fact that
+         --  SR.Max_Length >= SR.Last + New_Item_Length without causing
+         --  overflow.
+         and then New_Item_Length <= SR.Max_Length
+         and then SR.Max_Length - New_Item_Length >= SR.Last
+      then
+         --  Try to reuse existing shared string
+         SR.Data (SR.Last + 1 .. SR.Last + New_Item_Length) := New_Item;
+         SR.Last := SR.Last + New_Item_Length;
 
       else
-         DR := Allocate (DL + DL / Growth_Factor);
-         DR.Data (1 .. SR.Last) := SR.Data (1 .. SR.Last);
-         DR.Data (SR.Last + 1 .. DL) := New_Item;
-         DR.Last := DL;
-         Source.Reference := DR;
-         Unreference (SR);
+         --  Otherwise, allocate new one and fill it. Deferring the worst case
+         --  into a separate non-inlined function ensure that inlined Append
+         --  code size remains short and thus efficient.
+         Non_Inlined_Append (Source, New_Item);
       end if;
    end Append;
 
@@ -598,26 +618,24 @@ package body Ada.Strings.Wide_Wide_Unbounded is
      (Source   : in out Unbounded_Wide_Wide_String;
       New_Item : Wide_Wide_Character)
    is
-      SR : constant Shared_Wide_Wide_String_Access := Source.Reference;
-      DL : constant Natural := SR.Last + 1;
-      DR : Shared_Wide_Wide_String_Access;
+      pragma Suppress (All_Checks);
+      --  Suppress checks as they are redundant with the checks done in that
+      --  function.
 
+      SR : constant Shared_Wide_Wide_String_Access := Source.Reference;
    begin
-      --  Try to reuse existing shared string
-
-      if Can_Be_Reused (SR, SR.Last + 1) then
+      if System.Atomic_Counters.Is_One (SR.Counter)
+         and then SR.Max_Length > SR.Last
+      then
+         --  Try to reuse existing shared string
          SR.Data (SR.Last + 1) := New_Item;
          SR.Last := SR.Last + 1;
 
-      --  Otherwise, allocate new one and fill it
-
       else
-         DR := Allocate (DL + DL / Growth_Factor);
-         DR.Data (1 .. SR.Last) := SR.Data (1 .. SR.Last);
-         DR.Data (DL) := New_Item;
-         DR.Last := DL;
-         Source.Reference := DR;
-         Unreference (SR);
+         --  Otherwise, allocate new one and fill it. Deferring the worst case
+         --  into a separate non-inlined function ensure that inlined Append
+         --  code size remains short and thus efficient.
+         Non_Inlined_Append (Source, New_Item);
       end if;
    end Append;
 
@@ -777,6 +795,7 @@ package body Ada.Strings.Wide_Wide_Unbounded is
      (Source : Unbounded_Wide_Wide_String;
       Index  : Positive) return Wide_Wide_Character
    is
+      pragma Suppress (All_Checks);
       SR : constant Shared_Wide_Wide_String_Access := Source.Reference;
    begin
       if Index <= SR.Last then
@@ -1184,6 +1203,66 @@ package body Ada.Strings.Wide_Wide_Unbounded is
       return Source.Reference.Last;
    end Length;
 
+   ------------------------
+   -- Non_Inlined_Append --
+   ------------------------
+
+   procedure Non_Inlined_Append
+       (Source   : in out Unbounded_Wide_Wide_String;
+        New_Item : Unbounded_Wide_Wide_String)
+   is
+      SR : constant Shared_Wide_Wide_String_Access := Source.Reference;
+      NR  : constant Shared_Wide_Wide_String_Access := New_Item.Reference;
+      DL : constant Natural := SR.Last + NR.Last;
+      DR : Shared_Wide_Wide_String_Access;
+   begin
+      DR := Allocate (DL + DL / Growth_Factor);
+      DR.Data (1 .. SR.Last) := SR.Data (1 .. SR.Last);
+      DR.Data (SR.Last + 1 .. DL) := NR.Data (1 .. NR.Last);
+      DR.Last := DL;
+      Source.Reference := DR;
+      Unreference (SR);
+   end Non_Inlined_Append;
+
+   procedure Non_Inlined_Append
+     (Source   : in out Unbounded_Wide_Wide_String;
+      New_Item : Wide_Wide_String)
+   is
+      SR : constant Shared_Wide_Wide_String_Access := Source.Reference;
+      DL : constant Natural := SR.Last +  New_Item'Length;
+      DR : Shared_Wide_Wide_String_Access;
+   begin
+      DR := Allocate (DL + DL / Growth_Factor);
+      DR.Data (1 .. SR.Last) := SR.Data (1 .. SR.Last);
+      DR.Data (SR.Last + 1 .. DL) := New_Item;
+      DR.Last := DL;
+      Source.Reference := DR;
+      Unreference (SR);
+   end Non_Inlined_Append;
+
+   procedure Non_Inlined_Append
+      (Source   : in out Unbounded_Wide_Wide_String;
+       New_Item : Wide_Wide_Character)
+   is
+      SR : constant Shared_Wide_Wide_String_Access := Source.Reference;
+   begin
+      if SR.Last = Natural'Last then
+         raise Constraint_Error;
+      else
+         declare
+            DL : constant Natural := SR.Last + 1;
+            DR : Shared_Wide_Wide_String_Access;
+         begin
+            DR := Allocate (DL + DL / Growth_Factor);
+            DR.Data (1 .. SR.Last) := SR.Data (1 .. SR.Last);
+            DR.Data (DL) := New_Item;
+            DR.Last := DL;
+            Source.Reference := DR;
+            Unreference (SR);
+         end;
+      end if;
+   end Non_Inlined_Append;
+
    ---------------
    -- Overwrite --
    ---------------
diff --git a/gcc/ada/libgnat/a-stzunb__shared.ads 
b/gcc/ada/libgnat/a-stzunb__shared.ads
index 5de03471fa7..1e7b9e12408 100644
--- a/gcc/ada/libgnat/a-stzunb__shared.ads
+++ b/gcc/ada/libgnat/a-stzunb__shared.ads
@@ -79,15 +79,18 @@ package Ada.Strings.Wide_Wide_Unbounded is
 
    procedure Append
      (Source   : in out Unbounded_Wide_Wide_String;
-      New_Item : Unbounded_Wide_Wide_String);
+      New_Item : Unbounded_Wide_Wide_String)
+   with Inline => True;
 
    procedure Append
      (Source   : in out Unbounded_Wide_Wide_String;
-      New_Item : Wide_Wide_String);
+      New_Item : Wide_Wide_String)
+   with Inline => True;
 
    procedure Append
      (Source   : in out Unbounded_Wide_Wide_String;
-      New_Item : Wide_Wide_Character);
+      New_Item : Wide_Wide_Character)
+   with Inline => True;
 
    function "&"
      (Left  : Unbounded_Wide_Wide_String;
@@ -111,7 +114,8 @@ package Ada.Strings.Wide_Wide_Unbounded is
 
    function Element
      (Source : Unbounded_Wide_Wide_String;
-      Index  : Positive) return Wide_Wide_Character;
+      Index  : Positive) return Wide_Wide_Character
+   with Inline => True;
 
    procedure Replace_Element
      (Source : in out Unbounded_Wide_Wide_String;
-- 
2.43.0

Reply via email to