Skip to content

Commit be98036

Browse files
committed
Preparatory changes for MDEV-23162 Improve Protocol performance for numeric data
- Renaming this virtual method store() to store_str(): store(const char *str, size_t length, CHARSET_INFO *src_cs, CHARSET_INFO *dst_cs) We'll be adding more variants of store*() soon. This change will help to avoid ambiguities during overloading. - Adding a helper method store_ident(). - Renaming store_str(const LEX_CSTRING &s...) to store_lex_cstring(), to avoid ambiguties during overloading. - Adding a helper method store() for backward compatibility, to avoid a lot of changes in the code now. But eventually we should replace store() to non-ambiguius methods store_str() or store_ident(). - Adding a helper method Protocol::needs_conversion() and reusing it in two places.
1 parent c3afcc7 commit be98036

File tree

3 files changed

+66
-58
lines changed

3 files changed

+66
-58
lines changed

sql/protocol.cc

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ bool net_send_eof(THD *thd, uint server_status, uint statement_warn_count);
4343
static bool write_eof_packet(THD *, NET *, uint, uint);
4444
#endif
4545

46+
CHARSET_INFO *Protocol::character_set_results() const
47+
{
48+
return thd->variables.character_set_results;
49+
}
50+
51+
4652
#ifndef EMBEDDED_LIBRARY
4753
bool Protocol::net_store_data(const uchar *from, size_t length)
4854
#else
@@ -846,12 +852,13 @@ bool Protocol_text::store_field_metadata(const THD * thd,
846852

847853
if (thd->client_capabilities & CLIENT_PROTOCOL_41)
848854
{
849-
if (store(STRING_WITH_LEN("def"), cs, thd_charset) ||
850-
store_str(field.db_name, cs, thd_charset) ||
851-
store_str(field.table_name, cs, thd_charset) ||
852-
store_str(field.org_table_name, cs, thd_charset) ||
853-
store_str(field.col_name, cs, thd_charset) ||
854-
store_str(field.org_col_name, cs, thd_charset))
855+
const LEX_CSTRING def= {STRING_WITH_LEN("def")};
856+
if (store_ident(def) ||
857+
store_ident(field.db_name) ||
858+
store_ident(field.table_name) ||
859+
store_ident(field.org_table_name) ||
860+
store_ident(field.col_name) ||
861+
store_ident(field.org_col_name))
855862
return true;
856863
if (thd->client_capabilities & MARIADB_CLIENT_EXTENDED_METADATA)
857864
{
@@ -861,7 +868,7 @@ bool Protocol_text::store_field_metadata(const THD * thd,
861868
Don't apply character set conversion:
862869
extended metadata is a binary encoded data.
863870
*/
864-
if (store_str(metadata.lex_cstring(), cs, &my_charset_bin))
871+
if (store_lex_cstring(metadata.lex_cstring(), cs, &my_charset_bin))
865872
return true;
866873
}
867874
if (packet->realloc(packet->length() + 12))
@@ -894,8 +901,8 @@ bool Protocol_text::store_field_metadata(const THD * thd,
894901
}
895902
else
896903
{
897-
if (store_str(field.table_name, cs, thd_charset) ||
898-
store_str(field.col_name, cs, thd_charset) ||
904+
if (store_ident(field.table_name) ||
905+
store_ident(field.col_name) ||
899906
packet->realloc(packet->length() + 10))
900907
return true;
901908
pos= (char*) packet->end();
@@ -1178,9 +1185,7 @@ bool Protocol::store_string_aux(const char *from, size_t length,
11781185
CHARSET_INFO *fromcs, CHARSET_INFO *tocs)
11791186
{
11801187
/* 'tocs' is set 0 when client issues SET character_set_results=NULL */
1181-
if (tocs && !my_charset_same(fromcs, tocs) &&
1182-
fromcs != &my_charset_bin &&
1183-
tocs != &my_charset_bin)
1188+
if (needs_conversion(fromcs, tocs))
11841189
{
11851190
/* Store with conversion */
11861191
return net_store_data_cs((uchar*) from, length, fromcs, tocs);
@@ -1202,8 +1207,8 @@ bool Protocol::store_warning(const char *from, size_t length)
12021207
}
12031208

12041209

1205-
bool Protocol_text::store(const char *from, size_t length,
1206-
CHARSET_INFO *fromcs, CHARSET_INFO *tocs)
1210+
bool Protocol_text::store_str(const char *from, size_t length,
1211+
CHARSET_INFO *fromcs, CHARSET_INFO *tocs)
12071212
{
12081213
#ifndef DBUG_OFF
12091214
DBUG_PRINT("info", ("Protocol_text::store field %u (%u): %.*b", field_pos,
@@ -1216,14 +1221,6 @@ bool Protocol_text::store(const char *from, size_t length,
12161221
}
12171222

12181223

1219-
bool Protocol::store(const char *from, size_t length,
1220-
CHARSET_INFO *fromcs)
1221-
{
1222-
CHARSET_INFO *tocs= this->thd->variables.character_set_results;
1223-
return store(from, length, fromcs, tocs);
1224-
}
1225-
1226-
12271224
bool Protocol_text::store_tiny(longlong from)
12281225
{
12291226
#ifndef DBUG_OFF
@@ -1451,8 +1448,8 @@ void Protocol_binary::prepare_for_resend()
14511448
}
14521449

14531450

1454-
bool Protocol_binary::store(const char *from, size_t length,
1455-
CHARSET_INFO *fromcs, CHARSET_INFO *tocs)
1451+
bool Protocol_binary::store_str(const char *from, size_t length,
1452+
CHARSET_INFO *fromcs, CHARSET_INFO *tocs)
14561453
{
14571454
field_pos++;
14581455
return store_string_aux(from, length, fromcs, tocs);
@@ -1517,8 +1514,8 @@ bool Protocol_binary::store_decimal(const my_decimal *d)
15171514
#endif
15181515
StringBuffer<DECIMAL_MAX_STR_LENGTH> str;
15191516
(void) d->to_string(&str);
1520-
return store(str.ptr(), str.length(), str.charset(),
1521-
thd->variables.character_set_results);
1517+
return store_str(str.ptr(), str.length(), str.charset(),
1518+
thd->variables.character_set_results);
15221519
}
15231520

15241521
bool Protocol_binary::store(float from, uint32 decimals, String *buffer)

sql/protocol.h

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ class Protocol
6161
MYSQL_FIELD *next_mysql_field;
6262
MEM_ROOT *alloc;
6363
#endif
64+
bool needs_conversion(CHARSET_INFO *fromcs,
65+
CHARSET_INFO *tocs) const
66+
{
67+
// 'tocs' is set 0 when client issues SET character_set_results=NULL
68+
return tocs && !my_charset_same(fromcs, tocs) &&
69+
fromcs != &my_charset_bin &&
70+
tocs != &my_charset_bin;
71+
}
6472
/*
6573
The following two are low-level functions that are invoked from
6674
higher-level store_xxx() funcs. The data is stored into this->packet.
@@ -77,6 +85,8 @@ class Protocol
7785
virtual bool send_error(uint sql_errno, const char *err_msg,
7886
const char *sql_state);
7987

88+
CHARSET_INFO *character_set_results() const;
89+
8090
public:
8191
THD *thd;
8292
Protocol(THD *thd_arg) { init(thd_arg); }
@@ -120,20 +130,33 @@ class Protocol
120130
virtual bool store_long(longlong from)=0;
121131
virtual bool store_longlong(longlong from, bool unsigned_flag)=0;
122132
virtual bool store_decimal(const my_decimal *)=0;
123-
bool store(const char *from, size_t length, CHARSET_INFO *cs);
124-
virtual bool store(const char *from, size_t length,
125-
CHARSET_INFO *fromcs, CHARSET_INFO *tocs)=0;
126-
bool store_str(const LEX_CSTRING &s, CHARSET_INFO *fromcs, CHARSET_INFO *tocs)
127-
{
128-
return store(s.str, (uint) s.length, fromcs, tocs);
129-
}
133+
virtual bool store_str(const char *from, size_t length,
134+
CHARSET_INFO *fromcs, CHARSET_INFO *tocs)=0;
130135
virtual bool store(float from, uint32 decimals, String *buffer)=0;
131136
virtual bool store(double from, uint32 decimals, String *buffer)=0;
132137
virtual bool store(MYSQL_TIME *time, int decimals)=0;
133138
virtual bool store_date(MYSQL_TIME *time)=0;
134139
virtual bool store_time(MYSQL_TIME *time, int decimals)=0;
135140
virtual bool store(Field *field)=0;
136141

142+
// Various useful wrappers for the virtual store*() methods.
143+
// Backward wrapper for store_str()
144+
bool store(const char *from, size_t length, CHARSET_INFO *cs)
145+
{
146+
return store_str(from, length, cs, character_set_results());
147+
}
148+
bool store_lex_cstring(const LEX_CSTRING &s,
149+
CHARSET_INFO *fromcs,
150+
CHARSET_INFO *tocs)
151+
{
152+
return store_str(s.str, (uint) s.length, fromcs, tocs);
153+
}
154+
bool store_ident(const LEX_CSTRING &s)
155+
{
156+
return store_lex_cstring(s, system_charset_info, character_set_results());
157+
}
158+
// End of wrappers
159+
137160
virtual bool send_out_parameters(List<Item_param> *sp_params)=0;
138161
#ifdef EMBEDDED_LIBRARY
139162
bool begin_dataset();
@@ -178,8 +201,8 @@ class Protocol_text :public Protocol
178201
virtual bool store_long(longlong from);
179202
virtual bool store_longlong(longlong from, bool unsigned_flag);
180203
virtual bool store_decimal(const my_decimal *);
181-
virtual bool store(const char *from, size_t length,
182-
CHARSET_INFO *fromcs, CHARSET_INFO *tocs);
204+
virtual bool store_str(const char *from, size_t length,
205+
CHARSET_INFO *fromcs, CHARSET_INFO *tocs);
183206
virtual bool store(MYSQL_TIME *time, int decimals);
184207
virtual bool store_date(MYSQL_TIME *time);
185208
virtual bool store_time(MYSQL_TIME *time, int decimals);
@@ -222,8 +245,8 @@ class Protocol_binary :public Protocol
222245
virtual bool store_long(longlong from);
223246
virtual bool store_longlong(longlong from, bool unsigned_flag);
224247
virtual bool store_decimal(const my_decimal *);
225-
virtual bool store(const char *from, size_t length,
226-
CHARSET_INFO *fromcs, CHARSET_INFO *tocs);
248+
virtual bool store_str(const char *from, size_t length,
249+
CHARSET_INFO *fromcs, CHARSET_INFO *tocs);
227250
virtual bool store(MYSQL_TIME *time, int decimals);
228251
virtual bool store_date(MYSQL_TIME *time);
229252
virtual bool store_time(MYSQL_TIME *time, int decimals);
@@ -270,7 +293,10 @@ class Protocol_discard : public Protocol_text
270293
bool store_long(longlong) { return false; }
271294
bool store_longlong(longlong, bool) { return false; }
272295
bool store_decimal(const my_decimal *) { return false; }
273-
bool store(const char *, size_t, CHARSET_INFO *, CHARSET_INFO *) { return false; }
296+
bool store_str(const char *, size_t, CHARSET_INFO *, CHARSET_INFO *)
297+
{
298+
return false;
299+
}
274300
bool store(MYSQL_TIME *, int) { return false; }
275301
bool store_date(MYSQL_TIME *) { return false; }
276302
bool store_time(MYSQL_TIME *, int) { return false; }

sql/sql_prepare.cc

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,8 @@ class Protocol_local :public Protocol
262262
virtual bool store_long(longlong from);
263263
virtual bool store_longlong(longlong from, bool unsigned_flag);
264264
virtual bool store_decimal(const my_decimal *);
265-
virtual bool store(const char *from, size_t length, CHARSET_INFO *cs);
266-
virtual bool store(const char *from, size_t length,
267-
CHARSET_INFO *fromcs, CHARSET_INFO *tocs);
265+
virtual bool store_str(const char *from, size_t length,
266+
CHARSET_INFO *fromcs, CHARSET_INFO *tocs);
268267
virtual bool store(MYSQL_TIME *time, int decimals);
269268
virtual bool store_date(MYSQL_TIME *time);
270269
virtual bool store_time(MYSQL_TIME *time, int decimals);
@@ -5275,9 +5274,7 @@ Protocol_local::store_string(const char *str, size_t length,
52755274
/* Store with conversion */
52765275
uint error_unused;
52775276

5278-
if (dst_cs && !my_charset_same(src_cs, dst_cs) &&
5279-
src_cs != &my_charset_bin &&
5280-
dst_cs != &my_charset_bin)
5277+
if (needs_conversion(src_cs, dst_cs))
52815278
{
52825279
if (unlikely(convert->copy(str, length, src_cs, dst_cs, &error_unused)))
52835280
return TRUE;
@@ -5334,22 +5331,10 @@ bool Protocol_local::store_decimal(const my_decimal *value)
53345331
}
53355332

53365333

5337-
/** Convert to cs_results and store a string. */
5338-
5339-
bool Protocol_local::store(const char *str, size_t length,
5340-
CHARSET_INFO *src_cs)
5341-
{
5342-
CHARSET_INFO *dst_cs;
5343-
5344-
dst_cs= m_connection->m_thd->variables.character_set_results;
5345-
return store_string(str, length, src_cs, dst_cs);
5346-
}
5347-
5348-
53495334
/** Store a string. */
53505335

5351-
bool Protocol_local::store(const char *str, size_t length,
5352-
CHARSET_INFO *src_cs, CHARSET_INFO *dst_cs)
5336+
bool Protocol_local::store_str(const char *str, size_t length,
5337+
CHARSET_INFO *src_cs, CHARSET_INFO *dst_cs)
53535338
{
53545339
return store_string(str, length, src_cs, dst_cs);
53555340
}

0 commit comments

Comments
 (0)