]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
a-stzunb.adb, [...] (Realloc_For_Chunk): New implementation which is slightly more...
authorPascal Obry <obry@adacore.com>
Thu, 16 Jun 2005 08:31:41 +0000 (10:31 +0200)
committerArnaud Charlet <charlet@gcc.gnu.org>
Thu, 16 Jun 2005 08:31:41 +0000 (10:31 +0200)
2005-06-14  Pascal Obry  <obry@adacore.com>

* a-stzunb.adb, a-stwiun.adb, a-strunb.adb (Realloc_For_Chunk): New
implementation which is slightly more efficient.

From-SVN: r101023

gcc/ada/a-strunb.adb
gcc/ada/a-stwiun.adb
gcc/ada/a-stzunb.adb

index bf492ebedd004005f1b6f9931d17682c94beb6da..026cb3bd71262c1e819c69139ebef24c0a196ae0 100644 (file)
@@ -1,8 +1,8 @@
 ------------------------------------------------------------------------------
 --                                                                          --
---                          GNAT RUNTIME COMPONENTS                         --
+--                         GNAT RUN-TIME COMPONENTS                         --
 --                                                                          --
---                A D A . S T R I N G S . U N B O U N D E D                 --
+--                 A D A . S T R I N G S . U N B O U N D E D                --
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
@@ -623,7 +623,6 @@ package body Ada.Strings.Unbounded is
         (Source.Reference (1 .. Source.Last), Pattern, From, Going, Mapping);
    end Index;
 
