Skip to content

Commit

Permalink
Optimize size of lex structures
Browse files Browse the repository at this point in the history
LEX, st_select_lex, st_select_unit optimized for space:
- Use bit fields for bool variables
- Ensure that all bit fields are initialized (improves
  performance for init functions as all bit fields can be
  initalized with one memory access)
- Move members around in above structures to remove alignment
  gaps

Some savings:
LEX: 7032 -> 6880
THD: 25608 -> 25456
st_select_lex_unit: 2048 -> 2008

LEX::start():                    1321 -> 1245 instructions
st_select_lex_unit::init_query()  284 ->  214 instructions
st_select_lex::init_query():      766 ->  692 instructions
st_select_lex::init_select():     563 ->  540 instructions

Other things:
- Removed not used LEX::select_allow_into
- Fixed MDEV-25510 Assertion `sel->select_lock ==
   st_select_lex::select_lock_type::NONE' which was caused by this commit.
  • Loading branch information
montywi authored and vuvova committed May 19, 2021
1 parent fa7d4ab commit 8e8bda7
Show file tree
Hide file tree
Showing 4 changed files with 383 additions and 302 deletions.
25 changes: 25 additions & 0 deletions mysql-test/main/parser.result
Original file line number Diff line number Diff line change
Expand Up @@ -2105,3 +2105,28 @@ ERROR HY000: Unknown data type: 'ACTION'
#
# End of 10.5 tests
#
#
# Start of 10.6 tests
#
#
# MDEV-25510 Assertion `sel->select_lock ==
# st_select_lex::select_lock_type::NONE' failed in Lex_select_lock::set_to
#
(SELECT x FROM t WINDOW w1 AS () FOR UPDATE) LIMIT 1;
ERROR 42S02: Table 'test.t' doesn't exist
create table t1 (x int);
insert into t1 values (1),(2);
explain extended (SELECT x FROM t1 WINDOW w1 as () FOR UPDATE) LIMIT 1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00
Warnings:
Note 1003 (select `test`.`t1`.`x` AS `x` from `test`.`t1` limit 1)
explain extended (SELECT x FROM t1 FOR UPDATE) LIMIT 1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00
Warnings:
Note 1003 (select `test`.`t1`.`x` AS `x` from `test`.`t1` limit 1)
drop table t1;
#
# End of 10.6 tests
#
21 changes: 21 additions & 0 deletions mysql-test/main/parser.test
Original file line number Diff line number Diff line change
Expand Up @@ -1883,3 +1883,24 @@ SELECT CAST(1 AS ACTION);
--echo #
--echo # End of 10.5 tests
--echo #

--echo #
--echo # Start of 10.6 tests
--echo #

--echo #
--echo # MDEV-25510 Assertion `sel->select_lock ==
--echo # st_select_lex::select_lock_type::NONE' failed in Lex_select_lock::set_to
--echo #

--error ER_NO_SUCH_TABLE
(SELECT x FROM t WINDOW w1 AS () FOR UPDATE) LIMIT 1;
create table t1 (x int);
insert into t1 values (1),(2);
explain extended (SELECT x FROM t1 WINDOW w1 as () FOR UPDATE) LIMIT 1;
explain extended (SELECT x FROM t1 FOR UPDATE) LIMIT 1;
drop table t1;

--echo #
--echo # End of 10.6 tests
--echo #
125 changes: 77 additions & 48 deletions sql/sql_lex.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1252,33 +1252,39 @@ void LEX::start(THD *thd_arg)
unit.slave= current_select= all_selects_list= &builtin_select;
sql_cache= LEX::SQL_CACHE_UNSPECIFIED;
describe= 0;
analyze_stmt= 0;
explain_json= false;
context_analysis_only= 0;
derived_tables= 0;
safe_to_cache_query= 1;
parsing_options.reset();
empty_field_list_on_rset= 0;
part_info= 0;
m_sql_cmd= NULL;
duplicates= DUP_ERROR;
ignore= 0;
spname= NULL;
spcont= NULL;
proc_list.first= 0;
escape_used= FALSE;
default_used= FALSE;
query_tables= 0;
reset_query_tables_list(FALSE);
clause_that_disallows_subselect= NULL;
selects_allow_into= FALSE;
selects_allow_procedure= FALSE;
use_only_table_context= FALSE;
parse_vcol_expr= FALSE;
check_exists= FALSE;
create_info.lex_start();

