summaryrefslogtreecommitdiff
path: root/sql/sql_plugin.cc
diff options
context:
space:
mode:
authorMonty <monty@mariadb.org>2020-09-16 11:23:50 +0300
committerSergei Golubchik <serg@mariadb.org>2021-05-19 22:27:27 +0200
commit36cdd5c3cdb06d8538f64c0b312ffe4672a92e75 (patch)
treef1c675fab2e79fc8cd7466b080ddbc5ce3a1b920 /sql/sql_plugin.cc
parentda85ad798708d045e7ba1963172daf81aeb80ab9 (diff)
downloadmariadb-git-36cdd5c3cdb06d8538f64c0b312ffe4672a92e75.tar.gz
Optimize usage of c_ptr(), c_ptr_quick() and String::alloc()
The problem was that when one used String::alloc() to allocate a string, the String ensures that there is space for an extra NULL byte in the buffer and if not, reallocates the string. This is a problem with the String::set_int() that calls alloc(21), which forces extra malloc/free calls to happen. - We do not anymore re-allocate String if alloc() is called with the Allocated_length. This reduces number of malloc() allocations, especially one big re-allocation in Protocol::send_result_Set_metadata() for almost every query that produced a result to the connnected client. - Avoid extra mallocs when using LONGLONG_BUFFER_SIZE This can now be done as alloc() doesn't increase buffers if new length is not bigger than old one. - c_ptr() is redesigned to be safer (but a bit longer) than before. - Remove wrong usage of c_ptr_quick() c_ptr_quick() was used in many cases to get the pointer to the used buffer, even when it didn't need to be \0 terminated. In this case ptr() is a better substitute. Another problem with c_ptr_quick() is that it did not guarantee that the string would be \0 terminated. - item_val_str(), an API function not used currently by the server, now always returns a null terminated string (before it didn't always do that). - Ensure that all String allocations uses STRING_PSI_MEMORY_KEY. The old mixed usage of performance keys caused assert's when String buffers where shrunk. - Binary_string::shrink() is simplifed - Fixed bug in String(const char *str, size_t len, CHARSET_INFO *cs) that used Binary_string((char *) str, len) instead of Binary_string(str,len). - Changed argument to String() creations and String.set() functions to use 'const char*' instead of 'char*'. This ensures that Alloced_length is not set, which gives safety against someone trying to change the original string. This also would allow us to use !Alloced_length in c_ptr() if needed. - Changed string_ptr_cmp() to use memcmp() instead of c_ptr() to avoid a possible malloc during string comparision.
Diffstat (limited to 'sql/sql_plugin.cc')
-rw-r--r--sql/sql_plugin.cc8
1 files changed, 6 insertions, 2 deletions
diff --git a/sql/sql_plugin.cc b/sql/sql_plugin.cc
index 02ae08c910c..195267b9abc 100644
--- a/sql/sql_plugin.cc
+++ b/sql/sql_plugin.cc
@@ -404,12 +404,16 @@ static int item_value_type(struct st_mysql_value *value)
static const char *item_val_str(struct st_mysql_value *value,
char *buffer, int *length)
{
- String str(buffer, *length, system_charset_info), *res;
+ size_t org_length= *length;
+ String str(buffer, org_length, system_charset_info), *res;
if (!(res= ((st_item_value_holder*)value)->item->val_str(&str)))
return NULL;
*length= res->length();
- if (res->c_ptr_quick() == buffer)
+ if (res->ptr() == buffer && res->length() < org_length)
+ {
+ buffer[res->length()]= 0;
return buffer;
+ }
/*
Lets be nice and create a temporary string since the