List:Commits« Previous MessageNext Message »
From:Tor Didriksen Date:March 3 2011 9:18am
Subject:bzr commit into mysql-trunk branch (tor.didriksen:3722) WL#5774
View as plain text  
#At file:///export/home/didrik/repo/trunk-dynarray/ based on revid:anitha.gopi@stripped

 3722 Tor Didriksen	2011-03-03
         WL #5774 Decrease number of malloc's for normal DML queries
         One of the malloc's was due to DYNAMIC_ARRAY keyuse;
         Here's an experiment to use an array allocated in thd->mem_root instead.

    added:
      sql/mem_root_array.h
      unittest/gunit/dynarray-t.cc
    modified:
      sql/sql_select.cc
      sql/sql_select.h
      sql/sql_test.cc
      sql/sql_test.h
      unittest/gunit/CMakeLists.txt
=== added file 'sql/mem_root_array.h'
--- a/sql/mem_root_array.h	1970-01-01 00:00:00 +0000
+++ b/sql/mem_root_array.h	2011-03-03 09:18:10 +0000
@@ -0,0 +1,139 @@
+/* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; version 2 of the License.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
+
+
+#ifndef MEM_ROOT_ARRAY_INCLUDED
+#define MEM_ROOT_ARRAY_INCLUDED
+
+#include <my_alloc.h>
+
+/**
+   A typesafe replacement for DYNAMIC_ARRAY.
+   We use MEM_ROOT for allocating storage, rather than the C++ heap.
+   The interface is chosen to be similar to std::vector.
+
+   Note that MEM_ROOT has no facility for reusing free space,
+   so don't use this if multiple re-expansions are likely to happen.
+*/
+template<typename Element_type>
+class Mem_root_array
+{
+public:
+  Mem_root_array()
+    : m_root(NULL), m_array(NULL), m_size(0), m_capacity(0)
+  {}
+
+  Mem_root_array(MEM_ROOT *root)
+    : m_root(root), m_array(NULL), m_size(0), m_capacity(0)
+  {}
+
+  ~Mem_root_array()
+  {
+    clear();
+  }
+
+  void set_mem_root(MEM_ROOT *root) { m_root= root; }
+
+  Element_type &at(size_t n)
+  {
+    DBUG_ASSERT(n < size());
+    return m_array[n];
+  }
+
+  const Element_type &at(size_t n) const
+  {
+    DBUG_ASSERT(n < size());
+    return m_array[n];
+  }
+
+  // Returns a pointer to the first element in the array.
+  Element_type *begin() { return &m_array[0]; }
+
+  // Returns an pointer to the past-the-end element in the array.
+  Element_type *end() { return &m_array[size()]; }
+
+  // Clear array contents.
+  void clear()
+  {
+    chop(0);
+  }
+
+  // Chops the tail off the array.
+  void chop(size_t pos)
+  {
+    if (pos >= m_size)
+      return;
+    for (size_t ix= pos; pos < m_size; ++pos)
+    {
+      Element_type *p= &m_array[ix];
+      p->~Element_type();                       // Delete discarded element.
+    }
+    m_size= pos;
+  }
+
+  // Reserves space for array elements.
+  // Copies over existing elements, in case we are re-expanding the array.
+  bool reserve(size_t n)
+  {
+    if (n <= m_capacity)
+      return false;
+
+    void *mem= alloc_root(m_root, n * element_size());
+    if (!mem)
+      return true;
+    Element_type *array= static_cast<Element_type*>(mem);
+
+    // Copy all the existing elements into the new array.
+    for (size_t ix= 0; ix < m_size; ++ix)
+    {
+      Element_type *new_p= &array[ix];
+      Element_type *old_p= &m_array[ix];
+      new (new_p) Element_type(*old_p);         // Copy into new location.
+      old_p->~Element_type();                   // Delete the old element.
+    }
+
+    // Forget the old array.
+    m_array= array;
+    m_capacity= n;
+    return false;
+  }
+
+  // Adds a new element at the end of the array, after its current last
+  // element. The content of this new element is initialized to a copy of
+  // the input argument.
+  bool push_back(const Element_type &element)
+  {
+    if (0 == m_capacity && reserve(10))
+      return true;
+    if (m_size == m_capacity && reserve(m_capacity * 2))
+      return true;
+    Element_type *p= &m_array[m_size++];
+    new (p) Element_type(element);
+    return false;
+  }
+
+  size_t element_size() const { return sizeof(Element_type); }
+  size_t size()         const { return m_size; }
+  size_t capacity()     const { return m_capacity; }
+
+private:
+  MEM_ROOT     *m_root;
+  Element_type *m_array;
+  size_t        m_size;
+  size_t        m_capacity;
+};
+
+
+#endif  // MEM_ROOT_ARRAY_INCLUDED