/* reset bool variables */
is_shutdown_wait_for_slaves= 0;
selects_allow_procedure= 0;
parse_vcol_expr= 0;
analyze_stmt= 0;
explain_json= 0;
local_file= 0;
check_exists= 0;
verbose= 0;
safe_to_cache_query= 1;
ignore= 0;
next_is_main= 0;
next_is_down= 0;
empty_field_list_on_rset= 0;
use_only_table_context= 0;
escape_used= 0;
default_used= 0;
is_lex_started= 1;

create_info.lex_start();
name= null_clex_str;
event_parse_data= NULL;
profile_options= PROFILE_NONE;
Expand Down Expand Up @@ -1307,11 +1313,6 @@ void LEX::start(THD *thd_arg)
vers_conditions.empty();
period_conditions.empty();

is_lex_started= TRUE;

next_is_main= FALSE;
next_is_down= FALSE;

wild= 0;
exchange= 0;

Expand Down Expand Up @@ -2898,25 +2899,30 @@ void st_select_lex_unit::init_query()
set_linkage(GLOBAL_OPTIONS_TYPE);
lim.clear();
union_distinct= 0;
prepared= optimized= optimized_2= executed= 0;
bag_set_op_optimized= 0;
optimize_started= 0;
item= 0;
union_result= 0;
table= 0;
fake_select_lex= 0;
saved_fake_select_lex= 0;
cleaned= 0;
item_list.empty();
describe= 0;
found_rows_for_union= 0;
derived= 0;
is_view= false;
with_clause= 0;
with_element= 0;
columns_are_renamed= false;
with_wrapped_tvc= false;
have_except_all_or_intersect_all= false;

/* reset all bit fields */
prepared= 0;
optimized= 0;
optimized_2= 0;
executed= 0;
cleaned= 0;
bag_set_op_optimized= 0;
optimize_started= 0;
have_except_all_or_intersect_all= 0;
with_wrapped_tvc= 0;
is_view= 0;
describe= 0;
columns_are_renamed= 0;
}

void st_select_lex::init_query()
Expand All @@ -2930,13 +2936,37 @@ void st_select_lex::init_query()
leaf_tables.empty();
item_list.empty();
min_max_opt_list.empty();
limit_params.clear();
join= 0;
having= prep_having= where= prep_where= 0;
cond_pushed_into_where= cond_pushed_into_having= 0;
attach_to_conds.empty();
olap= UNSPECIFIED_OLAP_TYPE;

/* reset all bit fields */
is_item_list_lookup= 0;
have_merged_subqueries= 0;
is_set_query_expr_tail= 0;
with_sum_func= 0;
braces= 0;
automatic_brackets= 0;
having_fix_field= 0;
having_fix_field_for_pushed_cond= 0;
subquery_in_having= 0;
is_item_list_lookup= 0;
with_all_modifier= 0;
is_correlated= 0;
first_natural_join_processing= 1;
first_cond_optimization= 1;
no_wrap_view_item= 0;
exclude_from_table_unique_test= 0;
in_tvc= 0;
skip_locked= 0;
m_non_agg_field_used= 0;
m_agg_func_used= 0;
m_custom_agg_func_used= 0;
is_service_select= 0;

