Skip to content

Commit 0b84f58

Browse files
committed
[nfc] Elide more STW memory allocations, simplify code
This removes a memory allocation with each log message accepted by the STW.
1 parent 3e240f7 commit 0b84f58

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

src/workerd/api/trace.c++

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,6 @@ jsg::Optional<kj::Array<kj::String>> getTraceScriptTags(const Trace& trace) {
111111
}
112112
}
113113

114-
template <typename Enum>
115-
kj::String enumToStr(const Enum& var) {
116-
// TODO(cleanup): Port this to capnproto.
117-
auto enums = capnp::Schema::from<Enum>().getEnumerants();
118-
uint i = static_cast<uint>(var);
119-
KJ_ASSERT(i < enums.size(), "invalid enum value");
120-
return kj::str(enums[i].getProto().getName());
121-
}
122-
123114
kj::Own<TraceItem::FetchEventInfo::Request::Detail> getFetchRequestDetail(
124115
jsg::Lock& js, const Trace& trace, const tracing::FetchEventInfo& eventInfo) {
125116
const auto getCf = [&]() -> jsg::Optional<jsg::V8Ref<v8::Object>> {
@@ -201,8 +192,8 @@ TraceItem::TraceItem(jsg::Lock& js, const Trace& trace)
201192
dispatchNamespace(mapCopyString(trace.dispatchNamespace)),
202193
scriptTags(getTraceScriptTags(trace)),
203194
durableObjectId(mapCopyString(trace.durableObjectId)),
204-
executionModel(enumToStr(trace.executionModel)),
205-
outcome(enumToStr(trace.outcome)),
195+
executionModel(kj::str(trace.executionModel)),
196+
outcome(kj::str(trace.outcome)),
206197
cpuTime(trace.cpuTime / kj::MILLISECONDS),
207198
wallTime(trace.wallTime / kj::MILLISECONDS),
208199
truncated(trace.truncated) {}

src/workerd/io/trace-stream.c++

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ namespace {
2828
V(CRON, "cron") \
2929
V(CUSTOM, "custom") \
3030
V(DAEMONDOWN, "daemonDown") \
31+
V(DEBUG, "debug") \
3132
V(DIAGNOSTICCHANNEL, "diagnosticChannel") \
3233
V(DISPATCHNAMESPACE, "dispatchNamespace") \
3334
V(EMAIL, "email") \
@@ -85,6 +86,7 @@ namespace {
8586
V(URL, "url") \
8687
V(VALUE, "value") \
8788
V(WALLTIME, "wallTime") \
89+
V(WARN, "warn") \
8890
V(WASCLEAN, "wasClean")
8991

9092
#define V(N, L) constexpr kj::LiteralStringConst N##_STR = L##_kjc;
@@ -318,19 +320,10 @@ jsg::JsValue ToJs(jsg::Lock& js, const EventOutcome& outcome, StringCache& cache
318320
KJ_UNREACHABLE;
319321
}
320322

321-
template <typename Enum>
322-
kj::String enumToStr(const Enum& var) {
323-
// TODO(cleanup): Port this to capnproto.
324-
auto enums = capnp::Schema::from<Enum>().getEnumerants();
325-
uint i = static_cast<uint>(var);
326-
KJ_ASSERT(i < enums.size(), "invalid enum value");
327-
return kj::str(enums[i].getProto().getName());
328-
}
329-
330323
jsg::JsValue ToJs(jsg::Lock& js, const Onset& onset, StringCache& cache) {
331324
auto obj = js.obj();
332325
obj.set(js, TYPE_STR, cache.get(js, ONSET_STR));
333-
obj.set(js, EXECUTIONMODEL_STR, cache.get(js, enumToStr(onset.workerInfo.executionModel)));
326+
obj.set(js, EXECUTIONMODEL_STR, cache.get(js, kj::str(onset.workerInfo.executionModel)));
334327
obj.set(js, SPANID_STR, js.str(onset.spanId.toGoString()));
335328

336329
KJ_IF_SOME(ns, onset.workerInfo.dispatchNamespace) {
@@ -468,7 +461,19 @@ jsg::JsValue ToJs(jsg::Lock& js, const Exception& ex, StringCache& cache) {
468461
}
469462

470463
jsg::JsValue ToJs(jsg::Lock& js, const LogLevel& level, StringCache& cache) {
471-
return cache.get(js, toLower(enumToStr<LogLevel>(level)));
464+
switch (level) {
465+
case LogLevel::DEBUG_:
466+
return cache.get(js, DEBUG_STR);
467+
case LogLevel::INFO:
468+
return cache.get(js, INFO_STR);
469+
case LogLevel::LOG:
470+
return cache.get(js, LOG_STR);
471+
case LogLevel::WARN:
472+
return cache.get(js, WARN_STR);
473+
case LogLevel::ERROR:
474+
return cache.get(js, ERROR_STR);
475+
}
476+
KJ_UNREACHABLE;
472477
}
473478

474479
jsg::JsValue ToJs(jsg::Lock& js, const Log& log, StringCache& cache) {

0 commit comments

Comments
 (0)