Skip to content

Commit c371b6c

Browse files
authored
feat: improve profiler performance (#332)
1 parent f0daf24 commit c371b6c

File tree

9 files changed

+26128
-368
lines changed

9 files changed

+26128
-368
lines changed

.clang-format-ignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ NativeScript/inspector/third_party/*
99
NativeScript/inspector/third_party/*/*
1010
NativeScript/inspector/third_party/*/*/*
1111
NativeScript/inspector/third_party/*/*/*/*
12+
NativeScript/third_party/*
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
#ifndef InspectorServer_h
22
#define InspectorServer_h
33

4+
#include <dispatch/dispatch.h>
5+
#include <sys/types.h>
6+
47
#include <functional>
58
#include <string>
6-
#include <sys/types.h>
7-
#include <dispatch/dispatch.h>
89

910
namespace v8_inspector {
1011

1112
class InspectorServer {
12-
public:
13-
static in_port_t Init(std::function<void (std::function<void (std::string)>)> onClientConnected, std::function<void (std::string)> onMessage);
14-
private:
15-
static void Send(dispatch_io_t channel, dispatch_queue_t queue, std::string message);
13+
public:
14+
static in_port_t Init(
15+
std::function<void(std::function<void(const std::string&)>)>
16+
onClientConnected,
17+
std::function<void(const std::string&)> onMessage);
18+
19+
private:
20+
static void Send(dispatch_io_t channel, dispatch_queue_t queue,
21+
const std::string& message);
1622
};
1723

18-
}
24+
} // namespace v8_inspector
1925

2026
#endif /* InspectorServer_h */

NativeScript/inspector/InspectorServer.mm

Lines changed: 122 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -5,143 +5,147 @@
55

66
namespace v8_inspector {
77

8-
in_port_t InspectorServer::Init(std::function<void (std::function<void (std::string)>)> onClientConnected, std::function<void (std::string)> onMessage) {
9-
in_port_t listenPort = 18183;
8+
in_port_t InspectorServer::Init(
9+
std::function<void(std::function<void(const std::string&)>)> onClientConnected,
10+
std::function<void(const std::string&)> onMessage) {
11+
in_port_t listenPort = 18183;
12+
13+
int serverSocket = -1;
14+
if ((serverSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
15+
assert(false);
16+
}
17+
18+
struct sockaddr_in serverAddress;
19+
memset(&serverAddress, 0, sizeof(serverAddress));
20+
serverAddress.sin_family = AF_INET;
21+
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
22+
do {
23+
serverAddress.sin_port = htons(listenPort);
24+
} while (bind(serverSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) < 0 &&
25+
++listenPort);
26+
27+
// Make the socket non-blocking
28+
if (fcntl(serverSocket, F_SETFL, O_NONBLOCK) < 0) {
29+
shutdown(serverSocket, SHUT_RDWR);
30+
close(serverSocket);
31+
assert(false);
32+
}
33+
34+
// Set up the dispatch source that will alert us to new incoming connections
35+
dispatch_queue_t q = dispatch_queue_create("server_queue", DISPATCH_QUEUE_CONCURRENT);
36+
dispatch_source_t acceptSource =
37+
dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, serverSocket, 0, q);
38+
dispatch_source_set_event_handler(acceptSource, ^{
39+
const unsigned long numPendingConnections = dispatch_source_get_data(acceptSource);
40+
for (unsigned long i = 0; i < numPendingConnections; i++) {
41+
int clientSocket = -1;
42+
struct sockaddr_in echoClntAddr;
43+
unsigned int clntLen = sizeof(echoClntAddr);
44+
45+
// Wait for a client to connect
46+
if ((clientSocket = accept(serverSocket, (struct sockaddr*)&echoClntAddr, &clntLen)) >= 0) {
47+
dispatch_io_t channel =
48+
dispatch_io_create(DISPATCH_IO_STREAM, clientSocket, q, ^(int error) {
49+
if (error) {
50+
NSLog(@"Error: %s", strerror(error));
51+
}
52+
close(clientSocket);
53+
});
54+
55+
onClientConnected([channel, q](std::string message) { Send(channel, q, message); });
56+
57+
__block dispatch_io_handler_t receiver = ^(bool done, dispatch_data_t data, int error) {
58+
if (error) {
59+
NSLog(@"Error: %s", strerror(error));
60+
}
1061

11-
int serverSocket = -1;
12-
if ((serverSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
13-
assert(false);
14-
}
62+
const void* bytes = [(NSData*)data bytes];
63+
if (!bytes) {
64+
return;
65+
}
1566

16-
struct sockaddr_in serverAddress;
17-
memset(&serverAddress, 0, sizeof(serverAddress));
18-
serverAddress.sin_family = AF_INET;
19-
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
20-
do {
21-
serverAddress.sin_port = htons(listenPort);
22-
} while (bind(serverSocket, (struct sockaddr *) &serverAddress, sizeof(serverAddress)) < 0 && ++listenPort);
23-
24-
// Make the socket non-blocking
25-
if (fcntl(serverSocket, F_SETFL, O_NONBLOCK) < 0) {
26-
shutdown(serverSocket, SHUT_RDWR);
27-
close(serverSocket);
28-
assert(false);
29-
}
67+
uint32_t length = ntohl(*(uint32_t*)bytes);
68+
69+
// Configure the channel...
70+
dispatch_io_set_low_water(channel, length);
3071

31-
// Set up the dispatch source that will alert us to new incoming connections
32-
dispatch_queue_t q = dispatch_queue_create("server_queue", DISPATCH_QUEUE_CONCURRENT);
33-
dispatch_source_t acceptSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, serverSocket, 0, q);
34-
dispatch_source_set_event_handler(acceptSource, ^{
35-
const unsigned long numPendingConnections = dispatch_source_get_data(acceptSource);
36-
for (unsigned long i = 0; i < numPendingConnections; i++) {
37-
int clientSocket = -1;
38-
struct sockaddr_in echoClntAddr;
39-
unsigned int clntLen = sizeof(echoClntAddr);
40-
41-
// Wait for a client to connect
42-
if ((clientSocket = accept(serverSocket, (struct sockaddr *) &echoClntAddr, &clntLen)) >= 0) {
43-
dispatch_io_t channel = dispatch_io_create(DISPATCH_IO_STREAM, clientSocket, q, ^(int error) {
44-
if (error) {
45-
NSLog(@"Error: %s", strerror(error));
46-
}
47-
close(clientSocket);
48-
});
49-
50-
onClientConnected([channel, q](std::string message) {
51-
Send(channel, q, message);
52-
});
53-
54-
__block dispatch_io_handler_t receiver = ^(bool done, dispatch_data_t data, int error) {
55-
if (error) {
56-
NSLog(@"Error: %s", strerror(error));
57-
}
58-
59-
const void* bytes = [(NSData*)data bytes];
60-
if (!bytes) {
61-
return;
62-
}
63-
64-
uint32_t length = ntohl(*(uint32_t*)bytes);
65-
66-
// Configure the channel...
67-
dispatch_io_set_low_water(channel, length);
68-
69-
// Setup read handler
70-
dispatch_io_read(channel, 0, length, q, ^(bool done, dispatch_data_t data, int error) {
71-
BOOL close = NO;
72-
if (error) {
73-
NSLog(@"Error: %s", strerror(error));
74-
close = YES;
75-
}
76-
77-
const size_t size = data ? dispatch_data_get_size(data) : 0;
78-
if (size) {
79-
NSString* payload = [[NSString alloc] initWithData:(NSData*)data encoding:NSUTF16LittleEndianStringEncoding];
80-
81-
onMessage([payload UTF8String]);
72+
// Setup read handler
73+
dispatch_io_read(channel, 0, length, q, ^(bool done, dispatch_data_t data, int error) {
74+
BOOL close = NO;
75+
if (error) {
76+
NSLog(@"Error: %s", strerror(error));
77+
close = YES;
78+
}
79+
80+
const size_t size = data ? dispatch_data_get_size(data) : 0;
81+
if (size) {
82+
NSString* payload = [[NSString alloc] initWithData:(NSData*)data
83+
encoding:NSUTF16LittleEndianStringEncoding];
84+
85+
onMessage([payload UTF8String]);
8286

8387
#pragma clang diagnostic push
8488
#pragma clang diagnostic ignored "-Warc-retain-cycles"
85-
dispatch_io_read(channel, 0, 4, q, receiver);
89+
dispatch_io_read(channel, 0, 4, q, receiver);
8690
#pragma clang diagnostic pop
87-
} else {
88-
close = YES;
89-
}
90-
91-
if (close) {
92-
dispatch_io_close(channel, DISPATCH_IO_STOP);
93-
}
94-
});
95-
};
96-
97-
if (channel) {
98-
dispatch_io_read(channel, 0, 4, q, receiver);
99-
}
91+
} else {
92+
close = YES;
10093
}
101-
else {
102-
NSLog(@"accept() failed;\n");
94+
95+
if (close) {
96+
dispatch_io_close(channel, DISPATCH_IO_STOP);
10397
}
98+
});
99+
};
100+
101+
if (channel) {
102+
dispatch_io_read(channel, 0, 4, q, receiver);
104103
}
105-
});
104+
} else {
105+
NSLog(@"accept() failed;\n");
106+
}
107+
}
108+
});
106109

107-
// Resume the source so we're ready to accept once we listen()
108-
dispatch_resume(acceptSource);
110+
// Resume the source so we're ready to accept once we listen()
111+
dispatch_resume(acceptSource);
109112

110-
// Listen() on the socket
111-
if (listen(serverSocket, SOMAXCONN) < 0) {
112-
shutdown(serverSocket, SHUT_RDWR);
113-
close(serverSocket);
114-
assert(false);
115-
}
113+
// Listen() on the socket
114+
if (listen(serverSocket, SOMAXCONN) < 0) {
115+
shutdown(serverSocket, SHUT_RDWR);
116+
close(serverSocket);
117+
assert(false);
118+
}
116119

117-
return listenPort;
120+
return listenPort;
118121
}
119122

120-
void InspectorServer::Send(dispatch_io_t channel, dispatch_queue_t queue, std::string message) {
121-
NSString* str = [NSString stringWithUTF8String:message.c_str()];
122-
NSUInteger length = [str lengthOfBytesUsingEncoding:NSUTF16LittleEndianStringEncoding];
123+
void InspectorServer::Send(dispatch_io_t channel, dispatch_queue_t queue,
124+
const std::string& message) {
125+
NSString* str = [NSString stringWithUTF8String:message.c_str()];
126+
NSUInteger length = [str lengthOfBytesUsingEncoding:NSUTF16LittleEndianStringEncoding];
123127

124-
uint8_t* buffer = (uint8_t*)malloc(length + sizeof(uint32_t));
128+
uint8_t* buffer = (uint8_t*)malloc(length + sizeof(uint32_t));
125129

126-
*(uint32_t*)buffer = htonl(length);
130+
*(uint32_t*)buffer = htonl(length);
127131

128-
[str getBytes:&buffer[sizeof(uint32_t)]
129-
maxLength:length
130-
usedLength:NULL
131-
encoding:NSUTF16LittleEndianStringEncoding
132-
options:0
133-
range:NSMakeRange(0, str.length)
134-
remainingRange:NULL];
132+
[str getBytes:&buffer[sizeof(uint32_t)]
133+
maxLength:length
134+
usedLength:NULL
135+
encoding:NSUTF16LittleEndianStringEncoding
136+
options:0
137+
range:NSMakeRange(0, str.length)
138+
remainingRange:NULL];
135139

136-
dispatch_data_t data = dispatch_data_create(buffer, length + sizeof(uint32_t), queue, ^{
137-
free(buffer);
138-
});
140+
dispatch_data_t data = dispatch_data_create(buffer, length + sizeof(uint32_t), queue, ^{
141+
free(buffer);
142+
});
139143

140-
dispatch_io_write(channel, 0, data, queue, ^(bool done, dispatch_data_t data, int error) {
141-
if (error) {
142-
NSLog(@"Error: %s", strerror(error));
143-
}
144-
});
144+
dispatch_io_write(channel, 0, data, queue, ^(bool done, dispatch_data_t data, int error) {
145+
if (error) {
146+
NSLog(@"Error: %s", strerror(error));
147+
}
148+
});
145149
}
146150

147-
}
151+
} // namespace v8_inspector

0 commit comments

Comments
 (0)