[PATCH 7/7] Reduce memory waste due to non-power-of-2 allocs

Dodji Seketeli dodji@redhat.com
Sat Sep 17 13:41:00 GMT 2011


Jason Merrill <jason@redhat.com> writes:

> On 07/16/2011 10:37 AM, Dodji Seketeli wrote:
> > Ideally, I'd prefer some parts of this patch to be integrated into the
> > memory allocator.  That is, I'd like to see the memory allocator have
> > an interface that returns the actual size of memory it has allocated.
> > This would help client code like this one to avoid requesting memory
> > unnecessarily.
> 
> This sounds good to me.  Or, it would probably be simpler to just
> provide a separate function for rounding up the allocated size using
> the same code as the allocator.

OK, so the patch below extracts a public ggc_alloced_size_for_request
function from the different implementations of the ggc allocator's
interface, and lets new_linemap use that.

I am adding CC-ing Laurynas Biveinis to the CC list.

Thanks.

From: Dodji Seketeli <dodji@redhat.com>
Date: Tue, 17 May 2011 16:48:01 +0200
Subject: [PATCH 7/7] Reduce memory waste due to non-power-of-2 allocs

This patch basically arranges for the allocation size of line_map
buffers to be as close as possible to a power of two.  This
*significantly* decreases peak memory consumption as (macro) maps are
numerous and stay live during all the compilation.

The patch adds a new ggc_alloced_size_for_request interface to the ggc
allocator.  In each of the two main allocator implementations of
('page' and 'zone') the function has been extracted from the main
allocation function code and returns the actual size of the allocated
memory region, thus giving a chance to the caller to maximize the
amount of memory it actually uses from the allocated memory region.
In the 'none' allocator implementation (that uses xmalloc) the
ggc_alloced_size_for_request just returns the requested allocation
size.

Tested on x86_64-unknown-linux-gnu against trunk for each allocator.

libcpp/

	* include/line-map.h (struct line_maps::alloced_size_for_request):
	New member.
	* line-map.c (new_linemap): Use set->alloced_size_for_request to
	get the actual allocated size of line maps.

gcc/

	* ggc.h (ggc_alloced_size_for_request): Declare new public entry
	point.
	* ggc-none.c (ggc_alloced_size_for_request): New public stub
	function.
	* ggc-page.c (ggc_alloced_size_order_for_request): New static
	function.  Factorized from ggc_internal_alloc_stat.
	(ggc_alloced_size_for_request): New public function.  Uses
	ggc_alloced_size_order_for_request.
	(ggc_internal_alloc_stat): Use ggc_alloced_size_order_for_request.
	* ggc-zone.c (ggc_alloced_size_for_request): New public function
	extracted from ggc_internal_alloc_zone_stat.
	(ggc_internal_alloc_zone_stat): Use ggc_alloced_size_for_request.
	* toplev.c (general_init): Initialize
	line_table->alloced_size_for_request.
---
 gcc/ggc-none.c            |    9 +++++++
 gcc/ggc-page.c            |   53 +++++++++++++++++++++++++++++++++++---------
 gcc/ggc-zone.c            |   27 ++++++++++++++++------
 gcc/ggc.h                 |    2 +
 gcc/toplev.c              |    1 +
 libcpp/include/line-map.h |    8 ++++++
 libcpp/line-map.c         |   42 ++++++++++++++++++++++++++++++-----
 7 files changed, 117 insertions(+), 25 deletions(-)

diff --git a/gcc/ggc-none.c b/gcc/ggc-none.c
index 97d25b9..2223cc6 100644
--- a/gcc/ggc-none.c
+++ b/gcc/ggc-none.c
@@ -39,6 +39,15 @@ ggc_alloc_typed_stat (enum gt_types_enum ARG_UNUSED (gte), size_t size
   return xmalloc (size);
 }
 
+/* For a given size of memory requested for allocation, return the
+   actual size that is going to be allocated.  */
+
+size_t
+ggc_alloced_size_for_request (size_t requested_size)
+{
+  return requested_size;
+}
+
 void *
 ggc_internal_alloc_stat (size_t size MEM_STAT_DECL)
 {
diff --git a/gcc/ggc-page.c b/gcc/ggc-page.c
index 624f029..dcf9c79 100644
--- a/gcc/ggc-page.c
+++ b/gcc/ggc-page.c
@@ -1054,6 +1054,47 @@ static unsigned char size_lookup[NUM_SIZE_LOOKUP] =
   9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9
 };
 
