Skip to content

Commit 62b7be6

Browse files
committed
perf: use const strings in inspector messages
1 parent 56ba572 commit 62b7be6

4 files changed

Lines changed: 25 additions & 17 deletions

File tree

NativeScript/inspector/InspectorServer.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ namespace v8_inspector {
1212
class InspectorServer {
1313
public:
1414
static in_port_t Init(
15-
std::function<void(std::function<void(std::string)>)> onClientConnected,
16-
std::function<void(std::string)> onMessage);
15+
std::function<void(std::function<void(const std::string&)>)>
16+
onClientConnected,
17+
std::function<void(const std::string&)> onMessage);
1718

1819
private:
1920
static void Send(dispatch_io_t channel, dispatch_queue_t queue,
20-
std::string message);
21+
const std::string& message);
2122
};
2223

2324
} // namespace v8_inspector

NativeScript/inspector/InspectorServer.mm

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
namespace v8_inspector {
77

88
in_port_t InspectorServer::Init(
9-
std::function<void(std::function<void(std::string)>)> onClientConnected,
10-
std::function<void(std::string)> onMessage) {
9+
std::function<void(std::function<void(const std::string&)>)> onClientConnected,
10+
std::function<void(const std::string&)> onMessage) {
1111
in_port_t listenPort = 18183;
1212

1313
int serverSocket = -1;
@@ -120,7 +120,8 @@
120120
return listenPort;
121121
}
122122

123-
void InspectorServer::Send(dispatch_io_t channel, dispatch_queue_t queue, std::string message) {
123+
void InspectorServer::Send(dispatch_io_t channel, dispatch_queue_t queue,
124+
const std::string& message) {
124125
NSString* str = [NSString stringWithUTF8String:message.c_str()];
125126
NSUInteger length = [str lengthOfBytesUsingEncoding:NSUTF16LittleEndianStringEncoding];
126127

NativeScript/inspector/JsV8InspectorClient.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class JsV8InspectorClient : V8InspectorClient, V8Inspector::Channel {
5555
dispatch_queue_t messagesQueue_;
5656
dispatch_queue_t messageLoopQueue_;
5757
dispatch_semaphore_t messageArrived_;
58-
std::function<void(std::string)> sender_;
58+
std::function<void(const std::string&)> sender_;
5959
bool isWaitingForDebugger_;
6060
bool hasScheduledDebugBreak_;
6161

@@ -68,8 +68,9 @@ class JsV8InspectorClient : V8InspectorClient, V8Inspector::Channel {
6868
void enableInspector(int argc, char** argv);
6969
void createInspectorSession();
7070
void notify(std::unique_ptr<StringBuffer> message);
71-
void onFrontendConnected(std::function<void(std::string)> sender);
72-
void onFrontendMessageReceived(std::string message);
71+
void notify(const std::string& message);
72+
void onFrontendConnected(std::function<void(const std::string&)> sender);
73+
void onFrontendMessageReceived(const std::string& message);
7374
std::string PumpMessage();
7475
static void registerDomainDispatcherCallback(
7576
const v8::FunctionCallbackInfo<v8::Value>& args);

NativeScript/inspector/JsV8InspectorClient.mm

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@
6666
NOTIFICATION("AttachRequest"), &attachRequestSubscription, dispatch_get_main_queue(),
6767
^(int token) {
6868
in_port_t listenPort = InspectorServer::Init(
69-
[this](std::function<void(std::string)> sender) { this->onFrontendConnected(sender); },
70-
[this](std::string message) { this->onFrontendMessageReceived(message); });
69+
[this](std::function<void(const std::string&)> sender) {
70+
this->onFrontendConnected(sender);
71+
},
72+
[this](const std::string& message) { this->onFrontendMessageReceived(message); });
7173

7274
LOG_DEBUGGER_PORT(listenPort);
7375
notify_post(NOTIFICATION("ReadyForAttach"));
@@ -99,7 +101,7 @@
99101
notify_cancel(waitForDebuggerSubscription);
100102
}
101103

102-
void JsV8InspectorClient::onFrontendConnected(std::function<void(std::string)> sender) {
104+
void JsV8InspectorClient::onFrontendConnected(std::function<void(const std::string&)> sender) {
103105
if (this->isWaitingForDebugger_) {
104106
this->isWaitingForDebugger_ = NO;
105107
CFRunLoopRef runloop = CFRunLoopGetMain();
@@ -118,7 +120,7 @@
118120
this->isConnected_ = true;
119121
}
120122

121-
void JsV8InspectorClient::onFrontendMessageReceived(std::string message) {
123+
void JsV8InspectorClient::onFrontendMessageReceived(const std::string& message) {
122124
dispatch_sync(this->messagesQueue_, ^{
123125
this->messages_.push(message);
124126
dispatch_semaphore_signal(messageArrived_);
@@ -243,10 +245,12 @@
243245

244246
void JsV8InspectorClient::notify(std::unique_ptr<StringBuffer> message) {
245247
StringView stringView = message->string();
246-
std::string value = ToStdString(stringView);
248+
notify(ToStdString(stringView));
249+
}
247250

251+
void JsV8InspectorClient::notify(const std::string& message) {
248252
if (this->sender_) {
249-
this->sender_(value);
253+
this->sender_(message);
250254
}
251255
}
252256

@@ -280,8 +284,9 @@
280284

281285
if (message.find("Tracing.end") != std::string::npos) {
282286
tracing_agent_->end();
283-
std::string res = tracing_agent_->getLastTrace();
284-
tracing_agent_->SendToDevtools(context, res);
287+
for (const auto& traceMessage : tracing_agent_->getLastTrace()) {
288+
notify(traceMessage);
289+
}
285290
return;
286291
}
287292

0 commit comments

Comments
 (0)