-
    function Index
      (Source  : Unbounded_String;
       Set     : Maps.Character_Set;
@@ -755,17 +754,36 @@ package body Ada.Strings.Unbounded is
      (Source     : in out Unbounded_String;
       Chunk_Size : Natural)
    is
-      Growth_Factor : constant := 50;
-      S_Length      : constant Natural := Source.Reference'Length;
+      Growth_Factor : constant := 32;
+      --  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
+      --  append, particularly important when a string is built up by repeated
+      --  append operations of small pieces. This is expressed as a factor so
+      --  32 means add 1/32 of the length of the string as growth space.
+
+      Min_Mul_Alloc : constant := Standard'Maximum_Alignment;
+      --  Allocation will be done by a multiple of Min_Mul_Alloc This causes
+      --  no memory loss as most (all?) malloc implementations are obliged to
+      --  align the returned memory on the maximum alignment as malloc does not
+      --  know the target alignment.
+
+      S_Length : constant Natural := Source.Reference'Length;
 
    begin
       if Chunk_Size > S_Length - Source.Last then
          declare
-            Alloc_Chunk_Size : constant Positive :=
-                                 Chunk_Size + (S_Length / Growth_Factor);
-            Tmp : String_Access;
+            New_Size : constant Positive :=
+                         S_Length + Chunk_Size + (S_Length / Growth_Factor);
+
+            New_Rounded_Up_Size : constant Positive :=
+                                    ((New_Size - 1) / Min_Mul_Alloc + 1) *
+                                       Min_Mul_Alloc;
+
+            Tmp : constant String_Access :=
+                    new String (1 .. New_Rounded_Up_Size);
+
          begin
-            Tmp := new String (1 .. S_Length + Alloc_Chunk_Size);
             Tmp (1 .. Source.Last) := Source.Reference (1 .. Source.Last);
             Free (Source.Reference);
             Source.Reference := Tmp;
index e722111c8db383d6d95d24c6b2a8dfa7ace03f49..d0e187eca28e301dcbb29fe876173021cedd3d68 100644 (file)
@@ -1,6 +1,6 @@
 ------------------------------------------------------------------------------
 --                                                                          --
---                         GNAT RUNTIME COMPONENTS                          --
+--                         GNAT RUN-TIME COMPONENTS                         --
 --                                                                          --
 --           A D A . S T R I N G S . W I D E _ U N B O U N D E D            --
 --                                                                          --
@@ -636,7 +636,6 @@ package body Ada.Strings.Wide_Unbounded is
           (Source.Reference (1 .. Source.Last), Pattern, From, Going, Mapping);
    end Index;
 
-
    function Index
      (Source  : Unbounded_Wide_String;
       Set     : Wide_Maps.Wide_Character_Set;
@@ -772,17 +771,36 @@ package body Ada.Strings.Wide_Unbounded is
      (Source     : in out Unbounded_Wide_String;
       Chunk_Size : Natural)
    is
-      Growth_Factor : constant := 50;
-      S_Length      : constant Natural := Source.Reference'Length;
+      Growth_Factor : constant := 32;
+      --  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
+      --  append, particularly important when a string is built up by repeated
+      --  append operations of small pieces. This is expressed as a factor so
+      --  32 means add 1/32 of the length of the string as growth space.
+
+      Min_Mul_Alloc : constant := Standard'Maximum_Alignment;
+      --  Allocation will be done by a multiple of Min_Mul_Alloc This causes
+      --  no memory loss as most (all?) malloc implementations are obliged to
+      --  align the returned memory on the maximum alignment as malloc does not
+      --  know the target alignment.
+
+      S_Length : constant Natural := Source.Reference'Length;
 
    begin
       if Chunk_Size > S_Length - Source.Last then
          declare
-            Alloc_Chunk_Size : constant Positive :=
-                                 Chunk_Size + (S_Length / Growth_Factor);
-            Tmp : Wide_String_Access;
+            New_Size : constant Positive :=
+                         S_Length + Chunk_Size + (S_Length / Growth_Factor);
+
+            New_Rounded_Up_Size : constant Positive :=
+                                    ((New_Size - 1) / Min_Mul_Alloc + 1) *
+                                       Min_Mul_Alloc;
+
+            Tmp : constant Wide_String_Access :=
+                    new Wide_String (1 .. New_Rounded_Up_Size);
+
          begin
-            Tmp := new Wide_String (1 .. S_Length + Alloc_Chunk_Size);
             Tmp (1 .. Source.Last) := Source.Reference (1 .. Source.Last);
             Free (Source.Reference);
             Source.Reference := Tmp;
@@ -935,7 +953,6 @@ package body Ada.Strings.Wide_Unbounded is
       return Source.Reference (1 .. Source.Last);
    end To_Wide_String;
 
-
    ---------------
    -- Translate --
    ---------------
index 8717bb375777325b91bd7560f0d375039e360806..390fb389e3c7f2d558694ce2a5369face659807b 100644 (file)
@@ -1,6 +1,6 @@
 ------------------------------------------------------------------------------
 --                                                                          --
---                         GNAT RUNTIME COMPONENTS                          --
+--                         GNAT RUN-TIME COMPONENTS                         --
 --                                                                          --
 --      A D A . S T R I N G S . W I D E _ W I D E _ U N B O U N D E D       --
 --                                                                          --
@@ -647,7 +647,6 @@ package body Ada.Strings.Wide_Wide_Unbounded is
           (Source.Reference (1 .. Source.Last), Pattern, From, Going, Mapping);
    end Index;
 
-
    function Index
      (Source : Unbounded_Wide_Wide_String;
       Set    : Wide_Wide_Maps.Wide_Wide_Character_Set;
@@ -783,17 +782,36 @@ package body Ada.Strings.Wide_Wide_Unbounded is
      (Source     : in out Unbounded_Wide_Wide_String;
       Chunk_Size : Natural)
    is
-      Growth_Factor : constant := 50;
-      S_Length      : constant Natural := Source.Reference'Length;
+      Growth_Factor : constant := 32;
+      --  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
+      --  append, particularly important when a string is built up by repeated
+      --  append operations of small pieces. This is expressed as a factor so
+      --  32 means add 1/32 of the length of the string as growth space.
+
+      Min_Mul_Alloc : constant := Standard'Maximum_Alignment;
+      --  Allocation will be done by a multiple of Min_Mul_Alloc This causes
+      --  no memory loss as most (all?) malloc implementations are obliged to
+      --  align the returned memory on the maximum alignment as malloc does not
+      --  know the target alignment.
+
+      S_Length : constant Natural := Source.Reference'Length;
 
    begin
       if Chunk_Size > S_Length - Source.Last then
          declare
-            Alloc_Chunk_Size : constant Positive :=
-                                 Chunk_Size + (S_Length / Growth_Factor);
-            Tmp : Wide_Wide_String_Access;
+            New_Size : constant Positive :=
+                         S_Length + Chunk_Size + (S_Length / Growth_Factor);
+
+            New_Rounded_Up_Size : constant Positive :=
+                                    ((New_Size - 1) / Min_Mul_Alloc + 1) *
+                                       Min_Mul_Alloc;
+
+            Tmp : constant Wide_Wide_String_Access :=
+                    new Wide_Wide_String (1 .. New_Rounded_Up_Size);
+
          begin
-            Tmp := new Wide_Wide_String (1 .. S_Length + Alloc_Chunk_Size);
             Tmp (1 .. Source.Last) := Source.Reference (1 .. Source.Last);
             Free (Source.Reference);
             Source.Reference := Tmp;
@@ -911,9 +929,9 @@ package body Ada.Strings.Wide_Wide_Unbounded is
       Free (Old);
    end Tail;
 
-   ------------------------------
+   -----------------------------------
    -- To_Unbounded_Wide_Wide_String --
-   ------------------------------
+   -----------------------------------
 
    function To_Unbounded_Wide_Wide_String
      (Source : Wide_Wide_String) return Unbounded_Wide_Wide_String
@@ -936,9 +954,9 @@ package body Ada.Strings.Wide_Wide_Unbounded is
       return Result;
    end To_Unbounded_Wide_Wide_String;
 
-   -------------------
+   -------------------------
    -- To_Wide_Wide_String --
-   --------------------
+   -------------------------
 
    function To_Wide_Wide_String
      (Source : Unbounded_Wide_Wide_String) return Wide_Wide_String
@@ -947,7 +965,6 @@ package body Ada.Strings.Wide_Wide_Unbounded is
       return Source.Reference (1 .. Source.Last);
    end To_Wide_Wide_String;
 
-
    ---------------
    -- Translate --
    ---------------