summaryrefslogtreecommitdiff
path: root/sql/sql_string.h
diff options
context:
space:
mode:
authorMichael Widenius <monty@askmonty.org>2010-11-08 13:43:54 +0200
committerMichael Widenius <monty@askmonty.org>2010-11-08 13:43:54 +0200
commit7b047a31a09a7999353232cb6407b4e6338d5b70 (patch)
tree8b23cc836adb9afa6adfa701ac0593b1673625a8 /sql/sql_string.h
parent18292caa48f5cca9b651f62c1dbbffefadb33d9f (diff)
downloadmariadb-git-7b047a31a09a7999353232cb6407b4e6338d5b70.tar.gz
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.
Diffstat (limited to 'sql/sql_string.h')
-rw-r--r--sql/sql_string.h48
1 files changed, 30 insertions, 18 deletions
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;
}