Skip to content

Commit

Permalink
Fix memory bloat in my_core::handler
Browse files Browse the repository at this point in the history
Summary:
In 8.0 sizeof(my_core::handler) == 5680. This is a regression from 5.6
where the size was 912 bytes.
This is because 8.0 introduced m_random_number_engine
field of type std::mt19937 inline. The size of this type is 5000 bytes.
This regressed workloads which started taking > 50 MB more as many
handlers are initialized when running workloads.

m_random_number_engine is only used when generating histogram
samples as part of running "ANALYZE TABLE UPDATE HISTOGRAM".
Since histograms are not yet widely used and the generator is needed
only when updating histogram, convert it into a pointer field. The
object of std::mt19937 is now dynamically created when running
UPDATE HISTOGRAM, This should be ok as histograms will not be
refreshed very often.

Reviewed By: yizhang82

Differential Revision: D27251616

fbshipit-source-id: 1c122177a15
  • Loading branch information
atish2196 authored and inikep committed Oct 20, 2021
1 parent acdce5d commit 750b64f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
14 changes: 11 additions & 3 deletions sql/handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3231,21 +3231,28 @@ int handler::ha_sample_init(void *&scan_ctx, double sampling_percentage,
DBUG_ASSERT(sampling_percentage >= 0.0);
DBUG_ASSERT(sampling_percentage <= 100.0);
DBUG_ASSERT(inited == NONE);
DBUG_ASSERT(m_random_number_engine == nullptr);

// Initialise the random number generator.
m_random_number_engine.seed(sampling_seed);
m_sampling_percentage = sampling_percentage;

int result = sample_init(scan_ctx, sampling_percentage, sampling_seed,
sampling_method);
inited = (result != 0) ? NONE : SAMPLING;

// Initialise the random number generator on successful initialization.
if (result == 0) {
m_random_number_engine = new std::mt19937();
m_random_number_engine->seed(sampling_seed);
}
return result;
}

int handler::ha_sample_end(void *scan_ctx) {
DBUG_TRACE;
DBUG_ASSERT(inited == SAMPLING);
inited = NONE;
delete m_random_number_engine;
m_random_number_engine = nullptr;
int result = sample_end(scan_ctx);
return result;
}
Expand Down Expand Up @@ -3285,7 +3292,8 @@ int handler::sample_next(void *scan_ctx MY_ATTRIBUTE((unused)), uchar *buf) {
int res = rnd_next(buf);

std::uniform_real_distribution<double> rnd(0.0, 1.0);
while (!res && rnd(m_random_number_engine) > (m_sampling_percentage / 100.0))
DBUG_ASSERT(m_random_number_engine);
while (!res && rnd(*m_random_number_engine) > (m_sampling_percentage / 100.0))
res = rnd_next(buf);

return res;
Expand Down
4 changes: 3 additions & 1 deletion sql/handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -4286,7 +4286,7 @@ class handler {
*/
PSI_table *m_psi;

std::mt19937 m_random_number_engine;
std::mt19937 *m_random_number_engine;
double m_sampling_percentage;

/* TODO(yzha) - we needed these to be public for MYSQL_TABLE_IO_WAIT */
Expand Down Expand Up @@ -4408,6 +4408,7 @@ class handler {
insert_id_for_cur_row(0),
auto_inc_intervals_count(0),
m_psi(nullptr),
m_random_number_engine(nullptr),
m_psi_batch_mode(PSI_BATCH_MODE_NONE),
m_psi_numrows(0),
m_psi_locker(nullptr),
Expand All @@ -4421,6 +4422,7 @@ class handler {

virtual ~handler(void) {
DBUG_ASSERT(m_psi == nullptr);
DBUG_ASSERT(m_random_number_engine == nullptr);
DBUG_ASSERT(m_psi_batch_mode == PSI_BATCH_MODE_NONE);
DBUG_ASSERT(m_psi_locker == nullptr);
DBUG_ASSERT(m_lock_type == F_UNLCK);
Expand Down

0 comments on commit 750b64f

Please sign in to comment.