From 3f4f049ac015c695a965142e7b1180eae83ae781 Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期四, 25 三月 2021 16:18:26 +0800
Subject: [PATCH] reason for speed change; refactor.
---
src/msg.h | 2 +-
src/shm_queue.h | 5 +++--
src/shm_queue.cpp | 7 +++++--
CMakeLists.txt | 3 +--
4 files changed, 10 insertions(+), 7 deletions(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
index cc97bc4..bff076f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -8,8 +8,7 @@
set(CMAKE_CXX_STANDARD_REQUIRED True)
if(CMAKE_GENERATOR MATCHES "Ninja")
- #check_and_add_flag(DIAGNOSTICS_COLOR -fdiagnostics-color)
- set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -fdiagnostics-color=always)
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdiagnostics-color=always")
endif()
# control where the static and shared libraries are built so that on windows
# we don't need to tinker with the path to run the executable
diff --git a/src/msg.h b/src/msg.h
index 8a820bd..ec17aaf 100644
--- a/src/msg.h
+++ b/src/msg.h
@@ -91,7 +91,7 @@
void FreeFrom(SharedMemory &shm);
};
-Msg BuildMsg(const MQId &src, const void *p, const size_t size);
+inline void swap(Msg &m1, Msg &m2) { m1.swap(m2); }
} // namespace bhome_shm
diff --git a/src/shm_queue.cpp b/src/shm_queue.cpp
index 1446446..f770afc 100644
--- a/src/shm_queue.cpp
+++ b/src/shm_queue.cpp
@@ -48,8 +48,6 @@
bool ShmMsgQueue::Send(const MQId &remote_id, const Msg &msg, const int timeout_ms)
{
Queue *remote = find(MsgQIdToName(remote_id));
-
- return remote && remote->Write(msg, timeout_ms);
if(!remote) {
return false;
@@ -65,6 +63,11 @@
bool ShmMsgQueue::Send(const MQId &remote_id, const void *data, const size_t size, const int timeout_ms)
{
+ // Test shows that in the 2 cases:
+ // 1) build msg first, then find remote queue;
+ // 2) find remote queue first, then build msg;
+ // 1 is about 50% faster than 2, maybe cache related.
+
Msg msg;
if (msg.Build(shm(), Id(), data, size, false)) {
if (Send(remote_id, msg, timeout_ms)) {
diff --git a/src/shm_queue.h b/src/shm_queue.h
index d0eb972..023b2d1 100644
--- a/src/shm_queue.h
+++ b/src/shm_queue.h
@@ -53,7 +53,7 @@
using Super::size;
using Super::capacity;
const MQId &Id() const { return id_; }
- bool Write(D buf, const int timeout_ms) {
+ bool Write(const D &buf, const int timeout_ms) {
Guard lock(mutex());
if (cond_write_.timed_wait(lock, MSFromNow(timeout_ms), [&]() { return !this->full(); })) {
this->push_back(buf);
@@ -67,7 +67,8 @@
bool Read(D &buf, const int timeout_ms){
Guard lock(mutex());
if (cond_read_.timed_wait(lock, MSFromNow(timeout_ms), [&]() { return !this->empty(); })) {
- buf = this->front();
+ using std::swap;
+ swap(buf, this->front());
this->pop_front();
cond_write_.notify_one();
return true;
--
Gitblit v1.8.0