Commit c127a1ba authored by Monty's avatar Monty

Reduce memory used by maria_open

- Allocate only enough memory that we need for key status
  - Saves ~100K temp usage on maria_open
parent 9ec19b9b
......@@ -274,7 +274,7 @@ MARIA_HA *maria_open(const char *name, int mode, uint open_flags)
{
int kfile,open_mode,save_errno;
uint i,j,len,errpos,head_length,base_pos,keys, realpath_err,
key_parts,unique_key_parts,fulltext_keys,uniques;
key_parts,base_key_parts,unique_key_parts,fulltext_keys,uniques;
uint internal_table= MY_TEST(open_flags & HA_OPEN_INTERNAL_TABLE);
uint file_version;
size_t info_length;
......@@ -404,21 +404,11 @@ MARIA_HA *maria_open(const char *name, int mode, uint open_flags)
Allocate space for header information and for data that is too
big to keep on stack
*/
if (!my_multi_malloc(MY_WME,
&disk_cache, info_length+128,
&rec_per_key_part,
(sizeof(*rec_per_key_part) * HA_MAX_POSSIBLE_KEY *
HA_MAX_KEY_SEG),
&nulls_per_key_part,
(sizeof(*nulls_per_key_part) * HA_MAX_POSSIBLE_KEY *
HA_MAX_KEY_SEG),
NullS))
if (!(disk_cache= my_malloc(info_length+128, MYF(MY_WME))))
{
my_errno=ENOMEM;
goto err;
}
share_buff.state.rec_per_key_part= rec_per_key_part;
share_buff.state.nulls_per_key_part= nulls_per_key_part;
end_pos=disk_cache+info_length;
errpos= 3;
......@@ -431,7 +421,7 @@ MARIA_HA *maria_open(const char *name, int mode, uint open_flags)
keys= (uint) share->state.header.keys;
uniques= (uint) share->state.header.uniques;
fulltext_keys= (uint) share->state.header.fulltext_keys;
key_parts= mi_uint2korr(share->state.header.key_parts);
base_key_parts= key_parts= mi_uint2korr(share->state.header.key_parts);
unique_key_parts= mi_uint2korr(share->state.header.unique_key_parts);
if (len != MARIA_STATE_INFO_SIZE)
{
......@@ -441,7 +431,8 @@ MARIA_HA *maria_open(const char *name, int mode, uint open_flags)
}
share->state_diff_length=len-MARIA_STATE_INFO_SIZE;
_ma_state_info_read(disk_cache, &share->state);
if (!_ma_state_info_read(disk_cache, &share->state))
goto err;
len= mi_uint2korr(share->state.header.base_info_length);
if (len != MARIA_BASE_INFO_SIZE)
{
......@@ -582,9 +573,9 @@ MARIA_HA *maria_open(const char *name, int mode, uint open_flags)
share->open_file_name.length= strlen(name);
if (!my_multi_malloc(MY_WME,
&share,sizeof(*share),
&share->state.rec_per_key_part,
&rec_per_key_part,
sizeof(double) * key_parts,
&share->state.nulls_per_key_part,
&nulls_per_key_part,
sizeof(long)* key_parts,
&share->keyinfo,keys*sizeof(MARIA_KEYDEF),
&share->uniqueinfo,uniques*sizeof(MARIA_UNIQUEDEF),
......@@ -609,11 +600,16 @@ MARIA_HA *maria_open(const char *name, int mode, uint open_flags)
goto err;
errpos= 4;
*share=share_buff;
memcpy((char*) share->state.rec_per_key_part,
(char*) rec_per_key_part, sizeof(double)*key_parts);
memcpy((char*) share->state.nulls_per_key_part,
(char*) nulls_per_key_part, sizeof(long)*key_parts);
*share= share_buff;
share->state.rec_per_key_part= rec_per_key_part;
share->state.nulls_per_key_part= nulls_per_key_part;
memcpy((char*) rec_per_key_part,
(char*) share_buff.state.rec_per_key_part,
sizeof(double)*base_key_parts);
memcpy((char*) nulls_per_key_part,
(char*) share_buff.state.nulls_per_key_part,
sizeof(long)*base_key_parts);
memcpy((char*) share->state.key_root,
(char*) key_root, sizeof(my_off_t)*keys);
strmov(share->unique_file_name.str, name_buff);
......@@ -911,6 +907,10 @@ MARIA_HA *maria_open(const char *name, int mode, uint open_flags)
(keys ? MARIA_INDEX_BLOCK_MARGIN *
share->block_size * keys : 0));
my_free(disk_cache);
my_free(share_buff.state.rec_per_key_part);
disk_cache= 0;
share_buff.state.rec_per_key_part= 0;
_ma_setup_functions(share);
max_data_file_length= share->base.max_data_file_length;
if ((*share->once_init)(share, info.dfile.file))
......@@ -1092,6 +1092,7 @@ MARIA_HA *maria_open(const char *name, int mode, uint open_flags)
/* fall through */
case 3:
my_free(disk_cache);
my_free(share_buff.state.rec_per_key_part);
/* fall through */
case 1:
mysql_file_close(kfile,MYF(0));
......@@ -1507,6 +1508,16 @@ static uchar *_ma_state_info_read(uchar *ptr, MARIA_STATE_INFO *state)
keys= (uint) state->header.keys;
key_parts= mi_uint2korr(state->header.key_parts);
/* Allocate memory for key parts if not already done */
if (!state->rec_per_key_part &&
!my_multi_malloc(MY_WME,
&state->rec_per_key_part,
sizeof(*state->rec_per_key_part) * key_parts,
&state->nulls_per_key_part,
sizeof(*state->nulls_per_key_part) * key_parts,
NullS))
DBUG_RETURN(0);
state->open_count = mi_uint2korr(ptr); ptr+= 2;
state->changed= mi_uint2korr(ptr); ptr+= 2;
state->create_rename_lsn= lsn_korr(ptr); ptr+= LSN_STORE_SIZE;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment