We allocate a number of times for each line in types.db.
Since plugin_register_dataset() makes a copy of the dataset anyway, we
can create the dataset on the stack instead of the heap.
This reduces the number of calloc calls on my system from 624 to 337
when running collectd -T with the default types.db.
static void parse_line(char *buf) {
char *fields[64];
size_t fields_num;
static void parse_line(char *buf) {
char *fields[64];
size_t fields_num;
fields_num = strsplit(buf, fields, 64);
if (fields_num < 2)
return;
fields_num = strsplit(buf, fields, 64);
if (fields_num < 2)
return;
if (fields[0][0] == '#')
return;
if (fields[0][0] == '#')
return;
- ds = calloc(1, sizeof(*ds));
- if (ds == NULL)
- return;
- sstrncpy(ds->type, fields[0], sizeof(ds->type));
+ sstrncpy(ds.type, fields[0], sizeof(ds.type));
- ds->ds_num = fields_num - 1;
- ds->ds = calloc(ds->ds_num, sizeof(*ds->ds));
- if (ds->ds == NULL) {
- sfree(ds);
+ ds.ds_num = fields_num - 1;
+ ds.ds = calloc(ds.ds_num, sizeof(*ds.ds));
+ if (ds.ds == NULL)
- for (size_t i = 0; i < ds->ds_num; i++)
- if (parse_ds(ds->ds + i, fields[i + 1], strlen(fields[i + 1])) != 0) {
+ for (size_t i = 0; i < ds.ds_num; i++)
+ if (parse_ds(ds.ds + i, fields[i + 1], strlen(fields[i + 1])) != 0) {
ERROR("types_list: parse_line: Cannot parse data source #%" PRIsz
" of data set %s",
ERROR("types_list: parse_line: Cannot parse data source #%" PRIsz
" of data set %s",
- i, ds->type);
- sfree(ds->ds);
- sfree(ds);
+ i, ds.type);
+ sfree(ds.ds);
- plugin_register_data_set(ds);
+ plugin_register_data_set(&ds);
- sfree(ds->ds);
- sfree(ds);
} /* void parse_line */
static void parse_file(FILE *fh) {
} /* void parse_line */
static void parse_file(FILE *fh) {