1
0
mirror of https://github.com/MariaDB/server.git synced 2025-07-29 05:21:33 +03:00

Reduce usage of strlen()

Changes:
- To detect automatic strlen() I removed the methods in String that
  uses 'const char *' without a length:
  - String::append(const char*)
  - Binary_string(const char *str)
  - String(const char *str, CHARSET_INFO *cs)
  - append_for_single_quote(const char *)
  All usage of append(const char*) is changed to either use
  String::append(char), String::append(const char*, size_t length) or
  String::append(LEX_CSTRING)
- Added STRING_WITH_LEN() around constant string arguments to
  String::append()
- Added overflow argument to escape_string_for_mysql() and
  escape_quotes_for_mysql() instead of returning (size_t) -1 on overflow.
  This was needed as most usage of the above functions never tested the
  result for -1 and would have given wrong results or crashes in case
  of overflows.
- Added Item_func_or_sum::func_name_cstring(), which returns LEX_CSTRING.
  Changed all Item_func::func_name()'s to func_name_cstring()'s.
  The old Item_func_or_sum::func_name() is now an inline function that
  returns func_name_cstring().str.
- Changed Item::mode_name() and Item::func_name_ext() to return
  LEX_CSTRING.
- Changed for some functions the name argument from const char * to
  to const LEX_CSTRING &:
  - Item::Item_func_fix_attributes()
  - Item::check_type_...()
  - Type_std_attributes::agg_item_collations()
  - Type_std_attributes::agg_item_set_converter()
  - Type_std_attributes::agg_arg_charsets...()
  - Type_handler_hybrid_field_type::aggregate_for_result()
  - Type_handler_geometry::check_type_geom_or_binary()
  - Type_handler::Item_func_or_sum_illegal_param()
  - Predicant_to_list_comparator::add_value_skip_null()
  - Predicant_to_list_comparator::add_value()
  - cmp_item_row::prepare_comparators()
  - cmp_item_row::aggregate_row_elements_for_comparison()
  - Cursor_ref::print_func()
- Removes String_space() as it was only used in one cases and that
  could be simplified to not use String_space(), thanks to the fixed
  my_vsnprintf().
- Added some const LEX_CSTRING's for common strings:
  - NULL_clex_str, DATA_clex_str, INDEX_clex_str.
- Changed primary_key_name to a LEX_CSTRING
- Renamed String::set_quick() to String::set_buffer_if_not_allocated() to
  clarify what the function really does.
- Rename of protocol function:
  bool store(const char *from, CHARSET_INFO *cs) to
  bool store_string_or_null(const char *from, CHARSET_INFO *cs).
  This was done to both clarify the difference between this 'store' function
  and also to make it easier to find unoptimal usage of store() calls.
- Added Protocol::store(const LEX_CSTRING*, CHARSET_INFO*)
- Changed some 'const char*' arrays to instead be of type LEX_CSTRING.
- class Item_func_units now used LEX_CSTRING for name.

Other things:
- Fixed a bug in mysql.cc:construct_prompt() where a wrong escape character
  in the prompt would cause some part of the prompt to be duplicated.
- Fixed a lot of instances where the length of the argument to
  append is known or easily obtain but was not used.
- Removed some not needed 'virtual' definition for functions that was
  inherited from the parent. I added override to these.
- Fixed Ordered_key::print() to preallocate needed buffer. Old code could
  case memory overruns.
- Simplified some loops when adding char * to a String with delimiters.
This commit is contained in:
Monty
2020-08-12 20:29:55 +03:00
committed by Sergei Golubchik
parent b3bc02f923
commit b6ff139aa3
113 changed files with 3543 additions and 1764 deletions

View File

