Skip to content

Commit 8f594b3

Browse files
committed
Session_sysvars_tracker::vars_list cleanups
- return proper type - removed useless node argument - removed useless mem_flag - classes are "private" by default - simplified iterators Part of MDEV-14984 - regression in connect performance
1 parent 0e91e0c commit 8f594b3

File tree

1 file changed

+50
-92
lines changed

1 file changed

+50
-92
lines changed

sql/session_tracker.cc

Lines changed: 50 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ class Session_sysvars_tracker : public State_tracker
7070

7171
class vars_list
7272
{
73-
private:
7473
/**
7574
Registered system variables. (@@session_track_system_variables)
7675
A hash to store the name of all the system variables specified by the
@@ -101,12 +100,20 @@ class Session_sysvars_tracker : public State_tracker
101100
}
102101
}
103102

104-
uchar* search(const sys_var *svar)
103+
sysvar_node_st *search(const sys_var *svar)
105104
{
106-
return (my_hash_search(&m_registered_sysvars, (const uchar *)&svar,
107-
sizeof(sys_var *)));
105+
return reinterpret_cast<sysvar_node_st*>(
106+
my_hash_search(&m_registered_sysvars,
107+
reinterpret_cast<const uchar*>(&svar),
108+
sizeof(sys_var*)));
108109
}
109110

111+
sysvar_node_st *at(ulong i)
112+
{
113+
DBUG_ASSERT(i < m_registered_sysvars.records);
114+
return reinterpret_cast<sysvar_node_st*>(
115+
my_hash_element(&m_registered_sysvars, i));
116+
}
110117
public:
111118
vars_list() :
112119
buffer_length(0)
@@ -129,22 +136,21 @@ class Session_sysvars_tracker : public State_tracker
129136
}
130137
}
131138

132-
uchar* insert_or_search(sysvar_node_st *node, const sys_var *svar)
139+
sysvar_node_st *insert_or_search(const sys_var *svar)
133140
{
134-
uchar *res;
135-
res= search(svar);
141+
sysvar_node_st *res= search(svar);
136142
if (!res)
137143
{
138144
if (track_all)
139145
{
140-
insert(node, svar, m_mem_flag);
146+
insert(svar);
141147
return search(svar);
142148
}
143149
}
144150
return res;
145151
}
146152

147-
bool insert(sysvar_node_st *node, const sys_var *svar, myf mem_flag);
153+
bool insert(const sys_var *svar);
148154
void reinit();
149155
void reset();
150156
inline bool is_enabled()
@@ -218,11 +224,6 @@ class Session_sysvars_tracker : public State_tracker
218224
static uchar *sysvars_get_key(const char *entry, size_t *length,
219225
my_bool not_used __attribute__((unused)));
220226

221-
// hash iterators
222-
static my_bool name_array_filler(void *ptr, void *data_ptr);
223-
static my_bool store_variable(void *ptr, void *data_ptr);
224-
static my_bool reset_variable(void *ptr, void *data_ptr);
225-
226227
static bool check_var_list(THD *thd, LEX_STRING var_list, bool throw_error,
227228
CHARSET_INFO *char_set, bool take_mutex);
228229
};
@@ -313,25 +314,20 @@ void Session_sysvars_tracker::vars_list::copy(vars_list* from, THD *thd)
313314
/**
314315
Inserts the variable to be tracked into m_registered_sysvars hash.
315316
316-
@param node Node to be inserted.
317317
@param svar address of the system variable
318318
319319
@retval false success
320320
@retval true error
321321
*/
322322

323-
bool Session_sysvars_tracker::vars_list::insert(sysvar_node_st *node,
324-
const sys_var *svar,
325-
myf mem_flag)
323+
bool Session_sysvars_tracker::vars_list::insert(const sys_var *svar)
326324
{
327-
if (!node)
325+
sysvar_node_st *node;
326+
if (!(node= (sysvar_node_st *) my_malloc(sizeof(sysvar_node_st),
327+
MYF(MY_WME | m_mem_flag))))
328328
{
329-
if (!(node= (sysvar_node_st *) my_malloc(sizeof(sysvar_node_st),
330-
MYF(MY_WME | mem_flag))))
331-
{
332-
reinit();
333-
return true;
334-
}
329+
reinit();
330+
return true;
335331
}
336332

337333
node->m_svar= (sys_var *)svar;
@@ -433,7 +429,7 @@ bool Session_sysvars_tracker::vars_list::parse_var_list(THD *thd,
433429
else if ((svar=
434430
find_sys_var_ex(thd, var.str, var.length, throw_error, true)))
435431
{
436-
if (insert(NULL, svar, m_mem_flag) == TRUE)
432+
if (insert(svar) == TRUE)
437433
goto error;
438434
}
439435
else if (throw_error && thd)
@@ -536,24 +532,6 @@ bool Session_sysvars_tracker::check_var_list(THD *thd,
536532
return false;
537533
}
538534

539-
struct name_array_filler_data
540-
{
541-
LEX_CSTRING **names;
542-
uint idx;
543-
544-
};
545-
546-
/** Collects variable references into array */
547-
my_bool Session_sysvars_tracker::name_array_filler(void *ptr,
548-
void *data_ptr)
549-
{
550-
Session_sysvars_tracker::sysvar_node_st *node=
551-
(Session_sysvars_tracker::sysvar_node_st *)ptr;
552-
name_array_filler_data *data= (struct name_array_filler_data *)data_ptr;
553-
if (*node->test_load)
554-
data->names[data->idx++]= &node->m_svar->name;
555-
return FALSE;
556-
}
557535

558536
/* Sorts variable references array */
559537
static int name_array_sorter(const void *a, const void *b)
@@ -573,7 +551,8 @@ static int name_array_sorter(const void *a, const void *b)
573551
bool Session_sysvars_tracker::vars_list::construct_var_list(char *buf,
574552
size_t buf_len)
575553
{
576-
struct name_array_filler_data data;
554+
LEX_CSTRING **names;
555+
uint idx;
577556
size_t left= buf_len;
578557
size_t names_size= m_registered_sysvars.records * sizeof(LEX_CSTRING *);
579558
const char separator= ',';
@@ -596,16 +575,19 @@ bool Session_sysvars_tracker::vars_list::construct_var_list(char *buf,
596575
return false;
597576
}
598577

599-
data.names= (LEX_CSTRING**)my_safe_alloca(names_size);
600-
601-
if (unlikely(!data.names))
578+
if (unlikely(!(names= (LEX_CSTRING**) my_safe_alloca(names_size))))
602579
return true;
603580

604-
data.idx= 0;
581+
idx= 0;
605582

606583
mysql_mutex_lock(&LOCK_plugin);
607-
my_hash_iterate(&m_registered_sysvars, &name_array_filler, &data);
608-
DBUG_ASSERT(data.idx <= m_registered_sysvars.records);
584+
for (ulong i= 0; i < m_registered_sysvars.records; i++)
585+
{
586+
sysvar_node_st *node= at(i);
587+
if (*node->test_load)
588+
names[idx++]= &node->m_svar->name;
589+
}
590+
DBUG_ASSERT(idx <= m_registered_sysvars.records);
609591

610592
/*
611593
We check number of records again here because number of variables
@@ -618,17 +600,16 @@ bool Session_sysvars_tracker::vars_list::construct_var_list(char *buf,
618600
return false;
619601
}
620602

621-
my_qsort(data.names, data.idx, sizeof(LEX_CSTRING *),
622-
&name_array_sorter);
603+
my_qsort(names, idx, sizeof(LEX_CSTRING*), &name_array_sorter);
623604

624-
for(uint i= 0; i < data.idx; i++)
605+
for(uint i= 0; i < idx; i++)
625606
{
626-
LEX_CSTRING *nm= data.names[i];
607+
LEX_CSTRING *nm= names[i];
627608
size_t ln= nm->length + 1;
628609
if (ln > left)
629610
{
630611
mysql_mutex_unlock(&LOCK_plugin);
631-
my_safe_afree(data.names, names_size);
612+
my_safe_afree(names, names_size);
632613
return true;
633614
}
634615
memcpy(buf, nm->str, nm->length);
@@ -639,7 +620,7 @@ bool Session_sysvars_tracker::vars_list::construct_var_list(char *buf,
639620
mysql_mutex_unlock(&LOCK_plugin);
640621

641622
buf--; buf[0]= '\0';
642-
my_safe_afree(data.names, names_size);
623+
my_safe_afree(names, names_size);
643624

644625
return false;
645626
}
@@ -724,24 +705,15 @@ bool Session_sysvars_tracker::update(THD *thd, set_var *var)
724705
}
725706

726707

727-
/*
728-
Function and structure to support storing variables from hash to the buffer.
729-
*/
730-
731-
struct st_store_variable_param
732-
{
733-
THD *thd;
734-
String *buf;
735-
};
736-
737-
my_bool Session_sysvars_tracker::store_variable(void *ptr, void *data_ptr)
708+
bool Session_sysvars_tracker::vars_list::store(THD *thd, String *buf)
738709
{
739-
Session_sysvars_tracker::sysvar_node_st *node=
740-
(Session_sysvars_tracker::sysvar_node_st *)ptr;
741-
if (node->m_changed)
710+
for (ulong i= 0; i < m_registered_sysvars.records; i++)
742711
{
743-
THD *thd= ((st_store_variable_param *)data_ptr)->thd;
744-
String *buf= ((st_store_variable_param *)data_ptr)->buf;
712+
sysvar_node_st *node= at(i);
713+
714+
if (!node->m_changed)
715+
continue;
716+
745717
char val_buf[SHOW_VAR_FUNC_BUFF_SIZE];
746718
SHOW_VAR show;
747719
CHARSET_INFO *charset;
@@ -750,7 +722,7 @@ my_bool Session_sysvars_tracker::store_variable(void *ptr, void *data_ptr)
750722
if (!*node->test_load)
751723
{
752724
mysql_mutex_unlock(&LOCK_plugin);
753-
return false;
725+
continue;
754726
}
755727
sys_var *svar= node->m_svar;
756728
bool is_plugin= svar->cast_pluginvar();
@@ -795,11 +767,6 @@ my_bool Session_sysvars_tracker::store_variable(void *ptr, void *data_ptr)
795767
return false;
796768
}
797769

798-
bool Session_sysvars_tracker::vars_list::store(THD *thd, String *buf)
799-
{
800-
st_store_variable_param data= {thd, buf};
801-
return my_hash_iterate(&m_registered_sysvars, &store_variable, &data);
802-
}
803770

804771
/**
805772
Store the data for changed system variables in the specified buffer.
@@ -836,14 +803,13 @@ bool Session_sysvars_tracker::store(THD *thd, String *buf)
836803
void Session_sysvars_tracker::mark_as_changed(THD *thd,
837804
LEX_CSTRING *var)
838805
{
839-
sysvar_node_st *node= NULL;
806+
sysvar_node_st *node;
840807
sys_var *svar= (sys_var *)var;
841808
/*
842809
Check if the specified system variable is being tracked, if so
843810
mark it as changed and also set the class's m_changed flag.
844811
*/
845-
if (orig_list->is_enabled() &&
846-
(node= (sysvar_node_st *) (orig_list->insert_or_search(node, svar))))
812+
if (orig_list->is_enabled() && (node= orig_list->insert_or_search(svar)))
847813
{
848814
node->m_changed= true;
849815
State_tracker::mark_as_changed(thd, var);
@@ -870,18 +836,10 @@ uchar *Session_sysvars_tracker::sysvars_get_key(const char *entry,
870836
}
871837

872838

873-
/* Function to support resetting hash nodes for the variables */
874-
875-
my_bool Session_sysvars_tracker::reset_variable(void *ptr,
876-
void *data_ptr)
877-
{
878-
((Session_sysvars_tracker::sysvar_node_st *)ptr)->m_changed= false;
879-
return false;
880-
}
881-
882839
void Session_sysvars_tracker::vars_list::reset()
883840
{
884-
my_hash_iterate(&m_registered_sysvars, &reset_variable, NULL);
841+
for (ulong i= 0; i < m_registered_sysvars.records; i++)
842+
at(i)->m_changed= false;
885843
}
886844

887845
static Session_sysvars_tracker* sysvar_tracker(THD *thd)

0 commit comments

Comments
 (0)