+/* For a given size of memory requested for allocation, return the
+   actual size that is going to be allocated, as well as the size
+   order.  */
+
+static void
+ggc_alloced_size_order_for_request (size_t requested_size,
+                                    size_t *size_order,
+                                    size_t *alloced_size)
+{
+  size_t order, object_size;
+
+  if (requested_size < NUM_SIZE_LOOKUP)
+    {
+      order = size_lookup[requested_size];
+      object_size = OBJECT_SIZE (order);
+    }
+  else
+    {
+      order = 10;
+      while (requested_size > (object_size = OBJECT_SIZE (order)))
+        order++;
+    }
+
+  if (size_order)
+    *size_order = order;
+  if (alloced_size)
+    *alloced_size = object_size;
+}
+
+/* For a given size of memory requested for allocation, return the
+   actual size that is going to be allocated.  */
+
+size_t
+ggc_alloced_size_for_request (size_t requested_size)
+{
+  size_t size = 0;
+  
+  ggc_alloced_size_order_for_request (requested_size, NULL, &size);
+  return size;
+}
+
 /* Typed allocation function.  Does nothing special in this collector.  */
 
 void *
@@ -1072,17 +1113,7 @@ ggc_internal_alloc_stat (size_t size MEM_STAT_DECL)
   struct page_entry *entry;
   void *result;
 
-  if (size < NUM_SIZE_LOOKUP)
-    {
-      order = size_lookup[size];
-      object_size = OBJECT_SIZE (order);
-    }
-  else
-    {
-      order = 10;
-      while (size > (object_size = OBJECT_SIZE (order)))
-	order++;
-    }
+  ggc_alloced_size_order_for_request (size, &order, &object_size);
 
   /* If there are non-full pages for this size allocation, they are at
      the head of the list.  */
diff --git a/gcc/ggc-zone.c b/gcc/ggc-zone.c
index d0c1d79..ff7b466 100644
--- a/gcc/ggc-zone.c
+++ b/gcc/ggc-zone.c
@@ -1073,6 +1073,24 @@ free_chunk (char *ptr, size_t size, struct alloc_zone *zone)
     fprintf (G.debug_file, "Deallocating object, chunk=%p\n", (void *)chunk);
 }
 
+/* For a given size of memory requested for allocation, return the
+   actual size that is going to be allocated.  */
+
+size_t
+ggc_alloced_size_for_request (size_t requested_size)
+{
+  size_t size;
+
+  /* Make sure that zero-sized allocations get a unique and freeable
+     pointer.  */
+  if (requested_size == 0)
+    size = MAX_ALIGNMENT;
+  else
+    size = (requested_size + MAX_ALIGNMENT - 1) & -MAX_ALIGNMENT;
+
+  return size;
+}
+
 /* Allocate a chunk of memory of at least ORIG_SIZE bytes, in ZONE.  */
 
 void *
