Skip to content

Commit

Permalink
Avoid unnecessary string copies
Browse files Browse the repository at this point in the history
Do not duplicate the strings for enumeration keys, as they are read-
only.
  • Loading branch information
cgzones authored and allinurl committed May 14, 2024
1 parent 2e6c0a7 commit e49a3e4
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/commons.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,13 @@ str2enum (const GEnum map[], int len, const char *str) {
*
* On error, -1 is returned.
* On success, the enumerated module value is returned. */
char *
const char *
enum2str (const GEnum map[], int len, int idx) {
int i;

for (i = 0; i < len; ++i) {
if (idx == map[i].idx)
return xstrdup (map[i].str);
return map[i].str;
}

return NULL;
Expand All @@ -221,7 +221,7 @@ get_module_enum (const char *str) {
*
* On error, NULL is returned.
* On success, the string module value is returned. */
char *
const char *
get_module_str (GModule module) {
return enum2str (enum_modules, ARRAY_SIZE (enum_modules), module);
}
Expand Down
4 changes: 2 additions & 2 deletions src/commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ extern int module_list[TOTAL_MODULES];
GAgents *new_gagents (uint32_t size);
void free_agents_array (GAgents *agents);

char *enum2str (const GEnum map[], int len, int idx);
char *get_module_str (GModule module);
const char *enum2str (const GEnum map[], int len, int idx);
const char *get_module_str (GModule module);
float get_percentage (unsigned long long total, unsigned long long hit);
int get_max_choices (void);
int get_module_enum (const char *str);
Expand Down
2 changes: 1 addition & 1 deletion src/gkhash.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ new_gkdb (void) {
*
* On error, NULL is returned.
* On success, the string module value is returned. */
char *
const char *
get_mtr_type_str (GSMetricType type) {
static const GEnum enum_metric_types[] = {
{"II32", MTRC_TYPE_II32},
Expand Down
2 changes: 1 addition & 1 deletion src/gkhash.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ uint32_t *get_sorted_dates (uint32_t * len);
void free_storage (void);
void init_pre_storage (Logs *logs);

char *get_mtr_type_str (GSMetricType type);
const char *get_mtr_type_str (GSMetricType type);
void *get_db_instance (uint32_t key);
void *get_hdb (GKDB * db, GAMetric mtrc);

Expand Down
4 changes: 1 addition & 3 deletions src/gkmhash.c
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ ht_insert_keymap (GModule module, uint32_t date, uint32_t key, uint32_t *ckey) {
khash_t (ii32) * cache = get_hash_from_cache (module, MTRC_KEYMAP);

uint32_t val = 0;
char *modstr = NULL;
const char *modstr;

if (!hash)
return 0;
Expand All @@ -432,11 +432,9 @@ ht_insert_keymap (GModule module, uint32_t date, uint32_t key, uint32_t *ckey) {

modstr = get_module_str (module);
if ((val = ins_ii32_inc (hash, key, ht_ins_seq, seqs, modstr)) == 0) {
free (modstr);
return val;
}
*ckey = ins_ii32_ai (cache, key);
free (modstr);

return val;
}
Expand Down
2 changes: 1 addition & 1 deletion src/gstorage.c
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ free_gmetrics (GMetrics *metric) {
*
* On error, NULL is returned.
* On success, the string module value is returned. */
char *
const char *
get_mtr_str (GSMetric metric) {
/* String modules to enumerated modules */
static const GEnum enum_metrics[] = {
Expand Down
2 changes: 1 addition & 1 deletion src/gstorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ extern const httpprotocols http_protocols[];
extern const size_t http_methods_len;
extern const size_t http_protocols_len;

char *get_mtr_str (GSMetric metric);
const char *get_mtr_str (GSMetric metric);
int excluded_ip (GLogItem * logitem);
uint32_t *i322ptr (uint32_t val);
uint64_t *uint642ptr (uint64_t val);
Expand Down
11 changes: 3 additions & 8 deletions src/persistence.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ build_filename (const char *type, const char *modstr, const char *mtrstr) {
* On success, the filename is returned */
static char *
get_filename (GModule module, GKHashMetric mtrc) {
char *mtrstr = NULL, *modstr = NULL, *type = NULL, *fn = NULL;
const char *mtrstr, *modstr, *type;
char *fn = NULL;

if (!(mtrstr = get_mtr_str (mtrc.metric.storem)))
FATAL ("Unable to allocate metric name.");
Expand All @@ -243,10 +244,6 @@ get_filename (GModule module, GKHashMetric mtrc) {

fn = build_filename (type, modstr, mtrstr);

free (mtrstr);
free (type);
free (modstr);

return fn;
}

Expand Down Expand Up @@ -945,7 +942,7 @@ migrate_metric (GModule module, GKHashMetric mtrc) {

int ret = 0;
char *fn = NULL, *path = NULL;
char *modstr = NULL, *mtrstr = NULL;
const char *modstr, *mtrstr;
khint_t k;

k = kh_get (si32, db_props, "version");
Expand Down Expand Up @@ -1001,8 +998,6 @@ migrate_metric (GModule module, GKHashMetric mtrc) {
}

free (fn);
free (modstr);
free (mtrstr);
free (path);

return ret;
Expand Down

0 comments on commit e49a3e4

Please sign in to comment.