context.select_lex= this;
context.init();
cond_count= between_count= with_wild= 0;
Expand All @@ -2949,29 +2979,18 @@ void st_select_lex::init_query()
n_child_sum_items= 0;
hidden_bit_fields= 0;
fields_in_window_functions= 0;
subquery_in_having= 0;
is_item_list_lookup= 0;
changed_elements= 0;
first_natural_join_processing= 1;
first_cond_optimization= 1;
is_service_select= 0;
parsing_place= NO_MATTER;
save_parsing_place= NO_MATTER;
context_analysis_place= NO_MATTER;
exclude_from_table_unique_test= no_wrap_view_item= FALSE;
nest_level= 0;
link_next= 0;
prep_leaf_list_state= UNINIT;
have_merged_subqueries= FALSE;
bzero((char*) expr_cache_may_be_used, sizeof(expr_cache_may_be_used));
select_list_tables= 0;
m_non_agg_field_used= false;
m_agg_func_used= false;
m_custom_agg_func_used= false;
window_specs.empty();
window_funcs.empty();
tvc= 0;
in_tvc= false;
versioned_tables= 0;
pushdown_select= 0;
}
Expand All @@ -2986,6 +3005,8 @@ void st_select_lex::init_select()
type= 0;
db= null_clex_str;
having= 0;
table_join_options= 0;
select_lock= select_lock_type::NONE;
in_sum_expr= with_wild= 0;
options= 0;
ftfunc_list_alloc.empty();
Expand All @@ -2994,20 +3015,23 @@ void st_select_lex::init_select()
order_list.empty();
/* Set limit and offset to default values */
limit_params.clear();
is_set_query_expr_tail= false;
select_lock= select_lock_type::NONE;
skip_locked= false;

/* Reset bit fields */
is_set_query_expr_tail= 0;
with_sum_func= 0;
with_all_modifier= 0;
is_correlated= 0;
in_tvc= 0;
skip_locked= 0;
m_non_agg_field_used= 0;
m_agg_func_used= 0;
m_custom_agg_func_used= 0;

cur_pos_in_select_list= UNDEF_POS;
cond_value= having_value= Item::COND_UNDEF;
inner_refs_list.empty();
insert_tables= 0;
merged_into= 0;
m_non_agg_field_used= false;
m_agg_func_used= false;
m_custom_agg_func_used= false;
name_visibility_map.clear_all();
with_dep= 0;
join= 0;
Expand All @@ -3017,7 +3041,6 @@ void st_select_lex::init_select()
tvc= 0;
in_funcs.empty();
curr_tvc_name= 0;
in_tvc= false;
versioned_tables= 0;
nest_flags= 0;
}
Expand Down Expand Up @@ -3937,8 +3960,9 @@ void Query_tables_list::destroy_query_tables_list()

LEX::LEX()
: explain(NULL), result(0), part_info(NULL), arena_for_set_stmt(0), mem_root_for_set_stmt(0),
json_table(NULL), option_type(OPT_DEFAULT), context_analysis_only(0), sphead(0),
default_used(0), is_lex_started(0), limit_rows_examined_cnt(ULONGLONG_MAX)
json_table(NULL), default_used(0), is_lex_started(0),
option_type(OPT_DEFAULT), context_analysis_only(0), sphead(0),
limit_rows_examined_cnt(ULONGLONG_MAX)
{

init_dynamic_array2(PSI_INSTRUMENT_ME, &plugins, sizeof(plugin_ref),
Expand Down Expand Up @@ -9782,7 +9806,13 @@ void Lex_select_lock::set_to(SELECT_LEX *sel)
}
}
else
{
/*
select_lock can be FOR_UPDATE in case of
(SELECT x FROM t WINDOW w1 AS () FOR UPDATE) LIMIT 1
*/
sel->select_lock= st_select_lex::select_lock_type::NONE;
}
}

bool Lex_order_limit_lock::set_to(SELECT_LEX *sel)
Expand Down Expand Up @@ -10272,7 +10302,6 @@ bool LEX::parsed_create_view(SELECT_LEX_UNIT *unit, int check)
bool LEX::select_finalize(st_select_lex_unit *expr)
{
sql_command= SQLCOM_SELECT;
selects_allow_into= TRUE;
selects_allow_procedure= TRUE;
if (set_main_unit(expr))
return true;
Expand Down
Loading

0 comments on commit 8e8bda7

Please sign in to comment.