From 7b047a31a09a7999353232cb6407b4e6338d5b70 Mon Sep 17 00:00:00 2001 From: Michael Widenius Date: Mon, 8 Nov 2010 13:43:54 +0200 Subject: Make SQLString reallocation addaptive Avoid doing reallocs Prealloc some strings / provide extension allocation size to some strings This gave a 25 % speedup in some mysql-test-run tests. mysys/safemalloc.c: More DBUG_PRINT sql/net_serv.cc: Make all mallocs() look the similar. (just-for-safety fix) sql/protocol.cc: Ensure that communication packet buffer is allocated. (It's freed by stored precedures and some DLL statements) sql/sp.cc: Fixed valgrind warning sql/sql_select.cc: Set extent allocation for buffer that has a lot of append() calls. sql/sql_show.cc: Fixed wrong usage of string buffer. Old code worked in test suite 'just-by-chance' sql/sql_string.cc: Call realloc_with_extra_if_needed() in append() functions. sql/sql_string.h: Added 'extra_alloc' member, to specify chunck size for realloc(). extra_alloc is addaptive to catch cases where preallocation of buffers is not done properly. Simplified free() to allow compiler to optimize things better (and to keep things consistent). Fixed shrink() to take into account the extra memory added to the Alloced_length in realloc(). This saves us a realloc() per query. sql/sql_test.cc: Set extent allocation for buffer that has a lot of append() calls. sql/table.cc: Set extent allocation for buffer that has a lot of append() calls. --- sql/sql_string.h | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) (limited to 'sql/sql_string.h') diff --git a/sql/sql_string.h b/sql/sql_string.h index e595de47f99..dc9122a4fcb 100644 --- a/sql/sql_string.h +++ b/sql/sql_string.h @@ -53,23 +53,24 @@ uint convert_to_printable(char *to, size_t to_len, class String { char *Ptr; - uint32 str_length,Alloced_length; + uint32 str_length,Alloced_length, extra_alloc; bool alloced; CHARSET_INFO *str_charset; public: String() { - Ptr=0; str_length=Alloced_length=0; alloced=0; + Ptr=0; str_length=Alloced_length=extra_alloc=0; alloced=0; str_charset= &my_charset_bin; } String(uint32 length_arg) { - alloced=0; Alloced_length=0; (void) real_alloc(length_arg); + alloced=0; Alloced_length= extra_alloc= 0; (void) real_alloc(length_arg); str_charset= &my_charset_bin; } String(const char *str, CHARSET_INFO *cs) { - Ptr=(char*) str; str_length=(uint) strlen(str); Alloced_length=0; alloced=0; + Ptr=(char*) str; str_length= (uint32) strlen(str); + Alloced_length= extra_alloc= 0; alloced=0; str_charset=cs; } /* @@ -79,18 +80,18 @@ public: */ String(const char *str,uint32 len, CHARSET_INFO *cs) { - Ptr=(char*) str; str_length=len; Alloced_length=0; alloced=0; + Ptr=(char*) str; str_length=len; Alloced_length= extra_alloc=0; alloced=0; str_charset=cs; } String(char *str,uint32 len, CHARSET_INFO *cs) { - Ptr=(char*) str; Alloced_length=str_length=len; alloced=0; + Ptr=(char*) str; Alloced_length=str_length=len; extra_alloc= 0; alloced=0; str_charset=cs; } String(const String &str) { Ptr=str.Ptr ; str_length=str.str_length ; - Alloced_length=str.Alloced_length; alloced=0; + Alloced_length=str.Alloced_length; extra_alloc= 0; alloced=0; str_charset=str.str_charset; } static void *operator new(size_t size, MEM_ROOT *mem_root) throw () @@ -106,8 +107,10 @@ public: inline CHARSET_INFO *charset() const { return str_charset; } inline uint32 length() const { return str_length;} inline uint32 alloced_length() const { return Alloced_length;} + inline uint32 extra_allocation() const { return extra_alloc;} inline char& operator [] (uint32 i) const { return Ptr[i]; } inline void length(uint32 len) { str_length=len ; } + inline void extra_allocation(uint32 len) { extra_alloc= len; } inline bool is_empty() const { return (str_length == 0); } inline void mark_as_const() { Alloced_length= 0;} inline const char *ptr() const { return Ptr; } @@ -136,23 +139,21 @@ public: { DBUG_ASSERT(&str != this); free(); - Ptr=(char*) str.ptr()+offset; str_length=arg_length; alloced=0; + Ptr=(char*) str.ptr()+offset; str_length=arg_length; if (str.Alloced_length) Alloced_length=str.Alloced_length-offset; - else - Alloced_length=0; str_charset=str.str_charset; } inline void set(char *str,uint32 arg_length, CHARSET_INFO *cs) { free(); - Ptr=(char*) str; str_length=Alloced_length=arg_length ; alloced=0; + Ptr=(char*) str; str_length=Alloced_length=arg_length; str_charset=cs; } inline void set(const char *str,uint32 arg_length, CHARSET_INFO *cs) { free(); - Ptr=(char*) str; str_length=arg_length; Alloced_length=0 ; alloced=0; + Ptr=(char*) str; str_length=arg_length; str_charset=cs; } bool set_ascii(const char *str, uint32 arg_length); @@ -203,11 +204,11 @@ public: if (alloced) { alloced=0; - Alloced_length=0; my_free(Ptr,MYF(0)); - Ptr=0; - str_length=0; /* Safety */ } + Alloced_length= extra_alloc= 0; + Ptr=0; + str_length=0; /* Safety */ } inline bool alloc(uint32 arg_length) { @@ -217,9 +218,21 @@ public: } bool real_alloc(uint32 arg_length); // Empties old string bool realloc(uint32 arg_length); - inline void shrink(uint32 arg_length) // Shrink buffer + bool realloc_with_extra(uint32 arg_length) + { + if (extra_alloc < 4096) + extra_alloc= extra_alloc*2+128; + return realloc(arg_length + extra_alloc); + } + bool realloc_with_extra_if_needed(uint32 arg_length) { if (arg_length < Alloced_length) + return 0; + return realloc_with_extra(arg_length); + } + inline void shrink(uint32 arg_length) // Shrink buffer + { + if (ALIGN_SIZE(arg_length+1) < Alloced_length) { char *new_ptr; if (!(new_ptr=(char*) my_realloc(Ptr,arg_length,MYF(0)))) @@ -246,7 +259,6 @@ public: DBUG_ASSERT(!s.uses_buffer_owned_by(this)); free(); Ptr=s.Ptr ; str_length=s.str_length ; Alloced_length=s.Alloced_length; - alloced=0; } return *this; } @@ -281,7 +293,7 @@ public: } else { - if (realloc(str_length+1)) + if (realloc_with_extra(str_length + 1)) return 1; Ptr[str_length++]=chr; } -- cgit v1.2.1 From ff3da0f963b28d128005da8ca5d0c93a72d34b27 Mon Sep 17 00:00:00 2001 From: Michael Widenius Date: Mon, 28 Feb 2011 12:48:50 +0200 Subject: Change TABLE->alias to String for less memory reallocation Changed some String.ptr() -> String.c_ptr() for String that are not guaranteed to end with \0 Removed some c_ptr() usage from parameters to functions that takes ptr & length Use preallocate buffers to avoid calling malloc() for most operations. sql/event_db_repository.cc: alias is now a String sql/event_scheduler.cc: c_ptr -> c_ptr_safe() to avoid warnings from valgrind. sql/events.cc: c_ptr -> c_ptr_safe() to avoid warnings from valgrind. c_ptr -> ptr() as function takes ptr & length sql/field.cc: alias is now a String sql/field.h: alias is now a String sql/ha_partition.cc: alias is now a String sql/handler.cc: alias is now a String ptr() -> c_ptr() as string is not guaranteed to be \0 terminated sql/item.cc: Store error parameter in separarte buffer to ensure correct error message sql/item_func.cc: ptr() -> c_ptr_safe() as string is not guaranteed to be \0 terminated sql/item_sum.h: Use my_strtod() instead of my_atof() to not have to make string \0 terminated sql/lock.cc: alias is now a String sql/log.cc: c_ptr() -> ptr() as function takes ptr & length sql/log_event.cc: c_ptr_quick() -> ptr() as we only want to get the pointer to String buffer sql/opt_range.cc: ptr() -> c_ptr() as string is not guaranteed to be \0 terminated sql/opt_table_elimination.cc: alias is now a String sql/set_var.cc: ptr() -> c_ptr() as string is not guaranteed to be \0 terminated c_ptr() -> c_ptr_safe() to avoid warnings from valgrind. c_ptr() -> ptr() as function takes ptr & length Simplify some code. sql/sp.cc: c_ptr() -> ptr() as function takes ptr & length sql/sp_rcontext.cc: alias is now a String sql/sql_base.cc: alias is now a String. Here we win a realloc() for most alias usage. sql/sql_class.cc: Use size descriptor for printf() to avoid accessing bytes outside of buffer sql/sql_insert.cc: Change allocation of TABLE as it's now contains a String _ptr() -> ptr() as function takes ptr & length sql/sql_load.cc: Use preallocate buffers to avoid calling malloc() for most operations. sql/sql_parse.cc: Use c_ptr_safe() to ensure string is \0 terminated. sql/sql_plugin.cc: c_ptr_quick() -> ptr() as function takes ptr & length sql/sql_select.cc: alias is now a String sql/sql_show.cc: alias is now a String sql/sql_string.h: Added move() function to change who owns the string (owner does the free) sql/sql_table.cc: alias is now a String c_ptr() -> c_ptr_safe() to avoid warnings from valgrind. sql/sql_test.cc: c_ptr() -> c_ptr_safe() to avoid warnings from valgrind. alias is now a String sql/sql_trigger.cc: c_ptr() -> c_ptr_safe() to avoid warnings from valgrind. Use field->init() to setup pointers to alias. sql/sql_update.cc: alias is now a String sql/sql_view.cc: ptr() -> c_ptr_safe() as string is not guaranteed to be \0 terminated sql/sql_yacc.yy: r() -> c_ptr() as string is not guaranteed to be \0 terminated sql/table.cc: alias is now a String sql/table.h: alias is now a String storage/federatedx/ha_federatedx.cc: Remove extra 1 byte alloc that is automaticly done by strmake() Ensure that error message ends with \0 storage/maria/ha_maria.cc: alias is now a String storage/myisam/ha_myisam.cc: alias is now a String --- sql/sql_string.h | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'sql/sql_string.h') diff --git a/sql/sql_string.h b/sql/sql_string.h index dc9122a4fcb..b154f0c1667 100644 --- a/sql/sql_string.h +++ b/sql/sql_string.h @@ -274,6 +274,14 @@ public: bool set_or_copy_aligned(const char *s, uint32 arg_length, CHARSET_INFO *cs); bool copy(const char*s,uint32 arg_length, CHARSET_INFO *csfrom, CHARSET_INFO *csto, uint *errors); + void move(String &s) + { + free(); + Ptr=s.Ptr ; str_length=s.str_length ; Alloced_length=s.Alloced_length; + extra_alloc= s.extra_alloc; + alloced= s.alloced; + s.alloced= 0; + } bool append(const String &s); bool append(const char *s); bool append(const char *s,uint32 arg_length); @@ -304,6 +312,7 @@ public: friend int sortcmp(const String *a,const String *b, CHARSET_INFO *cs); friend int stringcmp(const String *a,const String *b); friend String *copy_if_not_alloced(String *a,String *b,uint32 arg_length); + friend class Field; uint32 numchars(); int charpos(int i,uint32 offset=0); -- cgit v1.2.1