Skip to content

Commit 276ef19

Browse files
committed
[nfc] Clean up string allocations
1 parent dfcde42 commit 276ef19

File tree

10 files changed

+51
-56
lines changed

10 files changed

+51
-56
lines changed

src/workerd/api/actor-state.c++

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ class FacetOutgoingFactory final: public Fetcher::OutgoingFactory {
929929
return context.getMetrics().wrapActorSubrequestClient(context.getSubrequest(
930930
[&](TraceContext& tracing, IoChannelFactory& ioChannelFactory) {
931931
if (tracing.span.isObserved()) {
932-
tracing.span.setTag("facet_name"_kjc, kj::str(name));
932+
tracing.span.setTag("facet_name"_kjc, name.asPtr());
933933
}
934934

935935
// Lazily initialize actorChannel

src/workerd/api/actor.c++

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ kj::Own<WorkerInterface> LocalActorOutgoingFactory::newSingleUseClient(
2222
return context.getMetrics().wrapActorSubrequestClient(context.getSubrequest(
2323
[&](TraceContext& tracing, IoChannelFactory& ioChannelFactory) {
2424
if (tracing.span.isObserved()) {
25-
tracing.span.setTag("actor_id"_kjc, kj::str(actorId));
25+
tracing.span.setTag("actor_id"_kjc, actorId.asPtr());
2626
}
2727

2828
// Lazily initialize actorChannel
@@ -77,7 +77,7 @@ kj::Own<WorkerInterface> ReplicaActorOutgoingFactory::newSingleUseClient(
7777
return context.getMetrics().wrapActorSubrequestClient(context.getSubrequest(
7878
[&](TraceContext& tracing, IoChannelFactory& ioChannelFactory) {
7979
if (tracing.span.isObserved()) {
80-
tracing.span.setTag("actor_id"_kjc, kj::heapString(actorId));
80+
tracing.span.setTag("actor_id"_kjc, actorId.asPtr());
8181
}
8282

8383
// Unlike in `GlobalActorOutgoingFactory`, we do not create this lazily, since our channel was

src/workerd/api/cache.c++

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -537,44 +537,42 @@ kj::Own<kj::HttpClient> Cache::getHttpClient(IoContext& context,
537537
TraceContext traceContext(
538538
context.makeTraceSpan(operationName), context.makeUserTraceSpan(operationName));
539539

540-
traceContext.setTag("url.full"_kjc, kj::str(url));
540+
traceContext.setTag("url.full"_kjc, url);
541541
// TODO(o11y): Can we parse cacheControl more cleanly? For example, if tags are duplicated in the
542542
// same category we should choose the last value. We should also support these tags for
543543
// cache_match (where we should pull them from the returned response and need to keep the span
544544
// alive until then).
545545
KJ_IF_SOME(c, cacheControl) {
546546
// cacheability
547547
if (c.contains("no-store")) {
548-
traceContext.setTag("cache_control.cacheability"_kjc, kj::str("no-store"));
548+
traceContext.setTag("cache_control.cacheability"_kjc, "no-store"_kjc);
549549
} else if (c.contains("private")) {
550-
traceContext.setTag("cache_control.cacheability"_kjc, kj::str("private"));
550+
traceContext.setTag("cache_control.cacheability"_kjc, "private"_kjc);
551551
} else if (c.contains("public")) {
552-
traceContext.setTag("cache_control.cacheability"_kjc, kj::str("public"));
552+
traceContext.setTag("cache_control.cacheability"_kjc, "public"_kjc);
553553
}
554554

555555
// expiration
556556
if (c.contains("no-cache")) {
557-
traceContext.setTag("cache_control.expiration"_kjc, kj::str("no-cache"));
557+
traceContext.setTag("cache_control.expiration"_kjc, "no-cache"_kjc);
558558
} else KJ_IF_SOME(idx, c.find("max-age="_kj)) {
559559
auto maybeNum = c.slice(idx + "max-age="_kj.size()).tryParseAs<double>();
560560
KJ_IF_SOME(num, maybeNum) {
561-
traceContext.setTag(
562-
"cache_control.expiration"_kjc, kj::str(kj::str("max-age="), kj::str(num)));
561+
traceContext.setTag("cache_control.expiration"_kjc, kj::str("max-age="_kjc, num));
563562
}
564563
} else KJ_IF_SOME(idx, c.find("s-maxage="_kj)) {
565564
auto maybeNum = c.slice(idx + "s-maxage="_kj.size()).tryParseAs<double>();
566565
KJ_IF_SOME(num, maybeNum) {
567-
traceContext.setTag(
568-
"cache_control.expiration"_kjc, kj::str(kj::str("s-maxage="), kj::str(num)));
566+
traceContext.setTag("cache_control.expiration"_kjc, kj::str("s-maxage="_kjc, num));
569567
}
570568
}
571569

572570
// revalidation. Note: There are also stale-while-revalidate and stale-if-error directives, but
573571
// they are ignored by the Workers Cache API and we do not set them as tags accordingly.
574572
if (c.contains("must-revalidate")) {
575-
traceContext.setTag("cache_control.revalidation"_kjc, kj::str("must-revalidate"));
573+
traceContext.setTag("cache_control.revalidation"_kjc, "must-revalidate"_kjc);
576574
} else if (c.contains("proxy-revalidate")) {
577-
traceContext.setTag("cache_control.revalidation"_kjc, kj::str("proxy-revalidate"));
575+
traceContext.setTag("cache_control.revalidation"_kjc, "proxy-revalidate"_kjc);
578576
}
579577
}
580578
auto cacheClient = context.getCacheClient();

src/workerd/api/eventsource.c++

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ void EventSource::notifyMessages(jsg::Lock& js, kj::Array<PendingMessage> messag
307307
for (auto& message: messages) {
308308
auto data = kj::str(kj::delimited(kj::mv(message.data), "\n"_kjc));
309309
if (data.size() == 0) continue;
310-
kj::String type = kj::mv(message.event).orDefault(kj::str("message"));
310+
kj::String type = kj::mv(message.event).orDefault([]() { return kj::str("message"); });
311311
dispatchEventImpl(js,
312312
js.alloc<MessageEvent>(js, kj::mv(type), js.str(data), kj::mv(message.id),
313313
kj::none /** source **/, impl.map([](FetchImpl& i) -> jsg::Url& { return i.url; })));

src/workerd/api/kv.c++

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,10 @@ kj::OneOf<jsg::Promise<KvNamespace::GetResult>, jsg::Promise<jsg::JsRef<jsg::JsM
266266
auto traceSpan = context.makeTraceSpan("kv_get"_kjc);
267267
auto userSpan = context.makeUserTraceSpan("kv_get"_kjc);
268268
TraceContext traceContext(kj::mv(traceSpan), kj::mv(userSpan));
269-
traceContext.userSpan.setTag("db.system.name"_kjc, kj::str("cloudflare-kv"_kjc));
270-
traceContext.userSpan.setTag("db.operation.name"_kjc, kj::str("get"_kjc));
271-
traceContext.userSpan.setTag("cloudflare.binding.name"_kjc, kj::str(bindingName));
272-
traceContext.userSpan.setTag("cloudflare.binding.type"_kjc, kj::str("KV"_kjc));
269+
traceContext.userSpan.setTag("db.system.name"_kjc, "cloudflare-kv"_kjc);
270+
traceContext.userSpan.setTag("db.operation.name"_kjc, "get"_kjc);
271+
traceContext.userSpan.setTag("cloudflare.binding.name"_kjc, bindingName.asPtr());
272+
traceContext.userSpan.setTag("cloudflare.binding.type"_kjc, "KV"_kjc);
273273

274274
KJ_SWITCH_ONEOF(name) {
275275
KJ_CASE_ONEOF(arr, kj::Array<kj::String>) {
@@ -303,10 +303,10 @@ KvNamespace::getWithMetadata(jsg::Lock& js,
303303
auto traceSpan = context.makeTraceSpan("kv_getWithMetadata"_kjc);
304304
auto userSpan = context.makeUserTraceSpan("kv_getWithMetadata"_kjc);
305305
TraceContext traceContext(kj::mv(traceSpan), kj::mv(userSpan));
306-
traceContext.userSpan.setTag("db.system.name"_kjc, kj::str("cloudflare-kv"_kjc));
307-
traceContext.userSpan.setTag("db.operation.name"_kjc, kj::str("get"_kjc));
308-
traceContext.userSpan.setTag("cloudflare.binding.name"_kjc, kj::str(bindingName));
309-
traceContext.userSpan.setTag("cloudflare.binding.type"_kjc, kj::str("KV"_kjc));
306+
traceContext.userSpan.setTag("db.system.name"_kjc, "cloudflare-kv"_kjc);
307+
traceContext.userSpan.setTag("db.operation.name"_kjc, "get"_kjc);
308+
traceContext.userSpan.setTag("cloudflare.binding.name"_kjc, bindingName.asPtr());
309+
traceContext.userSpan.setTag("cloudflare.binding.type"_kjc, "KV"_kjc);
310310
KJ_SWITCH_ONEOF(name) {
311311
KJ_CASE_ONEOF(arr, kj::Array<kj::String>) {
312312
return context.attachSpans(js,
@@ -330,7 +330,7 @@ jsg::Promise<KvNamespace::GetWithMetadataResult> KvNamespace::getWithMetadataImp
330330
LimitEnforcer::KvOpType op) {
331331
validateKeyName("GET", name);
332332

333-
traceContext.userSpan.setTag("cloudflare.kv.query.keys"_kjc, kj::str(name));
333+
traceContext.userSpan.setTag("cloudflare.kv.query.keys"_kjc, name.asPtr());
334334
traceContext.userSpan.setTag("cloudflare.kv.query.keys.count"_kjc, static_cast<int64_t>(1));
335335

336336
kj::Url url;
@@ -372,7 +372,7 @@ jsg::Promise<KvNamespace::GetWithMetadataResult> KvNamespace::getWithMetadataImp
372372
-> jsg::Promise<KvNamespace::GetWithMetadataResult> {
373373
auto cacheStatus =
374374
response.headers->get(context.getHeaderIds().cfCacheStatus).map([&](kj::StringPtr cs) {
375-
traceContext.userSpan.setTag("cloudflare.kv.response.cache_status"_kjc, kj::str(cs));
375+
traceContext.userSpan.setTag("cloudflare.kv.response.cache_status"_kjc, cs);
376376
return jsg::JsRef<jsg::JsValue>(js, js.strIntern(cs));
377377
});
378378

@@ -463,10 +463,10 @@ jsg::Promise<jsg::JsRef<jsg::JsValue>> KvNamespace::list(
463463
auto userSpan = context.makeUserTraceSpan("kv_list"_kjc);
464464
TraceContext traceContext(kj::mv(traceSpan), kj::mv(userSpan));
465465

466-
traceContext.userSpan.setTag("db.system.name"_kjc, kj::str("cloudflare-kv"_kjc));
467-
traceContext.userSpan.setTag("db.operation.name"_kjc, kj::str("list"_kjc));
468-
traceContext.userSpan.setTag("cloudflare.binding.name"_kjc, kj::str(bindingName));
469-
traceContext.userSpan.setTag("cloudflare.binding.type"_kjc, kj::str("KV"_kjc));
466+
traceContext.userSpan.setTag("db.system.name"_kjc, "cloudflare-kv"_kjc);
467+
traceContext.userSpan.setTag("db.operation.name"_kjc, "list"_kjc);
468+
traceContext.userSpan.setTag("cloudflare.binding.name"_kjc, bindingName.asPtr());
469+
traceContext.userSpan.setTag("cloudflare.binding.type"_kjc, "KV"_kjc);
470470

471471
kj::Url url;
472472
url.scheme = kj::str("https");
@@ -480,13 +480,13 @@ jsg::Promise<jsg::JsRef<jsg::JsValue>> KvNamespace::list(
480480
}
481481
KJ_IF_SOME(maybePrefix, o.prefix) {
482482
KJ_IF_SOME(prefix, maybePrefix) {
483-
traceContext.userSpan.setTag("cloudflare.kv.query.prefix"_kjc, kj::str(prefix));
483+
traceContext.userSpan.setTag("cloudflare.kv.query.prefix"_kjc, prefix.asPtr());
484484
url.query.add(kj::Url::QueryParam{kj::str("prefix"), kj::str(prefix)});
485485
}
486486
}
487487
KJ_IF_SOME(maybeCursor, o.cursor) {
488488
KJ_IF_SOME(cursor, maybeCursor) {
489-
traceContext.userSpan.setTag("cloudflare.kv.query.cursor"_kjc, kj::str(cursor));
489+
traceContext.userSpan.setTag("cloudflare.kv.query.cursor"_kjc, cursor.asPtr());
490490
url.query.add(kj::Url::QueryParam{kj::str("cursor"), kj::str(cursor)});
491491
}
492492
}
@@ -552,11 +552,11 @@ jsg::Promise<void> KvNamespace::put(jsg::Lock& js,
552552
auto userSpan = context.makeUserTraceSpan("kv_put"_kjc);
553553
TraceContext traceContext(kj::mv(traceSpan), kj::mv(userSpan));
554554

555-
traceContext.userSpan.setTag("db.system.name"_kjc, kj::str("cloudflare-kv"_kjc));
556-
traceContext.userSpan.setTag("db.operation.name"_kjc, kj::str("put"_kjc));
557-
traceContext.userSpan.setTag("cloudflare.binding.name"_kjc, kj::str(bindingName));
558-
traceContext.userSpan.setTag("cloudflare.binding.type"_kjc, kj::str("KV"_kjc));
559-
traceContext.userSpan.setTag("cloudflare.kv.query.keys"_kjc, kj::str(name));
555+
traceContext.userSpan.setTag("db.system.name"_kjc, "cloudflare-kv"_kjc);
556+
traceContext.userSpan.setTag("db.operation.name"_kjc, "put"_kjc);
557+
traceContext.userSpan.setTag("cloudflare.binding.name"_kjc, bindingName.asPtr());
558+
traceContext.userSpan.setTag("cloudflare.binding.type"_kjc, "KV"_kjc);
559+
traceContext.userSpan.setTag("cloudflare.kv.query.keys"_kjc, name.asPtr());
560560
traceContext.userSpan.setTag("cloudflare.kv.query.keys.count"_kjc, static_cast<int64_t>(1));
561561

562562
kj::Url url;
@@ -612,16 +612,15 @@ jsg::Promise<void> KvNamespace::put(jsg::Lock& js,
612612
KJ_CASE_ONEOF(text, kj::String) {
613613
headers.setPtr(kj::HttpHeaderId::CONTENT_TYPE, MimeType::PLAINTEXT_STRING);
614614
expectedBodySize = static_cast<uint64_t>(text.size());
615-
traceContext.userSpan.setTag("cloudflare.kv.query.value_type"_kjc, kj::str("text"));
615+
traceContext.userSpan.setTag("cloudflare.kv.query.value_type"_kjc, "text"_kjc);
616616
}
617617
KJ_CASE_ONEOF(data, kj::Array<byte>) {
618618
expectedBodySize = static_cast<uint64_t>(data.size());
619-
traceContext.userSpan.setTag("cloudflare.kv.query.value_type"_kjc, kj::str("ArrayBuffer"));
619+
traceContext.userSpan.setTag("cloudflare.kv.query.value_type"_kjc, "ArrayBuffer"_kjc);
620620
}
621621
KJ_CASE_ONEOF(stream, jsg::Ref<ReadableStream>) {
622622
expectedBodySize = stream->tryGetLength(StreamEncoding::IDENTITY);
623-
traceContext.userSpan.setTag(
624-
"cloudflare.kv.query.value_type"_kjc, kj::str("ReadableStream"));
623+
traceContext.userSpan.setTag("cloudflare.kv.query.value_type"_kjc, "ReadableStream"_kjc);
625624
}
626625
}
627626

@@ -688,11 +687,11 @@ jsg::Promise<void> KvNamespace::delete_(jsg::Lock& js, kj::String name) {
688687
auto userSpan = context.makeUserTraceSpan("kv_delete"_kjc);
689688
TraceContext traceContext(kj::mv(traceSpan), kj::mv(userSpan));
690689

691-
traceContext.userSpan.setTag("db.system.name"_kjc, kj::str("cloudflare-kv"_kjc));
692-
traceContext.userSpan.setTag("db.operation.name"_kjc, kj::str("delete"_kjc));
693-
traceContext.userSpan.setTag("cloudflare.binding.name"_kjc, kj::str(bindingName));
694-
traceContext.userSpan.setTag("cloudflare.binding.type"_kjc, kj::str("KV"_kjc));
695-
traceContext.userSpan.setTag("cloudflare.kv.query.keys"_kjc, kj::str(name));
690+
traceContext.userSpan.setTag("db.system.name"_kjc, "cloudflare-kv"_kjc);
691+
traceContext.userSpan.setTag("db.operation.name"_kjc, "delete"_kjc);
692+
traceContext.userSpan.setTag("cloudflare.binding.name"_kjc, bindingName.asPtr());
693+
traceContext.userSpan.setTag("cloudflare.binding.type"_kjc, "KV"_kjc);
694+
traceContext.userSpan.setTag("cloudflare.kv.query.keys"_kjc, name.asPtr());
696695
traceContext.userSpan.setTag("cloudflare.kv.query.keys.count"_kjc, static_cast<int64_t>(1));
697696

698697
auto urlStr = kj::str("https://fake-host/", kj::encodeUriComponent(name), "?urlencoded=true");

src/workerd/api/memory-cache.c++

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ void SharedMemoryCache::putWhileLocked(ThreadUnsafeData& data,
135135
size_t valueSize = value->bytes.size();
136136

137137
auto writeSpan = IoContext::current().makeTraceSpan("memory_cache_write"_kjc);
138-
writeSpan.setTag("key"_kjc, kj::str(key));
138+
writeSpan.setTag("key"_kjc, key.asPtr());
139139
writeSpan.setTag("value_size"_kjc, static_cast<double>(valueSize));
140140
writeSpan.setTag("has_expiration"_kjc, expiration != kj::none);
141141

src/workerd/api/streams/internal.c++

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace {
2525
if (IoContext::hasCurrent()) {
2626
auto& context = IoContext::current();
2727
if (context.isInspectorEnabled()) {
28-
context.logWarning(kj::str(message));
28+
context.logWarning(message);
2929
}
3030
}
3131

src/workerd/api/worker-rpc.c++

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -703,12 +703,12 @@ JsRpcStub::~JsRpcStub() noexcept(false) {
703703
// IoContext tear-down is problematic since logWarningOnce() is a method on
704704
// IoContext...
705705
if (IoContext::hasCurrent()) {
706-
IoContext::current().logWarningOnce(kj::str(
706+
IoContext::current().logWarningOnce(
707707
"An RPC stub was not disposed properly. You must call dispose() on all stubs in order to "
708708
"let the other side know that you are no longer using them. You cannot rely on "
709709
"the garbage collector for this because it may take arbitrarily long before actually "
710710
"collecting unreachable objects. As a shortcut, calling dispose() on the result of "
711-
"an RPC call disposes all stubs within it."));
711+
"an RPC call disposes all stubs within it."_kj);
712712
}
713713
}
714714
}
@@ -738,11 +738,11 @@ RpcStubDisposalGroup::~RpcStubDisposalGroup() noexcept(false) {
738738
//
739739
// TODO(cleanup): Same comment as in ~JsRpcStub().
740740
if (IoContext::hasCurrent()) {
741-
IoContext::current().logWarningOnce(kj::str(
741+
IoContext::current().logWarningOnce(
742742
"An RPC result was not disposed properly. One of the RPC calls you made expects you "
743743
"to call dispose() on the return value, but you didn't do so. You cannot rely on "
744744
"the garbage collector for this because it may take arbitrarily long before actually "
745-
"collecting unreachable objects."));
745+
"collecting unreachable objects."_kj);
746746
}
747747
}
748748
} else {

src/workerd/io/io-own.c++

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,8 @@ void DeleteQueue::checkFarGet(const DeleteQueue& deleteQueue, const std::type_in
5656
}
5757

5858
void DeleteQueue::checkWeakGet(workerd::WeakRef<IoContext>& weak) {
59-
if (!weak.isValid()) {
60-
JSG_FAIL_REQUIRE(
61-
Error, kj::str("Couldn't complete operation because the execution context has ended."));
62-
}
59+
JSG_REQUIRE(weak.isValid(), Error,
60+
"Couldn't complete operation because the execution context has ended.");
6361
}
6462

6563
kj::Promise<void> DeleteQueue::resetCrossThreadSignal() const {

src/workerd/io/trace.c++

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ Attribute Attribute::clone() const {
929929
}
930930

931931
kj::String Attribute::toString() const {
932-
return kj::str("Attribute: ", name, ", ", kj::str(value));
932+
return kj::str("Attribute: ", name, ", ", value);
933933
}
934934

935935
Return::Return(kj::Maybe<FetchResponseInfo> info): info(kj::mv(info)) {}

0 commit comments

Comments
 (0)