Skip to content

Commit 3c2f921

Browse files
committed
[mod] performance optimization
1 parent 34aa285 commit 3c2f921

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

Diff for: src/scadup/Broker.cpp

+14-9
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ const char* GET_FLAG(G_ScaFlag x) { return (x >= NONE && x < MAX_VAL) ? G_FlagVa
1818

1919
void signalCatch(int value)
2020
{
21-
if (value == SIGSEGV)
22-
return;
21+
if (value == SIGSEGV) {
22+
LOGE("Segmentation fault caught!");
23+
exit(EXIT_FAILURE); // exit
24+
}
2325
LOGI("caught signal: %d", value);
2426
}
2527

@@ -53,7 +55,7 @@ ssize_t Scadup::writes(SOCKET socket, const uint8_t* data, size_t len)
5355
return 0;
5456
if (errno == EPIPE)
5557
return -1;
56-
std::mutex mtxLck = {};
58+
static std::mutex mtxLck; // static lock
5759
std::lock_guard<std::mutex> lock(mtxLck);
5860
auto left = (ssize_t)len;
5961
auto* buff = new(std::nothrow) uint8_t[left];
@@ -269,8 +271,13 @@ int Broker::ProxyTask(Networks& works, const Network& work)
269271
const size_t sz1 = sizeof(Message::Payload::status);
270272
size_t left = work.head.size - HEAD_SIZE;
271273
static Message mval{};
272-
auto* msg = new(mval) Message;
273-
msg->payload.content = new char[left - sz1];
274+
auto* msg = new(mval) Message; // placement new
275+
msg->payload.content = new(std::nothrow) char[left - sz1];
276+
if (msg->payload.content == nullptr) {
277+
LOGE("Payload content allocation failed!");
278+
delete msg;
279+
return -1;
280+
}
274281
memset(msg->payload.content, 0, left - sz1);
275282
size_t len = 0;
276283
size_t size = sz1;
@@ -378,19 +385,17 @@ void Broker::checkAlive(Networks& works, bool* active)
378385
std::vector<Network>& vec = work.second;
379386
for (auto it = vec.begin(); it != vec.end(); ) {
380387
if (!it->active) {
381-
it = vec.erase(it);
382388
LOGI("delete offline client %s:%u", it->IP, it->PORT);
389+
it = vec.erase(it);
383390
} else {
384391
++it;
385392
}
386393
}
387394
}
388395
for (auto it = works.begin(); it != works.end(); ) {
389396
if (it->second.empty()) {
390-
auto next = std::next(it);
391-
works.erase(it);
392-
it = next;
393397
LOGI("works key(%s) is null deleted! now size=%d", GET_FLAG(it->first), works.size());
398+
it = works.erase(it);
394399
} else {
395400
++it;
396401
}

0 commit comments

Comments
 (0)