summaryrefslogtreecommitdiff
path: root/gcc/ada/a-strunb.adb
diff options
context:
space:
mode:
authorPascal Obry <obry@adacore.com>2005-06-16 10:31:41 +0200
committerArnaud Charlet <charlet@gcc.gnu.org>2005-06-16 10:31:41 +0200
commit0ba5b393544f89748b24bd522b56ce62d38e2a13 (patch)
tree98b90457f8da442292245fa317ed89f4c9975189 /gcc/ada/a-strunb.adb
parent00109226ca019e0e67fc12872afe1a2a7a68115b (diff)
downloadgcc-0ba5b393544f89748b24bd522b56ce62d38e2a13.tar.gz
a-stzunb.adb, [...] (Realloc_For_Chunk): New implementation which is slightly more efficient.
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
Diffstat (limited to 'gcc/ada/a-strunb.adb')
-rw-r--r--gcc/ada/a-strunb.adb36
1 files changed, 27 insertions, 9 deletions
diff --git a/gcc/ada/a-strunb.adb b/gcc/ada/a-strunb.adb
index bf492ebedd0..026cb3bd712 100644
--- a/gcc/ada/a-strunb.adb
+++ b/gcc/ada/a-strunb.adb
@@ -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;