Skip to content

Commit

Permalink
sql_cacher: Avoid redundant memcpy() operations
Browse files Browse the repository at this point in the history
  • Loading branch information
liviuchircu committed Apr 1, 2021
1 parent e3bfd3a commit 9e81f5b
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions modules/sql_cacher/sql_cacher.c
Expand Up @@ -531,7 +531,7 @@ static int insert_in_cachedb(cache_entry_t *c_entry, db_handlers_t *db_hdls,
unsigned int i, offset = 0, strs_offset = 0;
int int_val;
int int_key_len = 0, rc = 0;
char int_buf[4], int_enc_buf[INT_B64_ENC_LEN];
char int_enc_buf[INT_B64_ENC_LEN];
char *int_key_buf = NULL;
str str_val;
db_type_t val_type;
Expand All @@ -547,8 +547,7 @@ static int insert_in_cachedb(cache_entry_t *c_entry, db_handlers_t *db_hdls,
}

/* store the reload version (base64 encoded) */
memcpy(int_buf, &reload_version, 4);
base64encode((unsigned char *)int_enc_buf, (unsigned char *)int_buf, 4);
base64encode((unsigned char *)int_enc_buf, (unsigned char *)&reload_version, 4);
memcpy(cdb_val.s, int_enc_buf, INT_B64_ENC_LEN);

offset += INT_B64_ENC_LEN;
Expand All @@ -570,15 +569,13 @@ static int insert_in_cachedb(cache_entry_t *c_entry, db_handlers_t *db_hdls,
break;
default: continue;
}

if (VAL_NULL(values + i))
memset(int_enc_buf, 0, INT_B64_ENC_LEN);
else {
memcpy(int_buf, &int_val, 4);
base64encode((unsigned char *)int_enc_buf, (unsigned char *)int_buf, 4);
}
else
base64encode((unsigned char *)int_enc_buf, (unsigned char *)&int_val, 4);

memcpy(cdb_val.s + offset, int_enc_buf, INT_B64_ENC_LEN);

offset += INT_B64_ENC_LEN;
}

Expand All @@ -600,15 +597,14 @@ static int insert_in_cachedb(cache_entry_t *c_entry, db_handlers_t *db_hdls,
break;
default: continue;
}

if (VAL_NULL(values + i))
int_val = 0;
else
int_val = strs_offset;

memcpy(int_buf, &int_val, 4);
base64encode((unsigned char *)int_enc_buf, (unsigned char *)int_buf, 4);
base64encode((unsigned char *)int_enc_buf, (unsigned char *)&int_val, 4);
memcpy(cdb_val.s + offset, int_enc_buf, INT_B64_ENC_LEN);

offset += INT_B64_ENC_LEN;

memcpy(cdb_val.s + strs_offset, str_val.s, str_val.len);
Expand Down Expand Up @@ -1345,7 +1341,6 @@ static int cdb_val_decode(pv_name_fix_t *pv_name, str *cdb_val, int reload_versi
str *str_res, int *int_res)
{
int int_val, next_str_off, i, rc;
char int_buf[4];
const char zeroes[INT_B64_ENC_LEN] = {0};

if (pv_name->col_offset == -1) {
Expand All @@ -1354,10 +1349,9 @@ static int cdb_val_decode(pv_name_fix_t *pv_name, str *cdb_val, int reload_versi
}

/* decode the reload version */
if (base64decode((unsigned char *)int_buf,
if (base64decode((unsigned char *)&int_val,
(unsigned char *)(cdb_val->s), INT_B64_ENC_LEN) != 4)
goto error;
memcpy(&int_val, int_buf, 4);

if (reload_version != int_val)
return 3;
Expand All @@ -1367,10 +1361,9 @@ static int cdb_val_decode(pv_name_fix_t *pv_name, str *cdb_val, int reload_versi
return 1;

/* decode the integer value or the offset of the string value */
if (base64decode((unsigned char *)int_buf,
if (base64decode((unsigned char *)&int_val,
(unsigned char *)(cdb_val->s + pv_name->col_offset), INT_B64_ENC_LEN) != 4)
goto error;
memcpy(&int_val, int_buf, 4);

if (is_str_column(pv_name)) {
/* null string value in db */
Expand All @@ -1384,11 +1377,10 @@ static int cdb_val_decode(pv_name_fix_t *pv_name, str *cdb_val, int reload_versi
/* calculate the length of the current string using the offset of the next not null string */
i = 1;
do {
rc = base64decode((unsigned char *)int_buf, (unsigned char *)(cdb_val->s +
rc = base64decode((unsigned char *)&next_str_off, (unsigned char *)(cdb_val->s +
pv_name->col_offset + i * INT_B64_ENC_LEN), INT_B64_ENC_LEN);
if (rc != 4)
goto error;
memcpy(&next_str_off, int_buf, 4);
i++;
} while (next_str_off == 0 && pv_name->col_offset + i*INT_B64_ENC_LEN <
(pv_name->c_entry->nr_columns + 1) * INT_B64_ENC_LEN);
Expand Down

0 comments on commit 9e81f5b

Please sign in to comment.