@ -296,7 +296,7 @@ static void push_string_list(THD *thd, List<Item> *item_list,
else
buf->append(',');
buf->append(line);
buf->append(line, strlen(line));
}
push_string(thd, item_list, buf);
}
@ -406,7 +406,7 @@ int print_explain_row(select_result_sink *result,
Item_float *fl= new (mem_root) Item_float(thd, *r_rows, 2);
String tmp;
String *res= fl->val_str(&tmp);
r_rows_str.append(res->ptr());
r_rows_str.append(*res);
item_list.push_back(new (mem_root)
Item_string_sys(thd, r_rows_str.ptr(),
r_rows_str.length()), mem_root);
@ -552,7 +552,7 @@ int Explain_union::print_explain(Explain_query *query,
Item_float *fl= new (mem_root) Item_float(thd, avg_rows, 2);
String tmp;
String *res= fl->val_str(&tmp);
r_rows_str.append(res->ptr());
r_rows_str.append(*res);
item_list.push_back(new (mem_root)
Item_string_sys(thd, r_rows_str.ptr(),
r_rows_str.length()), mem_root);
@ -1043,11 +1043,11 @@ void Explain_aggr_filesort::print_json_members(Json_writer *writer,
first= false;
else
{
str.append(", ");
str.append(STRING_WITH_LEN(", "));
}
append_item_to_str(&str, item);
if (*direction == ORDER::ORDER_DESC)
str.append(" desc");
str.append(STRING_WITH_LEN(" desc"));
}
writer->add_member("sort_key").add_str(str.c_ptr_safe());
@ -1125,14 +1125,15 @@ void Explain_table_access::fill_key_str(String *key_str, bool is_json) const
CHARSET_INFO *cs= system_charset_info;
bool is_hj= (type == JT_HASH || type == JT_HASH_NEXT ||
type == JT_HASH_RANGE || type == JT_HASH_INDEX_MERGE);
const char *hash_key_prefix= "#hash#";
LEX_CSTRING hash_key_prefix= { STRING_WITH_LEN("#hash#") };
const char *key_name;
if (key.get_key_name())
if ((key_name= key.get_key_name()))
{
if (is_hj)
key_str->append(hash_key_prefix, strlen(hash_key_prefix), cs);
key_str->append(hash_key_prefix.str, hash_key_prefix.length, cs);
key_str->append(key.get_key_name());
key_str->append(key_name, strlen(key_name));
if (is_hj && type != JT_HASH)
key_str->append(':');
@ -1148,7 +1149,10 @@ void Explain_table_access::fill_key_str(String *key_str, bool is_json) const
key_str->append(buf2);
}
if (type == JT_HASH_NEXT)
key_str->append(hash_next_key.get_key_name());
{
key_name= hash_next_key.get_key_name();
key_str->append(key_name, strlen(key_name));
}
}
@ -1290,8 +1294,8 @@ int Explain_table_access::print_explain(select_result_sink *output, uint8 explai
push_str(thd, &item_list, join_type_str[type]);
else
{
join_type_buf.append(join_type_str[type]);
join_type_buf.append("|filter");
join_type_buf.append(join_type_str[type], strlen(join_type_str[type]));
join_type_buf.append(STRING_WITH_LEN("|filter"));
item_list.push_back(new (mem_root)
Item_string_sys(thd, join_type_buf.ptr(),
join_type_buf.length()),
@ -1311,7 +1315,7 @@ int Explain_table_access::print_explain(select_result_sink *output, uint8 explai
if (rowid_filter)
{
key_str.append("|");
key_str.append('|');
StringBuffer<64> rowid_key_str;
rowid_filter->quick->print_key(&rowid_key_str);
key_str.append(rowid_key_str);
@ -1354,10 +1358,10 @@ int Explain_table_access::print_explain(select_result_sink *output, uint8 explai
if (rowid_filter)
{
rows_str.append(" (");
rows_str.append(STRING_WITH_LEN(" ("));
rows_str.append_ulonglong((ulonglong) (round(rowid_filter->selectivity *
100.0)));
rows_str.append("%)");
rows_str.append(STRING_WITH_LEN("%)"));
}
item_list.push_back(new (mem_root)
Item_string_sys(thd, rows_str.ptr(),
@ -1380,13 +1384,13 @@ int Explain_table_access::print_explain(select_result_sink *output, uint8 explai
Item_float *fl= new (mem_root) Item_float(thd, avg_rows, 2);
String tmp;
String *res= fl->val_str(&tmp);
r_rows_str.append(res->ptr());
r_rows_str.append(*res);
if (rowid_filter)
{
r_rows_str.append(" (");
r_rows_str.append(STRING_WITH_LEN(" ("));
r_rows_str.append_ulonglong(
(ulonglong) (rowid_filter->tracker->get_r_selectivity_pct() * 100.0));
r_rows_str.append("%)");
r_rows_str.append(STRING_WITH_LEN("%)"));
}
item_list.push_back(new (mem_root)
Item_string_sys(thd, r_rows_str.ptr(),
@ -1929,42 +1933,41 @@ void Explain_table_access::print_explain_json(Explain_query *query,
sql_explain.h
*/
const char * extra_tag_text[]=
const LEX_CSTRING extra_tag_text[]=
{
"ET_none",
"Using index condition",
"Using index condition(BKA)",
"Using ", // special handling
"Range checked for each record (index map: 0x", // special handling
"Using where with pushed condition",
"Using where",
"Not exists",
{ STRING_WITH_LEN("ET_none") },
{ STRING_WITH_LEN("Using index condition") },
{ STRING_WITH_LEN("Using index condition(BKA)") },
{ STRING_WITH_LEN("Using ") }, // special handling
{ STRING_WITH_LEN("Range checked for each record (index map: 0x") }, // special handling
{ STRING_WITH_LEN("Using where with pushed condition") },
{ STRING_WITH_LEN("Using where") },
{ STRING_WITH_LEN("Not exists") },
"Using index",
"Full scan on NULL key",
"Skip_open_table",
"Open_frm_only",
"Open_full_table",
{ STRING_WITH_LEN("Using index") },
{ STRING_WITH_LEN("Full scan on NULL key") },
{ STRING_WITH_LEN("Skip_open_table") },
{ STRING_WITH_LEN("Open_frm_only") },
{ STRING_WITH_LEN("Open_full_table") },
"Scanned 0 databases",
"Scanned 1 database",
"Scanned all databases",
{ STRING_WITH_LEN("Scanned 0 databases") },
{ STRING_WITH_LEN("Scanned 1 database") },
{ STRING_WITH_LEN("Scanned all databases") },
"Using index for group-by", // special handling
{ STRING_WITH_LEN("Using index for group-by") }, // special handling
{ STRING_WITH_LEN("USING MRR: DONT PRINT ME") }, // special handling
"USING MRR: DONT PRINT ME", // special handling
{ STRING_WITH_LEN("Distinct") },
{ STRING_WITH_LEN("LooseScan") },
{ STRING_WITH_LEN("Start temporary") },
{ STRING_WITH_LEN("End temporary") },
{ STRING_WITH_LEN("FirstMatch") }, // special handling
"Distinct",
"LooseScan",
"Start temporary",
"End temporary",
"FirstMatch", // special handling
{ STRING_WITH_LEN("Using join buffer") }, // special handling
"Using join buffer", // special handling
"Const row not found",
"Unique row not found",
"Impossible ON condition",
{ STRING_WITH_LEN("Const row not found") },
{ STRING_WITH_LEN("Unique row not found") },
{ STRING_WITH_LEN("Impossible ON condition") }
};
@ -1984,7 +1987,8 @@ void Explain_table_access::append_tag_name(String *str, enum explain_extra_tag t
char buf[MAX_KEY / 4 + 1];
str->append(STRING_WITH_LEN("Range checked for each "
"record (index map: 0x"));
str->append(range_checked_fer->keys_map.print(buf));
range_checked_fer->keys_map.print(buf);
str->append(buf, strlen(buf));
str->append(')');
break;
}
@ -1998,12 +2002,16 @@ void Explain_table_access::append_tag_name(String *str, enum explain_extra_tag t
str->append(extra_tag_text[tag]);
str->append(STRING_WITH_LEN(" ("));
const char *buffer_type= bka_type.incremental ? "incremental" : "flat";
LEX_CSTRING buffer_type;
if (bka_type.incremental)
buffer_type= { STRING_WITH_LEN("incremental") };
else
buffer_type= { STRING_WITH_LEN("flat") };
str->append(buffer_type);
str->append(STRING_WITH_LEN(", "));
str->append(bka_type.join_alg);
str->append(bka_type.join_alg, strlen(bka_type.join_alg));
str->append(STRING_WITH_LEN(" join"));
str->append(STRING_WITH_LEN(")"));
str->append(')');
if (bka_type.mrr_type.length())
{
str->append(STRING_WITH_LEN("; "));
@ -2016,9 +2024,9 @@ void Explain_table_access::append_tag_name(String *str, enum explain_extra_tag t
{
if (firstmatch_table_name.length())
{
str->append("FirstMatch(");
str->append(STRING_WITH_LEN("FirstMatch("));
str->append(firstmatch_table_name);
str->append(")");
str->append(')');
}
else
str->append(extra_tag_text[tag]);
@ -2028,10 +2036,10 @@ void Explain_table_access::append_tag_name(String *str, enum explain_extra_tag t
{
str->append(extra_tag_text[tag]);
if (loose_scan_is_scanning)
str->append(" (scanning)");
str->append(STRING_WITH_LEN(" (scanning)"));
break;
case ET_TABLE_FUNCTION:
str->append("Table function: json_table");
str->append(STRING_WITH_LEN("Table function: json_table"));
break;
}
default:
@ -2086,13 +2094,16 @@ void Explain_quick_select::print_json(Json_writer *writer)
void Explain_quick_select::print_extra_recursive(String *str)
{
const char *name;
if (is_basic())
{
str->append(range.get_key_name());
name= range.get_key_name();
str->append(name, strlen(name));
}
else
{
str->append(get_name_by_type());
name= get_name_by_type();
str->append(name, strlen(name));
str->append('(');
List_iterator_fast<Explain_quick_select> it (children);
Explain_quick_select* child;
@ -2141,7 +2152,7 @@ void Explain_quick_select::print_key(String *str)
{
if (str->length() > 0)
str->append(',');
str->append(range.get_key_name());
str->append(range.get_key_name(), strlen(range.get_key_name()));
}
else
{