This patch adds a function that allows to directly properly size an xarray
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 1085742/ 216745904 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 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/coregrind/m_transtab.c b/coregrind/m_transtab.c
index 0f6807e..a5f6f26 100644
--- a/coregrind/m_transtab.c
+++ b/coregrind/m_transtab.c
@@ -569,6 +569,7 @@
          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 @@
          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]);
diff --git a/coregrind/m_xarray.c b/coregrind/m_xarray.c
index 18e3f6c..6a306f8 100644
--- a/coregrind/m_xarray.c
+++ b/coregrind/m_xarray.c
@@ -133,6 +133,24 @@
    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) {
diff --git a/include/pub_tool_xarray.h b/include/pub_tool_xarray.h
index 2f429e4..e3bc84e 100644
--- a/include/pub_tool_xarray.h
+++ b/include/pub_tool_xarray.h
@@ -104,6 +104,13 @@
 /* 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
diff --git a/memcheck/mc_translate.c b/memcheck/mc_translate.c
index 64d1fd4..892b43b 100644
--- a/memcheck/mc_translate.c
+++ b/memcheck/mc_translate.c
@@ -6303,6 +6303,7 @@
 
    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;