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
  • Loading branch information
montywi committed Apr 9, 2021
1 parent 611d7e3 commit 5907630
Show file tree
Hide file tree
Showing 2 changed files with 328 additions and 306 deletions.
112 changes: 65 additions & 47 deletions sql/sql_lex.cc
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 @@ -2912,25 +2913,30 @@ void st_select_lex_unit::init_query()
set_linkage(GLOBAL_OPTIONS_TYPE);
lim.set_unlimited();
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 @@ -2949,8 +2955,29 @@ void st_select_lex::init_query()
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;
explicit_limit= 0;
subquery_in_having= 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;
m_non_agg_field_used= 0;
m_agg_func_used= 0;
m_custom_agg_func_used= 0;

context.select_lex= this;
context.init();
cond_count= between_count= with_wild= 0;
Expand All @@ -2963,28 +2990,18 @@ void st_select_lex::init_query()
n_child_sum_items= 0;
hidden_bit_fields= 0;
fields_in_window_functions= 0;
subquery_in_having= explicit_limit= 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;
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 @@ -3009,20 +3026,23 @@ void st_select_lex::init_select()
/* Set limit and offset to default values */
select_limit= 0; /* denotes the default limit = HA_POS_ERROR */
offset_limit= 0; /* denotes the default offset = 0 */
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;
m_non_agg_field_used= 0;
m_agg_func_used= 0;
m_custom_agg_func_used= 0;
in_tvc= 0;
skip_locked= 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 @@ -3032,7 +3052,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 @@ -3923,8 +3942,8 @@ 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),
option_type(OPT_DEFAULT), context_analysis_only(0), sphead(0),
default_used(0), is_lex_started(0), limit_rows_examined_cnt(ULONGLONG_MAX)
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 @@ -10202,7 +10221,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

0 comments on commit 5907630

Please sign in to comment.