Skip to content

Commit f65a814

Browse files
committed
ai perf: more2
1 parent d59ca1f commit f65a814

File tree

14 files changed

+39
-34
lines changed

14 files changed

+39
-34
lines changed

src/domino/DataDomino.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ S_PTR<void> DataDomino<aDominoType>::getData(const Domino::EvName& aEvName) cons
6363
template<typename aDominoType>
6464
bool DataDomino<aDominoType>::replaceDataOK(const Domino::EvName& aEvName, S_PTR<void> aData) noexcept
6565
{
66-
return ev_data_S_.replaceOK(this->newEvent(aEvName), aData);
66+
return ev_data_S_.replaceOK(this->newEvent(aEvName), std::move(aData));
6767
}
6868

6969
// ***********************************************************************************************

src/domino/RmEvDom.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ Domino::Event RmEvDom<aDominoType>::recycleEv_() noexcept
6464
if (isRemovedEv_.empty())
6565
return Domino::D_EVENT_FAILED_RET;
6666

67-
const auto ev = *(isRemovedEv_.begin());
68-
isRemovedEv_.erase(ev);
67+
const auto it = isRemovedEv_.begin();
68+
const auto ev = *it;
69+
isRemovedEv_.erase(it); // erase by iterator to avoid second lookup
6970
return ev;
7071
}
7172

src/log/StrCoutFSL.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ StrCoutFSL::~StrCoutFSL() noexcept
5454
inline
5555
void StrCoutFSL::forceSave() const noexcept
5656
{
57-
std::cout << rdbuf() << std::endl; // internet says rdbuf() is faster than str()
57+
std::cout << rdbuf() << '\n'; // rdbuf() faster than str(); newline w/o flush faster than endl
5858
}
5959

6060
} // namespace

src/log/UniSmartLog.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class UniSmartLog
7373
auto&& name_log = name_log_S_.find(aUniLogName);
7474
return name_log == name_log_S_.end()
7575
? 0
76-
: name_log->second->str().size();
76+
: static_cast<size_t>(name_log->second->tellp()); // avoid str().size() which creates string copy
7777
}
7878
#endif
7979
};

src/obj_anywhere/DataStore.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ bool DataStore<aDataKey>::emplaceOK(const aDataKey& aKey, S_PTR<void> aData) noe
5858
}
5959
else
6060
{
61-
if (key_data_S_.emplace(aKey, std::move(aData)).second)
61+
if (key_data_S_.try_emplace(aKey, std::move(aData)).second)
6262
return true;
6363
else
6464
{
@@ -99,7 +99,7 @@ bool DataStore<aDataKey>::replaceOK(const aDataKey& aKey, S_PTR<void> aData) noe
9999
if (! aData)
100100
return emplaceOK(aKey, nullptr);
101101

102-
key_data_S_[aKey] = std::move(aData);
102+
key_data_S_.insert_or_assign(aKey, std::move(aData));
103103
return true;
104104
} catch(...) {
105105
ERR("(DataStore) except->failed!!! key=" << typeid(aDataKey).name());

src/obj_anywhere/ObjAnywhere.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ using namespace std;
99

1010
namespace rlib
1111
{
12-
shared_ptr<DataStore<ObjName>> ObjAnywhere::name_obj_S_;
12+
unique_ptr<DataStore<ObjName>> ObjAnywhere::name_obj_S_;
1313

1414
// ***********************************************************************************************
1515
void ObjAnywhere::deinit() noexcept
@@ -24,7 +24,7 @@ void ObjAnywhere::init(UniLog& oneLog) noexcept
2424
WRN("(ObjAnywhere) !!! Refuse dup init.")
2525
else
2626
{
27-
name_obj_S_ = make_shared<DataStore<ObjName>>();
27+
name_obj_S_ = make_unique<DataStore<ObjName>>();
2828
HID("(ObjAnywhere) Succeed.");
2929
}
3030
}

src/obj_anywhere/ObjAnywhere.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class ObjAnywhere
5656

5757
private:
5858
// -------------------------------------------------------------------------------------------
59-
static std::shared_ptr<DataStore<ObjName>> name_obj_S_; // store aObj w/o include aObj.hpp; shared_ptr is safe here
59+
static std::unique_ptr<DataStore<ObjName>> name_obj_S_; // store aObj w/o include aObj.hpp; unique_ptr is safe here
6060
};
6161

6262
// ***********************************************************************************************

src/thread/AsyncBack.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ bool AsyncBack::newTaskOK(const MT_TaskEntryFN& mt_aEntryFN, const TaskBackFN& a
2424
launch::async,
2525
// - must cp mt_aEntryFN than ref, otherwise dead loop
2626
// - &mt_nDoneFut is better than "this" that can access other non-MT-safe member
27-
[mt_aEntryFN, &mt_nDoneFut = mt_nDoneFut_]() noexcept // thread main
27+
[mt_aEntryFN = std::move(mt_aEntryFN), &mt_nDoneFut = mt_nDoneFut_]() mutable noexcept // thread main
2828
{
2929
SafePtr ret;
3030
try { ret = mt_aEntryFN(); }
3131
catch(...) {} // continue following
32+
mt_aEntryFN = nullptr; // early release captured function
3233

3334
mt_nDoneFut.fetch_add(1, std::memory_order_relaxed); // fastest +1
3435
mt_pingMainTH();

src/thread/MT_PingMainTH.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77

88
namespace rlib
99
{
10-
MT_Semaphore g_semToMainTH;
10+
alignas(64) MT_Semaphore g_semToMainTH;
1111
} // namespace

src/thread/MT_Semaphore.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void MT_Semaphore::mt_notify() noexcept
1717
{
1818
// - can't sem_getvalue() as NOT mt-safe
1919
// - mt_notified_ is to avoid sem counter overflow; & not rouse main-thread repeatedly
20-
if (!mt_notified_.test_and_set()) // memory_order_seq_cst to ensure other thread(s) see the flag
20+
if (!mt_notified_.test_and_set(std::memory_order_acquire)) // acquire: ensure sem_post visibility
2121
sem_post(&mt_sem_); // impossible failed since MT_Semaphore's constructor
2222
}
2323

@@ -34,9 +34,9 @@ void MT_Semaphore::timedwait(const size_t aSec, const size_t aRestNsec) noexcept
3434
for (;;)
3535
{
3636
const auto ret = sem_timedwait(&mt_sem_, &ts);
37-
if (errno == ETIMEDOUT || ret == 0) // timeout or notified -> wakeup to handle sth
37+
if (ret == 0 || errno == ETIMEDOUT) // notified or timeout -> wakeup to handle sth
3838
{
39-
mt_notified_.clear(); // memory_order_seq_cst to ensure other thread(s) see the flag
39+
mt_notified_.clear(std::memory_order_release); // release: publish flag clear to other threads
4040
return;
4141
}
4242

0 commit comments

Comments
 (0)