Skip to content

Commit 049e78c

Browse files
committed
trace: we only ever add two tags, so use a static array.
Avoids allocations. Also assume that name and value parameters outlive the trace span, so don't copy. Before: real 0m16.421000-18.407000(17.8128+/-0.72)s user 0m14.242000-16.041000(15.5382+/-0.67)s sys 0m2.179000-2.363000(2.273+/-0.061)s After: real 0m13.441000-14.592000(14.2686+/-0.43)s user 0m11.265000-12.289000(11.9626+/-0.37)s sys 0m2.175000-2.381000(2.3048+/-0.072)s Signed-off-by: Rusty Russell <[email protected]>
1 parent c3d452b commit 049e78c

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

common/trace.c

+30-16
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@
4343
const char *trace_service_name = "lightningd";
4444
static bool disable_trace = false;
4545

46+
#define SPAN_MAX_TAGS 2
47+
4648
struct span_tag {
47-
char *name, *value;
49+
const char *name;
50+
const char *valuestr;
51+
int valuelen;
4852
};
4953

5054
struct span {
@@ -64,7 +68,7 @@ struct span {
6468
* spans. */
6569
size_t key;
6670
struct span *parent;
67-
struct span_tag *tags;
71+
struct span_tag tags[SPAN_MAX_TAGS];
6872
const char *name;
6973

7074
/* Indicate whether this is a remote span, i.e., it was
@@ -242,9 +246,13 @@ static void trace_emit(struct span *s)
242246
}
243247

244248
tal_append_fmt(&res, "\"tags\": {");
245-
for (size_t i = 0; i < tal_count(s->tags); i++) {
246-
tal_append_fmt(&res, "%s\"%s\": \"%s\"", i == 0 ? "" : ", ",
247-
s->tags[i].name, s->tags[i].value);
249+
for (size_t i = 0; i < SPAN_MAX_TAGS; i++) {
250+
if (!s->tags[i].name)
251+
continue;
252+
tal_append_fmt(&res, "%s\"%s\": \"%.*s\"", i == 0 ? "" : ", ",
253+
s->tags[i].name,
254+
s->tags[i].valuelen,
255+
s->tags[i].valuestr);
248256
}
249257

250258
tal_append_fmt(&res, "}, \"traceId\": \"%s\"}]", trace_id);
@@ -263,7 +271,8 @@ static void trace_span_clear(struct span *s)
263271

264272
s->parent = NULL;
265273
s->name = NULL;
266-
s->tags = tal_free(s->tags);
274+
for (size_t i = 0; i < SPAN_MAX_TAGS; i++)
275+
s->tags[i].name = NULL;
267276
}
268277

269278
void trace_span_start_(const char *name, const void *key)
@@ -284,7 +293,6 @@ void trace_span_start_(const char *name, const void *key)
284293
randombytes_buf(s->id, SPAN_ID_SIZE);
285294
s->start_time = (now.ts.tv_sec * 1000000) + now.ts.tv_nsec / 1000;
286295
s->parent = current;
287-
s->tags = notleak(tal_arr(NULL, struct span_tag, 0));
288296
s->name = name;
289297

290298
/* If this is a new root span we also need to associate a new
@@ -349,16 +357,22 @@ void trace_span_tag(const void *key, const char *name, const char *value)
349357
struct span *span = trace_span_find(numkey);
350358
assert(span);
351359

352-
size_t s = tal_count(span->tags);
353-
tal_resize(&span->tags, s + 1);
354-
span->tags[s].name = tal_strdup(span->tags, name);
355-
if (strstarts(value, "\"")
356-
&& strlen(value) > 1
357-
&& strends(value, "\"")) {
358-
value = tal_strndup(tmpctx, value + 1,
359-
strlen(value) - 2);
360+
for (size_t i = 0; i < SPAN_MAX_TAGS; i++) {
361+
struct span_tag *t = &span->tags[i];
362+
if (!t->name) {
363+
t->name = name;
364+
t->valuestr = value;
365+
t->valuelen = strlen(value);
366+
if (t->valuestr[0] == '"'
367+
&& t->valuelen > 1
368+
&& t->valuestr[t->valuelen-1] == '"') {
369+
t->valuestr++;
370+
t->valuelen--;
371+
}
372+
return;
373+
}
360374
}
361-
span->tags[s].value = tal_strdup(span->tags, value);
375+
abort();
362376
}
363377

364378
void trace_span_suspend_(const void *key, const char *lbl)

0 commit comments

Comments
 (0)