1
0
mirror of https://github.com/MariaDB/server.git synced 2025-07-30 16:24:05 +03:00

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.
This commit is contained in:
Monty
2020-09-16 11:23:50 +03:00
committed by Sergei Golubchik
parent da85ad7987
commit 36cdd5c3cd
15 changed files with 123 additions and 71 deletions

View File

@ -2153,7 +2153,7 @@ static int init_binlog_sender(binlog_send_info *info,
"Start binlog_dump to slave_server(%lu), pos(%s, %lu), "
"using_gtid(%d), gtid('%s')", thd->variables.server_id,
log_ident, (ulong)*pos, info->using_gtid_state,
connect_gtid_state.c_ptr_quick());
connect_gtid_state.c_ptr_safe());
}
#ifndef DBUG_OFF
@ -2176,7 +2176,7 @@ static int init_binlog_sender(binlog_send_info *info,
const char *name=search_file_name;
if (info->using_gtid_state)
{
if (info->gtid_state.load(connect_gtid_state.c_ptr_quick(),
if (info->gtid_state.load(connect_gtid_state.ptr(),
connect_gtid_state.length()))
{
info->errmsg= "Out of memory or malformed slave request when obtaining "
@ -2185,7 +2185,7 @@ static int init_binlog_sender(binlog_send_info *info,
return 1;
}
if (info->until_gtid_state &&
info->until_gtid_state->load(slave_until_gtid_str.c_ptr_quick(),
info->until_gtid_state->load(slave_until_gtid_str.ptr(),
slave_until_gtid_str.length()))
{
info->errmsg= "Out of memory or malformed slave request when "