Skip to content

Commit 4d3f114

Browse files
committed
Optimizations.
1 parent cb25489 commit 4d3f114

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

source/base/threading/asio_event_dispatcher.cc

+15-7
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@ namespace base {
3232

3333
namespace {
3434

35-
const size_t kReservedSizeForTimersMap = 100;
35+
const size_t kReservedSizeForTimersMap = 50;
3636
const float kLoadFactorForTimersMap = 0.5;
3737

38+
#if defined(Q_OS_WINDOWS)
39+
const size_t kReservedSizeForEvents = 20;
40+
#endif // defined(Q_OS_WINDOWS)
41+
3842
} // namespace
3943

4044
//--------------------------------------------------------------------------------------------------
@@ -47,6 +51,10 @@ AsioEventDispatcher::AsioEventDispatcher(QObject* parent)
4751

4852
timers_.reserve(kReservedSizeForTimersMap);
4953
timers_.max_load_factor(kLoadFactorForTimersMap);
54+
55+
#if defined(Q_OS_WINDOWS)
56+
events_.reserve(kReservedSizeForEvents);
57+
#endif // defined(Q_OS_WINDOWS)
5058
}
5159

5260
//--------------------------------------------------------------------------------------------------
@@ -191,7 +199,7 @@ int AsioEventDispatcher::remainingTime(int id)
191199
}
192200

193201
//--------------------------------------------------------------------------------------------------
194-
#if defined(Q_OS_WIN)
202+
#if defined(Q_OS_WINDOWS)
195203
bool AsioEventDispatcher::registerEventNotifier(QWinEventNotifier* notifier)
196204
{
197205
HANDLE handle = notifier->handle();
@@ -212,10 +220,10 @@ bool AsioEventDispatcher::registerEventNotifier(QWinEventNotifier* notifier)
212220
events_.emplace_back(notifier, wait_handle);
213221
return true;
214222
}
215-
#endif // defined(Q_OS_WIN)
223+
#endif // defined(Q_OS_WINDOWS)
216224

217225
//--------------------------------------------------------------------------------------------------
218-
#if defined(Q_OS_WIN)
226+
#if defined(Q_OS_WINDOWS)
219227
void AsioEventDispatcher::unregisterEventNotifier(QWinEventNotifier* notifier)
220228
{
221229
auto it = events_.begin();
@@ -234,13 +242,13 @@ void AsioEventDispatcher::unregisterEventNotifier(QWinEventNotifier* notifier)
234242
}
235243
}
236244
}
237-
#endif // defined(Q_OS_WIN)
245+
#endif // defined(Q_OS_WINDOWS)
238246

239247
//--------------------------------------------------------------------------------------------------
240248
void AsioEventDispatcher::wakeUp()
241249
{
242250
// To stop run_one inside method processEvents completes.
243-
io_context_.stop();
251+
asio::post(io_context_, []{});
244252
}
245253

246254
//--------------------------------------------------------------------------------------------------
@@ -323,7 +331,7 @@ void AsioEventDispatcher::asyncWaitForNextTimer()
323331
return;
324332

325333
TimerData& timer = it->second;
326-
timer.start_time = Clock::now();
334+
timer.start_time = timer.expire_time;
327335
timer.expire_time = timer.start_time + Milliseconds(timer.interval);
328336

329337
asyncWaitForNextTimer();

source/base/threading/asio_event_dispatcher.h

+4-12
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class AsioEventDispatcher final : public QAbstractEventDispatcher
7979
TimePoint expire_time;
8080
};
8181

82-
#if defined(Q_OS_WIN)
82+
#if defined(Q_OS_WINDOWS)
8383
struct EventData
8484
{
8585
QWinEventNotifier* notifier;
@@ -88,23 +88,15 @@ class AsioEventDispatcher final : public QAbstractEventDispatcher
8888

8989
static void CALLBACK eventCallback(PVOID parameter, BOOLEAN timer_or_wait_fired);
9090

91-
std::list<EventData> events_;
92-
#endif // defined(Q_OS_WIN)
91+
std::vector<EventData> events_;
92+
#endif // defined(Q_OS_WINDOWS)
9393

9494
void asyncWaitForNextTimer();
9595

96-
struct FastHash
97-
{
98-
size_t operator()(int timer_id) const noexcept
99-
{
100-
return static_cast<size_t>(timer_id);
101-
}
102-
};
103-
10496
asio::io_context io_context_;
10597
asio::executor_work_guard<asio::io_context::executor_type> work_guard_;
10698
std::atomic_bool interrupted_ { false };
107-
std::unordered_map<int, TimerData, FastHash> timers_;
99+
std::unordered_map<int, TimerData> timers_;
108100
asio::high_resolution_timer high_resolution_timer_;
109101

110102
DISALLOW_COPY_AND_ASSIGN(AsioEventDispatcher);

0 commit comments

Comments
 (0)