Skip to content

Commit 8dd472d

Browse files
committed
trace2: do not use strbuf_split*()
tr2_cfg_load_patterns() and tr2_load_env_vars() functions are functions with very similar structure that each reads an environment variable, splits its value at the ',' boundaries, and trims the resulting string pieces into an array of strbufs. But the code paths that later use these strbufs take no advantage of the strbuf-ness of the result (they do not benefit from <ptr,len> representation to avoid having to run strlen(<ptr>), for example). Simplify the code by teaching these functions to split into a string list instead; even the trimming comes for free ;-). Signed-off-by: Junio C Hamano <[email protected]>
1 parent 820dcc2 commit 8dd472d

File tree

1 file changed

+27
-51
lines changed

1 file changed

+27
-51
lines changed

trace2/tr2_cfg.c

Lines changed: 27 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,87 +8,65 @@
88
#include "trace2/tr2_sysenv.h"
99
#include "wildmatch.h"
1010

11-
static struct strbuf **tr2_cfg_patterns;
12-
static int tr2_cfg_count_patterns;
11+
static struct string_list tr2_cfg_patterns = STRING_LIST_INIT_DUP;
1312
static int tr2_cfg_loaded;
1413

15-
static struct strbuf **tr2_cfg_env_vars;
16-
static int tr2_cfg_env_vars_count;
14+
static struct string_list tr2_cfg_env_vars = STRING_LIST_INIT_DUP;
1715
static int tr2_cfg_env_vars_loaded;
1816

1917
/*
2018
* Parse a string containing a comma-delimited list of config keys
21-
* or wildcard patterns into a list of strbufs.
19+
* or wildcard patterns into a string list.
2220
*/
23-
static int tr2_cfg_load_patterns(void)
21+
static size_t tr2_cfg_load_patterns(void)
2422
{
25-
struct strbuf **s;
2623
const char *envvar;
2724

2825
if (tr2_cfg_loaded)
29-
return tr2_cfg_count_patterns;
26+
return tr2_cfg_patterns.nr;
3027
tr2_cfg_loaded = 1;
3128

3229
envvar = tr2_sysenv_get(TR2_SYSENV_CFG_PARAM);
3330
if (!envvar || !*envvar)
34-
return tr2_cfg_count_patterns;
31+
return tr2_cfg_patterns.nr;
3532

36-
tr2_cfg_patterns = strbuf_split_buf(envvar, strlen(envvar), ',', -1);
37-
for (s = tr2_cfg_patterns; *s; s++) {
38-
struct strbuf *buf = *s;
39-
40-
if (buf->len && buf->buf[buf->len - 1] == ',')
41-
strbuf_setlen(buf, buf->len - 1);
42-
strbuf_trim(*s);
43-
}
44-
45-
tr2_cfg_count_patterns = s - tr2_cfg_patterns;
46-
return tr2_cfg_count_patterns;
33+
string_list_split_f(&tr2_cfg_patterns, envvar, ",", -1,
34+
STRING_LIST_SPLIT_TRIM);
35+
return tr2_cfg_patterns.nr;
4736
}
4837

4938
void tr2_cfg_free_patterns(void)
5039
{
51-
if (tr2_cfg_patterns)
52-
strbuf_list_free(tr2_cfg_patterns);
53-
tr2_cfg_count_patterns = 0;
40+
if (tr2_cfg_patterns.nr)
41+
string_list_clear(&tr2_cfg_patterns, 0);
5442
tr2_cfg_loaded = 0;
5543
}
5644

5745
/*
5846
* Parse a string containing a comma-delimited list of environment variable
59-
* names into a list of strbufs.
47+
* names into a string list.
6048
*/
61-
static int tr2_load_env_vars(void)
49+
static size_t tr2_load_env_vars(void)
6250
{
63-
struct strbuf **s;
6451
const char *varlist;
6552

6653
if (tr2_cfg_env_vars_loaded)
67-
return tr2_cfg_env_vars_count;
54+
return tr2_cfg_env_vars.nr;
6855
tr2_cfg_env_vars_loaded = 1;
6956

7057
varlist = tr2_sysenv_get(TR2_SYSENV_ENV_VARS);
7158
if (!varlist || !*varlist)
72-
return tr2_cfg_env_vars_count;
73-
74-
tr2_cfg_env_vars = strbuf_split_buf(varlist, strlen(varlist), ',', -1);
75-
for (s = tr2_cfg_env_vars; *s; s++) {
76-
struct strbuf *buf = *s;
77-
78-
if (buf->len && buf->buf[buf->len - 1] == ',')
79-
strbuf_setlen(buf, buf->len - 1);
80-
strbuf_trim(*s);
81-
}
59+
return tr2_cfg_env_vars.nr;
8260

83-
tr2_cfg_env_vars_count = s - tr2_cfg_env_vars;
84-
return tr2_cfg_env_vars_count;
61+
string_list_split_f(&tr2_cfg_env_vars, varlist, ",", -1,
62+
STRING_LIST_SPLIT_TRIM);
63+
return tr2_cfg_env_vars.nr;
8564
}
8665

8766
void tr2_cfg_free_env_vars(void)
8867
{
89-
if (tr2_cfg_env_vars)
90-
strbuf_list_free(tr2_cfg_env_vars);
91-
tr2_cfg_env_vars_count = 0;
68+
if (tr2_cfg_env_vars.nr)
69+
string_list_clear(&tr2_cfg_env_vars, 0);
9270
tr2_cfg_env_vars_loaded = 0;
9371
}
9472

@@ -103,12 +81,11 @@ struct tr2_cfg_data {
10381
static int tr2_cfg_cb(const char *key, const char *value,
10482
const struct config_context *ctx, void *d)
10583
{
106-
struct strbuf **s;
84+
struct string_list_item *item;
10785
struct tr2_cfg_data *data = (struct tr2_cfg_data *)d;
10886

109-
for (s = tr2_cfg_patterns; *s; s++) {
110-
struct strbuf *buf = *s;
111-
int wm = wildmatch(buf->buf, key, WM_CASEFOLD);
87+
for_each_string_list_item(item, &tr2_cfg_patterns) {
88+
int wm = wildmatch(item->string, key, WM_CASEFOLD);
11289
if (wm == WM_MATCH) {
11390
trace2_def_param_fl(data->file, data->line, key, value,
11491
ctx->kvi);
@@ -130,17 +107,16 @@ void tr2_cfg_list_config_fl(const char *file, int line)
130107
void tr2_list_env_vars_fl(const char *file, int line)
131108
{
132109
struct key_value_info kvi = KVI_INIT;
133-
struct strbuf **s;
110+
struct string_list_item *item;
134111

135112
kvi_from_param(&kvi);
136113
if (tr2_load_env_vars() <= 0)
137114
return;
138115

139-
for (s = tr2_cfg_env_vars; *s; s++) {
140-
struct strbuf *buf = *s;
141-
const char *val = getenv(buf->buf);
116+
for_each_string_list_item(item, &tr2_cfg_env_vars) {
117+
const char *val = getenv(item->string);
142118
if (val && *val)
143-
trace2_def_param_fl(file, line, buf->buf, val, &kvi);
119+
trace2_def_param_fl(file, line, item->string, val, &kvi);
144120
}
145121
}
146122

0 commit comments

Comments
 (0)