lichao
2021-03-25 3f4f049ac015c695a965142e7b1180eae83ae781
reason for speed change; refactor.
4个文件已修改
17 ■■■■■ 已修改文件
CMakeLists.txt 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/msg.h 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/shm_queue.cpp 7 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/shm_queue.h 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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
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
src/shm_queue.cpp
@@ -49,8 +49,6 @@
{
    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)) {
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;