=== modified file 'sql/sql_select.cc'
--- a/sql/sql_select.cc	2011-03-01 14:47:01 +0000
+++ b/sql/sql_select.cc	2011-03-03 09:18:10 +0000
@@ -63,11 +63,11 @@ const char *join_type_str[]={ "UNKNOWN",
 
 struct st_sargable_param;
 
-static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array);
+static void optimize_keyuse(JOIN *join, Key_use_array *keyuse_array);
 static bool make_join_statistics(JOIN *join, TABLE_LIST *leaves, Item *conds,
-				 DYNAMIC_ARRAY *keyuse);
+				 Key_use_array *keyuse);
 static bool optimize_semijoin_nests(JOIN *join, table_map all_table_map);
-static bool update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,
+static bool update_ref_and_keys(THD *thd, Key_use_array *keyuse,
                                 JOIN_TAB *join_tab,
                                 uint tables, Item *conds,
                                 COND_EQUAL *cond_equal,
@@ -3441,7 +3441,7 @@ JOIN::destroy()
   while ((sj_nest= sj_list_it++))
     sj_nest->sj_mat_exec= NULL;
 
-  delete_dynamic(&keyuse);
+  keyuse.clear();
   delete procedure;
   DBUG_RETURN(error);
 }
@@ -4598,7 +4598,7 @@ static uint get_tmp_table_rec_length(Lis
 
 static bool
 make_join_statistics(JOIN *join, TABLE_LIST *tables_arg, Item *conds,
-		     DYNAMIC_ARRAY *keyuse_array)
+		     Key_use_array *keyuse_array)
 {
   int error;
   TABLE *table;
@@ -5985,7 +5985,7 @@ max_part_bit(key_part_map bits)
 */
 
 static bool
-add_key_part(DYNAMIC_ARRAY *keyuse_array,KEY_FIELD *key_field)
+add_key_part(Key_use_array *keyuse_array,KEY_FIELD *key_field)
 {
   Field *field=key_field->field;
   TABLE *form= field->table;
@@ -6015,7 +6015,7 @@ add_key_part(DYNAMIC_ARRAY *keyuse_array
                                key_field->null_rejecting,
                                key_field->cond_guard,
                                key_field->sj_pred_no);
-          if (insert_dynamic(keyuse_array, &keyuse))
+          if (keyuse_array->push_back(keyuse))
             return TRUE;
 	}
       }
