]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
This patch adds a function that allows to directly properly size an xarray
authorPhilippe Waroquiers <philippe.waroquiers@skynet.be>
Fri, 1 May 2015 16:46:38 +0000 (16:46 +0000)
committerPhilippe Waroquiers <philippe.waroquiers@skynet.be>
Fri, 1 May 2015 16:46:38 +0000 (16:46 +0000)
when the size is known in advance.

3 places identified where this function can be used trivially.

The result is a reduction of 'realloc' operations in core
arena, and a small reduction in ttaux arena
(it is the nr of operations that decreases, the memory usage itself
stays the same (ignoring some 'rounding' effects).

E.g. for perf/bigcode 0, we change from
  core 1085742216745904 totalloc-blocks/bytes,     1085733 searches
  ttaux 5348/   6732560 totalloc-blocks/bytes,        5326 searches
to
  core 712666/ 190998592 totalloc-blocks/bytes,      712657 searches
  ttaux 5319/   6731808 totalloc-blocks/bytes,        5296 searches

For bz2, we switch from
  core 50285/  32383664 totalloc-blocks/bytes,       50256 searches
  ttaux 670/    245160 totalloc-blocks/bytes,         669 searches
to
  core 32564/  29971984 totalloc-blocks/bytes,       32535 searches
  ttaux 605/    243280 totalloc-blocks/bytes,         604 searches

Performance wise, on amd64, this improves memcheck performance
on perf tests by 0.0, 0.1 or 0.2 seconds depending on the test.

git-svn-id: svn://svn.valgrind.org/valgrind/trunk@15173

coregrind/m_transtab.c
coregrind/m_xarray.c
include/pub_tool_xarray.h
memcheck/mc_translate.c

index 0f6807eb9e53472d4176077b124252b7aa928fe5..a5f6f26fa6140c3116518526e2dbca0b8f64e463 100644 (file)
@@ -569,6 +569,7 @@ void InEdgeArr__add ( InEdgeArr* iea, InEdge* ie )
          XArray *var = VG_(newXA)(ttaux_malloc, "transtab.IEA__add",
                                   ttaux_free,
                                   sizeof(InEdge));
+         VG_(hintSizeXA) (var, iea->n_fixed + 1);
          UWord i;
          for (i = 0; i < iea->n_fixed; i++) {
             VG_(addToXA)(var, &iea->edges.fixed[i]);
@@ -649,6 +650,7 @@ void OutEdgeArr__add ( OutEdgeArr* oea, OutEdge* oe )
          XArray *var = VG_(newXA)(ttaux_malloc, "transtab.OEA__add",
                                   ttaux_free,
                                   sizeof(OutEdge));
+         VG_(hintSizeXA) (var, oea->n_fixed+1);
          UWord i;
          for (i = 0; i < oea->n_fixed; i++) {
             VG_(addToXA)(var, &oea->edges.fixed[i]);
index 18e3f6cb99198495307146b21091f4fc71ac3bc8..6a306f820142a15d50a0ba95f74f9eac0a5f64d0 100644 (file)
@@ -133,6 +133,24 @@ inline void* VG_(indexXA) ( const XArray* xa, Word n )
    return ((char*)xa->arr) + n * xa->elemSzB;
 }
 
+void VG_(hintSizeXA) ( XArray* xa, Word n)
+{
+   /* Currently, we support giving a size hint only just after the
+      call to VG_(newXA). So, we could instead have done
+      a function VG_(newXA_with_SizeHint). The separate VG_(hintSizeXA)
+      function is however chosen as we might one day accept to
+      give a size hint after having added elements. That could be useful
+      for reducing the size of an xarray to just the size currently needed
+      or to give a size hint when it is known that a lot more elements
+      are needed or when the final nr of elements is known. */
+   vg_assert(xa);
+   vg_assert(xa->usedsizeE == 0);
+   vg_assert(xa->totsizeE == 0);
+   vg_assert(!xa->arr);
+   xa->arr = xa->alloc_fn(xa->cc, n * xa->elemSzB);
+   xa->totsizeE = n;
+}
+
 static inline void ensureSpaceXA ( XArray* xa )
 {
    if (xa->usedsizeE == xa->totsizeE) {
index 2f429e4c618cc1644fe281467fdcb23071d711af..e3bc84e9cadbd184c85d5d2ca032f7e81472fd13 100644 (file)
@@ -104,6 +104,13 @@ extern Bool VG_(lookupXA_UNSAFE) ( const XArray* xao, const void* key,
 /* How elements are there in this XArray now? */
 extern Word VG_(sizeXA) ( const XArray* );
 
+/* If you know how many elements an XArray will have, you can
+   optimise memory usage and number of reallocation needed
+   to insert these elements. The call to VG_(hintSizeXA) must be
+   done just after the call to VG_(newXA), before any element
+   has been inserted. */
+extern void VG_(hintSizeXA) ( XArray*, Word);
+
 /* Index into the XArray.  Checks bounds and bombs if the index is
    invalid.  What this returns is the address of the specified element
    in the array, not (of course) the element itself.  Note that the
index 64d1fd4f5e87a11389b6af0ce3aa7f36cc0292d0..892b43b3be75fc065d125bab987ebc667bf77e6d 100644 (file)
@@ -6303,6 +6303,7 @@ IRSB* MC_(instrument) ( VgCallbackClosure* closure,
 
    mce.tmpMap = VG_(newXA)( VG_(malloc), "mc.MC_(instrument).1", VG_(free),
                             sizeof(TempMapEnt));
+   VG_(hintSizeXA) (mce.tmpMap, sb_in->tyenv->types_used);
    for (i = 0; i < sb_in->tyenv->types_used; i++) {
       TempMapEnt ent;
       ent.kind    = Orig;