From 1d589a140a690cb6f7acbbea3e3376cb8019ee95 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Thu, 18 Apr 2024 11:40:30 -0700 Subject: [PATCH] Improve new IoOwn logging Avoid allocating the new string on every dereference --- src/workerd/io/io-context.c++ | 8 ++++---- src/workerd/io/io-context.h | 5 +++-- src/workerd/io/io-own.c++ | 2 +- src/workerd/io/io-own.h | 11 ++++------- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/workerd/io/io-context.c++ b/src/workerd/io/io-context.c++ index cfcee708b52..7f0a4a02f5d 100644 --- a/src/workerd/io/io-context.c++ +++ b/src/workerd/io/io-context.c++ @@ -956,7 +956,7 @@ void IoContext::requireCurrent() { KJ_REQUIRE(threadLocalRequest == this, "request is not current in this thread"); } -void IoContext::checkFarGet(const DeleteQueue* expectedQueue, kj::StringPtr type) { +void IoContext::checkFarGet(const DeleteQueue* expectedQueue, const std::type_info& type) { KJ_ASSERT(expectedQueue); requireCurrent(); @@ -1273,9 +1273,9 @@ void IoContext::requireCurrentOrThrowJs(WeakRef& weak) { throwNotCurrentJsError(); } -void IoContext::throwNotCurrentJsError(kj::Maybe maybeType) { - auto type = maybeType.map([](kj::StringPtr type) { - return kj::str(" (I/O type: ", type, ")"); +void IoContext::throwNotCurrentJsError(kj::Maybe maybeType) { + auto type = maybeType.map([](const std::type_info& type) { + return kj::str(" (I/O type: ", jsg::typeName(type), ")"); }).orDefault(kj::String()); if (threadLocalRequest != nullptr && threadLocalRequest->actor != kj::none) { diff --git a/src/workerd/io/io-context.h b/src/workerd/io/io-context.h index 2c5b7affd77..9b25e1f7f09 100644 --- a/src/workerd/io/io-context.h +++ b/src/workerd/io/io-context.h @@ -370,7 +370,8 @@ class IoContext final: public kj::Refcounted, private kj::TaskSet::ErrorHandler static void requireCurrentOrThrowJs(WeakRef& weak); // Just throw the error that requireCurrentOrThrowJs() would throw on failure. - [[noreturn]] static void throwNotCurrentJsError(kj::Maybe maybeType = kj::none); + [[noreturn]] static void throwNotCurrentJsError( + kj::Maybe maybeType = kj::none); // ----------------------------------------------------------------- // Task scheduling and object storage @@ -811,7 +812,7 @@ class IoContext final: public kj::Refcounted, private kj::TaskSet::ErrorHandler void taskFailed(kj::Exception&& exception) override; void requireCurrent(); - void checkFarGet(const DeleteQueue* expectedQueue, kj::StringPtr type); + void checkFarGet(const DeleteQueue* expectedQueue, const std::type_info& type); kj::Maybe> promiseContextTag; diff --git a/src/workerd/io/io-own.c++ b/src/workerd/io/io-own.c++ index ec4faca1ece..38b8f3e694b 100644 --- a/src/workerd/io/io-own.c++ +++ b/src/workerd/io/io-own.c++ @@ -32,7 +32,7 @@ void DeleteQueue::scheduleDeletion(OwnedObject* object) const { } } -void DeleteQueue::checkFarGet(const DeleteQueue* deleteQueue, kj::StringPtr type) { +void DeleteQueue::checkFarGet(const DeleteQueue* deleteQueue, const std::type_info& type) { IoContext::current().checkFarGet(deleteQueue, type); } diff --git a/src/workerd/io/io-own.h b/src/workerd/io/io-own.h index 16a887fb504..f0acaf08c29 100644 --- a/src/workerd/io/io-own.h +++ b/src/workerd/io/io-own.h @@ -120,7 +120,7 @@ class DeleteQueue: public kj::AtomicRefcounted { // Implements the corresponding methods of IoContext and ActorContext. template IoOwn addObject(kj::Own obj, OwnedObjectList& ownedObjects); - static void checkFarGet(const DeleteQueue* deleteQueue, kj::StringPtr type); + static void checkFarGet(const DeleteQueue* deleteQueue, const std::type_info& type); }; template @@ -288,15 +288,13 @@ IoPtr& IoPtr::operator=(decltype(nullptr)) { template inline T* IoOwn::operator->() { - auto type = jsg::typeName(typeid(T)); - DeleteQueue::checkFarGet(deleteQueue, type); + DeleteQueue::checkFarGet(deleteQueue, typeid(T)); return item->ptr; } template inline IoOwn::operator kj::Own() && { - auto type = jsg::typeName(typeid(T)); - DeleteQueue::checkFarGet(deleteQueue, type); + DeleteQueue::checkFarGet(deleteQueue, typeid(T)); auto result = kj::mv(item->ptr); OwnedObjectList::unlink(*item); item = nullptr; @@ -306,8 +304,7 @@ inline IoOwn::operator kj::Own() && { template inline T* IoPtr::operator->() { - auto type = jsg::typeName(typeid(T)); - DeleteQueue::checkFarGet(deleteQueue, type); + DeleteQueue::checkFarGet(deleteQueue, typeid(T)); return ptr; }