@@ -6028,7 +6028,7 @@ add_key_part(DYNAMIC_ARRAY *keyuse_array
 #define FT_KEYPART   (MAX_REF_PARTS+10)
 
 static bool
-add_ft_keys(DYNAMIC_ARRAY *keyuse_array,
+add_ft_keys(Key_use_array *keyuse_array,
             JOIN_TAB *stat,Item *cond,table_map usable_tables)
 {
   Item_func_match *cond_func=NULL;
@@ -6090,7 +6090,7 @@ add_ft_keys(DYNAMIC_ARRAY *keyuse_array,
                        false,         // null_rejecting
                        NULL,          // cond_guard
                        UINT_MAX);     // sj_pred_no
-  return insert_dynamic(keyuse_array, &keyuse);
+  return keyuse_array->push_back(keyuse);
 }
 
 
@@ -6206,7 +6206,7 @@ static void add_key_fields_for_nj(JOIN *
 */
 
 static bool
-update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,JOIN_TAB *join_tab,
+update_ref_and_keys(THD *thd, Key_use_array *keyuse,JOIN_TAB *join_tab,
                     uint tables, Item *cond, COND_EQUAL *cond_equal,
                     table_map normal_tables, SELECT_LEX *select_lex,
                     SARGABLE_PARAM **sargables)
@@ -6249,7 +6249,8 @@ update_ref_and_keys(THD *thd, DYNAMIC_AR
   /* set a barrier for the array of SARGABLE_PARAM */
   (*sargables)[0].field= 0; 
 
-  if (my_init_dynamic_array(keyuse, sizeof(Key_use), 20, 64))
+  keyuse->set_mem_root(thd->mem_root);
+  if (keyuse->reserve(20))
     return TRUE;
   if (cond)
   {
@@ -6324,21 +6325,21 @@ update_ref_and_keys(THD *thd, DYNAMIC_AR
       used in the query, we drop the partial key parts from consideration).
     Special treatment for ft-keys.
   */
-  if (keyuse->elements)
+  if (keyuse->size())
   {
     Key_use *save_pos, *use;
 
-    my_qsort(keyuse->buffer, keyuse->elements, sizeof(Key_use),
+    my_qsort(keyuse->begin(), keyuse->size(), keyuse->element_size(),
              reinterpret_cast<qsort_cmp>(sort_keyuse));
 
     const Key_use key_end(NULL, NULL, 0, 0, 0, 0, 0, 0, false, NULL, 0);
-    if (insert_dynamic(keyuse, &key_end)) // added for easy testing
+    if (keyuse->push_back(key_end)) // added for easy testing
       return TRUE;
 
-    use= save_pos= dynamic_element(keyuse, 0, Key_use *);
+    use= save_pos= keyuse->begin();
     const Key_use *prev= &key_end;
     found_eq_constant=0;
-    for (i=0 ; i < keyuse->elements-1 ; i++,use++)
+    for (i=0 ; i < keyuse->size()-1 ; i++,use++)
     {
       if (!use->used_tables && use->optimize != KEY_OPTIMIZE_REF_OR_NULL)
 	use->table->const_key_parts[use->key]|= use->keypart_map;
@@ -6371,9 +6372,9 @@ update_ref_and_keys(THD *thd, DYNAMIC_AR
       use->table->reginfo.join_tab->checked_keys.set_bit(use->key);
       save_pos++;
     }
-    i= (uint) (save_pos - (Key_use *)keyuse->buffer);
-    (void) set_dynamic(keyuse, &key_end, i);
-    keyuse->elements=i;
+    i= (uint) (save_pos - keyuse->begin());
+    keyuse->at(i) = key_end;
+    keyuse->chop(i);
   }
   DBUG_EXECUTE("opt", print_keyuse_array(keyuse););
   return FALSE;
@@ -6383,12 +6384,11 @@ update_ref_and_keys(THD *thd, DYNAMIC_AR
   Update some values in keyuse for faster choose_plan() loop.
 */
 
-static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array)
+static void optimize_keyuse(JOIN *join, Key_use_array *keyuse_array)
 {
-  Key_use *end, *keyuse= dynamic_element(keyuse_array, 0, Key_use *);
-
-  for (end= keyuse+ keyuse_array->elements ; keyuse < end ; keyuse++)
+  for (size_t ix= 0; ix < keyuse_array->size(); ++ix)
   {
+    Key_use *keyuse= &keyuse_array->at(ix);
     table_map map;
     /*
       If we find a ref, assume this table matches a proportional

=== modified file 'sql/sql_select.h'
--- a/sql/sql_select.h	2011-02-08 15:49:51 +0000
+++ b/sql/sql_select.h	2011-03-03 09:18:10 +0000
@@ -34,6 +34,7 @@
 #include "records.h"                          /* READ_RECORD */
 #include "opt_range.h"                /* SQL_SELECT, QUICK_SELECT_I */
 
+#include "mem_root_array.h"
 
 /* Values in optimize */
 #define KEY_OPTIMIZE_EXISTS		1
@@ -47,6 +48,8 @@
 */
 class Key_use {
 public:
+  Key_use() { memset(this, 0, sizeof(*this)); }
+
   Key_use(TABLE *table_arg, Item *val_arg, table_map used_tables_arg,
           uint key_arg, uint keypart_arg, uint optimize_arg,
           key_part_map keypart_map_arg, ha_rows ref_table_rows_arg,
@@ -97,6 +100,10 @@ public:
   uint         sj_pred_no;
 };
 
+
+typedef Mem_root_array<Key_use> Key_use_array;
+void print_keyuse_array(Key_use_array *keyuse_array);
+
 class store_key;
 
 typedef struct st_table_ref : public Sql_alloc
@@ -1772,7 +1779,10 @@ public:
   bool          skip_sort_order;
 
   bool need_tmp, hidden_group_fields;
-  DYNAMIC_ARRAY keyuse;
+
+  // DYNAMIC_ARRAY keyuse;
+  Key_use_array keyuse;
+
   List<Item> all_fields; ///< to store all fields that used in query
   ///Above list changed to use temporary table
   List<Item> tmp_all_fields1, tmp_all_fields2, tmp_all_fields3;

=== modified file 'sql/sql_test.cc'
--- a/sql/sql_test.cc	2010-11-05 22:19:41 +0000
+++ b/sql/sql_test.cc	2011-03-03 09:18:10 +0000
@@ -239,7 +239,7 @@ TEST_join(JOIN *join)
 
 #define FT_KEYPART   (MAX_REF_PARTS+10)
 
-void print_keyuse(Key_use *keyuse)
+void print_keyuse(const Key_use *keyuse)
 {
   char buff[256];
   char buf2[64]; 
@@ -276,6 +276,16 @@ void print_keyuse_array(DYNAMIC_ARRAY *k
                  (dynamic_array_ptr(keyuse_array, i)));
 }
 
+void print_keyuse_array(Key_use_array *keyuse_array)
+{
+  DBUG_LOCK_FILE;
+  fprintf(DBUG_FILE, "Key_use array (%d elements)\n",
+          (int) keyuse_array->size());
+  DBUG_UNLOCK_FILE;
+  for(uint i=0; i < keyuse_array->size(); i++)
+    print_keyuse(&keyuse_array->at(i));
+}
+
 
 /* 
   Print the current state during query optimization.

=== modified file 'sql/sql_test.h'
--- a/sql/sql_test.h	2010-08-19 07:10:58 +0000
+++ b/sql/sql_test.h	2011-03-03 09:18:10 +0000
@@ -20,6 +20,7 @@
 
 class JOIN;
 struct TABLE_LIST;
+class Key_use;
 typedef class st_select_lex SELECT_LEX;
 typedef struct st_sort_field SORT_FIELD;
 
@@ -32,6 +33,7 @@ void print_plan(JOIN* join,uint idx, dou
 void dump_TABLE_LIST_graph(SELECT_LEX *select_lex, TABLE_LIST* tl);
 void print_sjm(TABLE_LIST *emb_sj_nest);
 void print_keyuse_array(DYNAMIC_ARRAY *keyuse_array);
+void print_keyuse(const Key_use *keyuse);
 #endif
 void mysql_print_status();
 

=== modified file 'unittest/gunit/CMakeLists.txt'
--- a/unittest/gunit/CMakeLists.txt	2011-02-17 14:39:47 +0000
+++ b/unittest/gunit/CMakeLists.txt	2011-03-03 09:18:10 +0000
@@ -210,6 +210,7 @@ ENDIF()
 SET(TESTS
   bounded_queue
   dbug
+  dynarray
   mdl
   mdl_mytap
   my_bitmap

=== added file 'unittest/gunit/dynarray-t.cc'
--- a/unittest/gunit/dynarray-t.cc	1970-01-01 00:00:00 +0000
+++ b/unittest/gunit/dynarray-t.cc	2011-03-03 09:18:10 +0000
@@ -0,0 +1,313 @@
+/* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; version 2 of the License.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
+
+// First include (the generated) my_config.h, to get correct platform defines,
+// then gtest.h (before any other MySQL headers), to avoid min() macros etc ...
+#include "my_config.h"
+#include <gtest/gtest.h>
+
+#include <algorithm>
+#include <functional>
+#include <vector>
+
+#include "sql_select.h"
+#include "mem_root_array.h"
+/**
+   WL #5774 Decrease number of malloc's for normal DML queries
+   One of the malloc's was due to DYNAMIC_ARRAY keyuse;
+   Here's an experiment to use an array allocated in thd->mem_root instead.
+
+   This is the interface we need to support:
+   struct XX_DYNAMIC_ARRAY
+   {
+     ....
+   };
+
+   void delete_dynamic(XX_DYNAMIC_ARRAY*);
+        dynamic_element(array,array_index,type);
+   bool init_dynamic_array2(XX_DYNAMIC_ARRAY*, uint, void*, uint, uint);
+   bool insert_dynamic(XX_DYNAMIC_ARRAY*, const Key_use*);
+   bool set_dynamic(XX_DYNAMIC_ARRAY*, const void*, uint);
+   void print_keyuse_array(XX_DYNAMIC_ARRAY*);
+*/
+
+pthread_key(MEM_ROOT**, THR_MALLOC);
+pthread_key(THD*, THR_THD);
+
+extern "C" void sql_alloc_error_handler(void)
+{
+  ADD_FAILURE();
+}
+
+// Cut'n paste this function from sql_select.cc,
+// to avoid linking in the entire server for this unit test.
+inline int sort_keyuse(Key_use *a, Key_use *b)
+{
+  int res;
+  if (a->table->tablenr != b->table->tablenr)
+    return (int) (a->table->tablenr - b->table->tablenr);
+  if (a->key != b->key)
+    return (int) (a->key - b->key);
+  if (a->keypart != b->keypart)
+    return (int) (a->keypart - b->keypart);
+  // Place const values before other ones
+  if ((res= test((a->used_tables & ~OUTER_REF_TABLE_BIT)) -
+       test((b->used_tables & ~OUTER_REF_TABLE_BIT))))
+    return res;
+  /* Place rows that are not 'OPTIMIZE_REF_OR_NULL' first */
+  return (int) ((a->optimize & KEY_OPTIMIZE_REF_OR_NULL) -
+		(b->optimize & KEY_OPTIMIZE_REF_OR_NULL));
+}
+
+// Rewrite of sort_keyuse() to comparison operator for use by std::less<>
+inline bool operator<(const Key_use &a, const Key_use &b)
+{
+  if (a.table->tablenr != b.table->tablenr)
+    return a.table->tablenr < b.table->tablenr;
+  if (a.key != b.key)
+    return a.key < b.key;
+  if (a.keypart != b.keypart)
+    return a.keypart < b.keypart;
+  const bool atab = test((a.used_tables & ~OUTER_REF_TABLE_BIT));
+  const bool btab = test((b.used_tables & ~OUTER_REF_TABLE_BIT));
+  if (atab != btab)
+    return atab < btab;
+  return
+    ((a.optimize & KEY_OPTIMIZE_REF_OR_NULL) <
+     (b.optimize & KEY_OPTIMIZE_REF_OR_NULL));
+}
+
+namespace {
+
+// Play around with these constants to see std::sort speedup vs. my_qsort.
+const int num_elements= 200;
+const int num_iterations= 1;
+
+// We generate some random data at startup, for testing of sorting.
+Key_use test_data[num_elements];
+TABLE   table_list[num_elements];
+
+
+class DynArrayTest : public ::testing::Test
+{
+public:
+  static void SetUpTestCase()
+  {
+    int ix;
+    for (ix= 0; ix < num_elements; ++ix)
+    {
+      table_list[ix].tablenr= ix % 3;
+      test_data[ix]=
+        Key_use(&table_list[ix],
+                NULL,                           // Item      *val
+                0,                              // table_map  used_tables
+                ix % 4,                         // uint       key
+                ix % 2,                         // uint       keypart
+                0,                              // uint       optimize
+                0,                              //            keypart_map
+                0,                              // ha_rows    ref_table_rows
+                true,                           // bool       null_rejecting
+                NULL,                           // bool      *cond_guard
+                0                               // uint       sj_pred_no
+                );
+    }
+    std::random_shuffle(&test_data[0], &test_data[num_elements]);
+  }
+
+  virtual void SetUp()
+  {
+    my_init_dynamic_array(&m_keyuse_dyn, sizeof(Key_use), num_elements, 64);
+    m_keyuse_vec.reserve(num_elements);
+  }
+
+  void insert_and_sort_dynamic()
+  {
+    reset_dynamic(&m_keyuse_dyn);
+    for (int ix= 0; ix < num_elements; ++ix)
+    {
+      insert_dynamic(&m_keyuse_dyn, &test_data[ix]);
+    }
+    my_qsort(m_keyuse_dyn.buffer, m_keyuse_dyn.elements, sizeof(Key_use),
+             reinterpret_cast<qsort_cmp>(sort_keyuse));
+  }
+
+  void insert_and_sort_vector()
+  {
+    m_keyuse_vec.clear();
+    for (int ix= 0; ix < num_elements; ++ix)
+    {
+      m_keyuse_vec.push_back(test_data[ix]);
+    }
+    std::sort(m_keyuse_vec.begin(), m_keyuse_vec.end(), std::less<Key_use>());
+  }
+
+  DYNAMIC_ARRAY        m_keyuse_dyn;
+  std::vector<Key_use> m_keyuse_vec;
+};
+
+
+void print_key(uint ix, Key_use *key)
+{
+  fprintf(stdout, "ix %d table %d key %d\n", ix, key->table->tablenr, key->key);
+  fflush(stdout);
+}
+
+void print_array(std::vector<Key_use> *arr)
+{
+  for (uint ix= 0; ix < arr->size(); ++ix)
+  {
+    Key_use &key_use = (*arr)[ix];
+    print_key(ix, &key_use);
+  }
+}
+
+void print_array(DYNAMIC_ARRAY *arr)
+{
+  for (uint ix= 0; ix < arr->elements; ++ix)
+  {
+    Key_use key_use;
+    void *p = &key_use;
+    get_dynamic(arr, static_cast<uchar*>(p), ix);
+    print_key(ix, &key_use);
+  }
+}
+
+void print_array(Mem_root_array<Key_use> *arr)
+{
+  for (uint ix= 0; ix < arr->size(); ++ix)
+  {
+    Key_use &key_use= arr->at(ix);
+    print_key(ix, &key_use);
+  }
+}
+
+
+// Test insert_dynamic() and my_qsort().
+TEST_F(DynArrayTest, DynArray)
+{
+  for (int ix= 0; ix < num_iterations; ++ix)
+    insert_and_sort_dynamic();
+  // print_array(&m_keyuse_dyn);
+}
+
+
+// Test vector::push_back() and std::sort()
+TEST_F(DynArrayTest, Vector)
+{
+  for (int ix= 0; ix < num_iterations; ++ix)
+    insert_and_sort_vector();
+  // print_array(&m_keyuse_vec);
+}
+
+
+class MemRootTest : public ::testing::Test
+{
+protected:
+  MemRootTest()
+    : m_mem_root_p(&m_mem_root), m_array(m_mem_root_p)
+  {}
+
+  virtual void SetUp()
+  {
+    init_sql_alloc(&m_mem_root, 1024, 0);
+    ASSERT_EQ(0, my_pthread_setspecific_ptr(THR_MALLOC, &m_mem_root_p));
+    MEM_ROOT *root= *my_pthread_getspecific_ptr(MEM_ROOT**, THR_MALLOC);
+    ASSERT_EQ(root, m_mem_root_p);
+
+    m_array.reserve(num_elements);
+  }
+
+  virtual void TearDown()
+  {
+    free_root(&m_mem_root, MYF(0));
+  }
+
+  static void SetUpTestCase()
+  {
+    ASSERT_EQ(0, pthread_key_create(&THR_THD, NULL));
+    ASSERT_EQ(0, pthread_key_create(&THR_MALLOC, NULL));
+  }
+
+  static void TearDownTestCase()
+  {
+    pthread_key_delete(THR_THD);
+    pthread_key_delete(THR_MALLOC);
+  }
+
+  void insert_and_sort_mysys()
+  {
+    m_array.clear();
+    for (int ix= 0; ix < num_elements; ++ix)
+    {
+      m_array.push_back(test_data[ix]);
+    }
+    my_qsort(m_array.begin(), m_array.size(), m_array.element_size(),
+             reinterpret_cast<qsort_cmp>(sort_keyuse));
+  }
+
+  void insert_and_sort_std()
+  {
+    m_array.clear();
+    for (int ix= 0; ix < num_elements; ++ix)
+    {
+      m_array.push_back(test_data[ix]);
+    }
+    std::sort(m_array.begin(), m_array.end(), std::less<Key_use>());
+  }
+
+  MEM_ROOT m_mem_root;
+  MEM_ROOT *m_mem_root_p;
+  Mem_root_array<Key_use> m_array;
+};
+
+
+// Test Mem_root_array::push_back() and my_qsort()
+TEST_F(MemRootTest, KeyUseMysys)
+{
+  for (int ix= 0; ix < num_iterations; ++ix)
+    insert_and_sort_mysys();
+  // print_array(&m_array);
+}
+
+
+// Test Mem_root_array::push_back() and std::sort()
+TEST_F(MemRootTest, KeyUseStd)
+{
+  for (int ix= 0; ix < num_iterations; ++ix)
+    insert_and_sort_std();
+  // print_array(&m_array);
+}
+
+
+// Test that Mem_root_array re-expanding works.
+TEST_F(MemRootTest, Reserve)
+{
+  Mem_root_array<uint> intarr;
+  intarr.set_mem_root(m_mem_root_p);
+  intarr.reserve(2);
+  for (uint ix=0; ix < 20; ++ix)
+  {
+    EXPECT_EQ(ix, intarr.size());
+    EXPECT_FALSE(intarr.push_back(ix));
+    EXPECT_EQ(ix, intarr.at(ix));
+  }
+  for (uint ix=0; ix < 20; ++ix)
+  {
+    EXPECT_EQ(ix, intarr.at(ix));
+  }
+}
+
+
+}


Attachment: [text/bzr-bundle] bzr/tor.didriksen@oracle.com-20110303091810-ppodne0mefr89yrp.bundle
Thread
bzr commit into mysql-trunk branch (tor.didriksen:3722) WL#5774Tor Didriksen3 Mar