Skip to content

Commit 0f04cf5

Browse files
cellison-hashbrownsandywolk
authored andcommitted
switch_event: clarify EF_UNIQ_HEADERS dup comment, brace one-line test loops
Address review feedback on the switch_event_dup change: - src/switch_event.c: the fast-path comment claimed todup's headers are "already unique" unconditionally. That only holds when EF_UNIQ_HEADERS is set (names are deduped on insert); reword to scope the claim to that case and to the verbatim-copy intent. - tests/unit/switch_event.c: expand one-line loops to braced form per the SignalWire coding guidelines. Signed-off-by: Calvin Ellison <cellison@youmail.com>
1 parent 42df5e6 commit 0f04cf5

2 files changed

Lines changed: 42 additions & 12 deletions

File tree

src/switch_event.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,11 +1346,13 @@ SWITCH_DECLARE(switch_status_t) switch_event_dup(switch_event_t **event, switch_
13461346
(*event)->bind_user_data = todup->bind_user_data;
13471347
(*event)->flags = todup->flags;
13481348

1349-
/* todup's headers are already unique (uniqueness is enforced on insert for an
1350-
* EF_UNIQ_HEADERS event), so the per-add duplicate scan that
1351-
* switch_event_base_add_header() performs when EF_UNIQ_HEADERS is set is
1352-
* redundant while cloning and makes this loop O(n^2) in the header count.
1353-
* Suppress the scan for the copy, then restore the flag. */
1349+
/* The destination inherited todup's flags above. When EF_UNIQ_HEADERS is set,
1350+
* switch_event_base_add_header() rescans the partial header list on every add
1351+
* to keep names unique, making this copy loop O(n^2). A clone should reproduce
1352+
* todup's list verbatim rather than run a fresh uniqueness pass, and a
1353+
* well-formed EF_UNIQ_HEADERS source is already unique (its names were deduped
1354+
* on insert), so that scan only adds cost here. Suppress it for the copy, then
1355+
* restore the flag so later adds enforce uniqueness again. */
13541356
if (switch_test_flag((*event), EF_UNIQ_HEADERS)) {
13551357
switch_clear_flag((*event), EF_UNIQ_HEADERS);
13561358
restore_uniq_headers = 1;

tests/unit/switch_event.c

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,18 @@ FST_TEST_BEGIN(dup_uniq_bench)
130130
switch_event_add_header_string(src, SWITCH_STACK_BOTTOM, name, val);
131131
}
132132
S = 0;
133-
for (hp = src->headers; hp; hp = hp->next) S++;
133+
for (hp = src->headers; hp; hp = hp->next) {
134+
S++;
135+
}
134136
fst_xcheck(S >= 191, "source has at least the 191 added headers");
135137

136138
/* correctness: dup preserves header count, the uniq flag, and values */
137139
status = switch_event_dup(&dup, src);
138140
fst_xcheck(status == SWITCH_STATUS_SUCCESS, "dup ok");
139141
count = 0;
140-
for (hp = dup->headers; hp; hp = hp->next) count++;
142+
for (hp = dup->headers; hp; hp = hp->next) {
143+
count++;
144+
}
141145
fst_xcheck(count == S, "dup header count equals source");
142146
fst_xcheck(switch_test_flag(dup, EF_UNIQ_HEADERS) != 0, "dup keeps EF_UNIQ_HEADERS");
143147
fst_check_string_equals(switch_event_get_header(dup, "var_0"), "value_0_some_padding_payload");
@@ -149,7 +153,11 @@ FST_TEST_BEGIN(dup_uniq_bench)
149153
status = switch_event_dup(&dup, src);
150154
fst_xcheck(status == SWITCH_STATUS_SUCCESS, "dup ok (2)");
151155
k = 0;
152-
for (hp = dup->headers; hp; hp = hp->next) { if (!strcmp(hp->name, "var_0")) k++; }
156+
for (hp = dup->headers; hp; hp = hp->next) {
157+
if (!strcmp(hp->name, "var_0")) {
158+
k++;
159+
}
160+
}
153161
fst_xcheck(k == 1, "var_0 present exactly once after re-set + dup");
154162
fst_check_string_equals(switch_event_get_header(dup, "var_0"), "REPLACED");
155163
switch_event_destroy(&dup);
@@ -201,11 +209,21 @@ FST_TEST_BEGIN(dup_faithful_copy)
201209
fst_xcheck(switch_test_flag(src, EF_UNIQ_HEADERS) != 0, "CHANNEL_DATA is EF_UNIQ");
202210
switch_event_add_header_string(src, SWITCH_STACK_BOTTOM, "k", "v1");
203211
switch_event_add_header_string(src, SWITCH_STACK_BOTTOM, "k", "v2");
204-
n = 0; for (hp = src->headers; hp; hp = hp->next) { if (!strcmp(hp->name, "k")) n++; }
212+
n = 0;
213+
for (hp = src->headers; hp; hp = hp->next) {
214+
if (!strcmp(hp->name, "k")) {
215+
n++;
216+
}
217+
}
205218
fst_xcheck(n == 1, "EF_UNIQ source keeps 'k' unique (collapsed to last)");
206219
status = switch_event_dup(&dup, src);
207220
fst_xcheck(status == SWITCH_STATUS_SUCCESS, "dup ok (unique)");
208-
n = 0; for (hp = dup->headers; hp; hp = hp->next) { if (!strcmp(hp->name, "k")) n++; }
221+
n = 0;
222+
for (hp = dup->headers; hp; hp = hp->next) {
223+
if (!strcmp(hp->name, "k")) {
224+
n++;
225+
}
226+
}
209227
fst_xcheck(n == 1, "dup of unique source stays unique");
210228
fst_check_string_equals(switch_event_get_header(dup, "k"), "v2");
211229
switch_event_destroy(&dup);
@@ -218,12 +236,22 @@ FST_TEST_BEGIN(dup_faithful_copy)
218236
switch_event_add_header_string(src, SWITCH_STACK_BOTTOM, "dupkey", "first");
219237
switch_event_add_header_string(src, SWITCH_STACK_BOTTOM, "dupkey", "second");
220238
switch_set_flag(src, EF_UNIQ_HEADERS);
221-
n = 0; for (hp = src->headers; hp; hp = hp->next) { if (!strcmp(hp->name, "dupkey")) n++; }
239+
n = 0;
240+
for (hp = src->headers; hp; hp = hp->next) {
241+
if (!strcmp(hp->name, "dupkey")) {
242+
n++;
243+
}
244+
}
222245
fst_xcheck(n == 2, "malformed source holds 2 'dupkey' headers");
223246
status = switch_event_dup(&dup, src);
224247
fst_xcheck(status == SWITCH_STATUS_SUCCESS, "dup ok (malformed)");
225248
fst_xcheck(switch_test_flag(dup, EF_UNIQ_HEADERS) != 0, "dup keeps EF_UNIQ flag");
226-
n = 0; for (hp = dup->headers; hp; hp = hp->next) { if (!strcmp(hp->name, "dupkey")) n++; }
249+
n = 0;
250+
for (hp = dup->headers; hp; hp = hp->next) {
251+
if (!strcmp(hp->name, "dupkey")) {
252+
n++;
253+
}
254+
}
227255
fst_xcheck(n == 2, "dup faithfully preserves both 'dupkey' headers (no silent collapse)");
228256
switch_event_destroy(&dup);
229257
switch_event_destroy(&src);

0 commit comments

Comments
 (0)