@@ -1084,14 +1102,7 @@ ggc_internal_alloc_zone_stat (size_t orig_size, struct alloc_zone *zone
   struct small_page_entry *entry;
   struct alloc_chunk *chunk, **pp;
   void *result;
-  size_t size = orig_size;
-
-  /* Make sure that zero-sized allocations get a unique and freeable
-     pointer.  */
-  if (size == 0)
-    size = MAX_ALIGNMENT;
-  else
-    size = (size + MAX_ALIGNMENT - 1) & -MAX_ALIGNMENT;
+  size_t size = ggc_alloced_size_for_request (orig_size);
 
   /* Try to allocate the object from several different sources.  Each
      of these cases is responsible for setting RESULT and SIZE to
diff --git a/gcc/ggc.h b/gcc/ggc.h
index 30eca66..9319dc1 100644
--- a/gcc/ggc.h
+++ b/gcc/ggc.h
@@ -145,6 +145,8 @@ extern void gt_pch_save (FILE *f);
 /* The internal primitive.  */
 extern void *ggc_internal_alloc_stat (size_t MEM_STAT_DECL);
 
+extern size_t ggc_alloced_size_for_request (size_t requested_size);
+
 #define ggc_internal_alloc(s) ggc_internal_alloc_stat (s MEM_STAT_INFO)
 
 /* Allocate an object of the specified type and size.  */
diff --git a/gcc/toplev.c b/gcc/toplev.c
index 2f90261..dd1eeab 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -1177,6 +1177,7 @@ general_init (const char *argv0)
   line_table = ggc_alloc_line_maps ();
   linemap_init (line_table);
   line_table->reallocator = realloc_for_line_map;
+  line_table->alloced_size_for_request = ggc_alloced_size_for_request;
   init_ttree ();
 
   /* Initialize register usage now so switches may override.  */
diff --git a/libcpp/include/line-map.h b/libcpp/include/line-map.h
index 3989c89..37a23f1 100644
--- a/libcpp/include/line-map.h
+++ b/libcpp/include/line-map.h
@@ -53,6 +53,10 @@ typedef unsigned int source_location;
 /* Memory allocation function typedef.  Works like xrealloc.  */
 typedef void *(*line_map_realloc) (void *, size_t);
 
+/* Memory allocator function that returns the actual allocated size,
+   for a given requested allocation.  */
+typedef size_t (*line_map_alloced_size_for_request_func) (size_t);
+
 /* An ordinary line map encodes physical source locations. Those
    physical source locations are called "spelling locations".
    
@@ -298,6 +302,10 @@ struct GTY(()) line_maps {
   /* If non-null, the allocator to use when resizing 'maps'.  If null,
      xrealloc is used.  */
   line_map_realloc reallocator;
+
+  /* The allocators' function used to know the actual size it
+     allocated, for a certain allocation size requested.  */
+  line_map_alloced_size_for_request_func alloced_size_for_request;
 };
 
 /* Returns the pointer to the memory region where information about
diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index 09dffcb..05f9dd8 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -35,6 +35,9 @@ static const struct line_map* linemap_macro_map_lookup (struct line_maps *,
 extern unsigned num_expanded_macros_counter;
 extern unsigned num_macro_tokens_counter;
 
+/* from ggc.h.  */
+size_t ggc_alloced_size_for_request (size_t requested_size);
+
 /* Initialize a line map set.  */
 
 void
@@ -77,16 +80,43 @@ new_linemap (struct line_maps *set,
   if (LINEMAPS_USED (set, macro_map_p) == LINEMAPS_ALLOCATED (set, macro_map_p))
     {
       /* We ran out of allocated line maps. Let's allocate more.  */
+      unsigned alloc_size;
 
       line_map_realloc reallocator
 	= set->reallocator ? set->reallocator : xrealloc;
+      line_map_alloced_size_for_request_func alloced_size_for_request =
+	set->alloced_size_for_request;
+
+      /* We are going to execute some dance to try to reduce the
+	 overhead of the memory allocator, in case we are using the
+	 ggc-page.c one.
+	 
+	 The actual size of memory we are going to get back from the
+	 allocator is the smallest power of 2 that is greater than the
+	 size we requested.  So let's consider that size then.  */
+
+      alloc_size =
+	(2 * LINEMAPS_ALLOCATED (set, macro_map_p) +  256)
+	* sizeof (struct line_map);
+
+      /* Get the actual size of memory that is going to be allocated
+	 by the allocator.  */
+      alloc_size = alloced_size_for_request (alloc_size);
+
+      /* Now alloc_size contains the exact memory size we would get if
+	 we have asked for the initial alloc_size amount of memory.
+	 Let's get back to the number of macro map that amounts
+	 to.  */
       LINEMAPS_ALLOCATED (set, macro_map_p) =
-	2 * LINEMAPS_ALLOCATED (set, macro_map_p) + 256;
-      LINEMAPS_MAPS (set, macro_map_p)
-	= (struct line_map *) (*reallocator) (LINEMAPS_MAPS (set, macro_map_p),
-					      LINEMAPS_ALLOCATED (set,
-								  macro_map_p)
-					      * sizeof (struct line_map));
+	alloc_size / (sizeof (struct line_map));
+
+      /* And now let's really do the re-allocation.  */
+      LINEMAPS_MAPS (set, macro_map_p) =
+	(struct line_map *) (*reallocator)
+	(LINEMAPS_MAPS (set, macro_map_p),
+	 (LINEMAPS_ALLOCATED (set, macro_map_p)
+	  * sizeof (struct line_map)));
+
       result =
 	&LINEMAPS_MAPS (set, macro_map_p)[LINEMAPS_USED (set, macro_map_p)];
       memset (result, 0,
-- 
		Dodji



More information about the Gcc-patches mailing list