Skip to content

Commit ac9ca01

Browse files
author
Janosch Machowinski
committed
perf: Do not search waitset from 0 on each request
Signed-off-by: Janosch Machowinski <j.machowinski@cellumation.com>
1 parent 5d99e3b commit ac9ca01

1 file changed

Lines changed: 23 additions & 14 deletions

File tree

rclcpp/include/rclcpp/wait_result.hpp

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,13 @@ class WaitResult final
183183
if (this->kind() == WaitResultKind::Ready) {
184184
auto & wait_set = this->get_wait_set();
185185
auto & rcl_wait_set = wait_set.storage_get_rcl_wait_set();
186-
for (size_t ii = 0; ii < wait_set.size_of_subscriptions(); ++ii) {
187-
if (rcl_wait_set.subscriptions[ii] != nullptr) {
188-
ret = wait_set.subscriptions(ii);
189-
rcl_wait_set.subscriptions[ii] = nullptr;
186+
for (; next_subscription_index_ < wait_set.size_of_subscriptions();
187+
++next_subscription_index_)
188+
{
189+
if (rcl_wait_set.subscriptions[next_subscription_index_] != nullptr) {
190+
ret = wait_set.subscriptions(next_subscription_index_);
191+
rcl_wait_set.subscriptions[next_subscription_index_] = nullptr;
192+
++next_subscription_index_;
190193
break;
191194
}
192195
}
@@ -203,10 +206,11 @@ class WaitResult final
203206
if (this->kind() == WaitResultKind::Ready) {
204207
auto & wait_set = this->get_wait_set();
205208
auto & rcl_wait_set = wait_set.storage_get_rcl_wait_set();
206-
for (size_t ii = 0; ii < wait_set.size_of_services(); ++ii) {
207-
if (rcl_wait_set.services[ii] != nullptr) {
208-
ret = wait_set.services(ii);
209-
rcl_wait_set.services[ii] = nullptr;
209+
for (; next_service_index_ < wait_set.size_of_services(); ++next_service_index_) {
210+
if (rcl_wait_set.services[next_service_index_] != nullptr) {
211+
ret = wait_set.services(next_service_index_);
212+
rcl_wait_set.services[next_service_index_] = nullptr;
213+
++next_service_index_;
210214
break;
211215
}
212216
}
@@ -223,10 +227,11 @@ class WaitResult final
223227
if (this->kind() == WaitResultKind::Ready) {
224228
auto & wait_set = this->get_wait_set();
225229
auto & rcl_wait_set = wait_set.storage_get_rcl_wait_set();
226-
for (size_t ii = 0; ii < wait_set.size_of_clients(); ++ii) {
227-
if (rcl_wait_set.clients[ii] != nullptr) {
228-
ret = wait_set.clients(ii);
229-
rcl_wait_set.clients[ii] = nullptr;
230+
for (; next_client_index_ < wait_set.size_of_clients(); ++next_client_index_) {
231+
if (rcl_wait_set.clients[next_client_index_] != nullptr) {
232+
ret = wait_set.clients(next_client_index_);
233+
rcl_wait_set.clients[next_client_index_] = nullptr;
234+
++next_client_index_;
230235
break;
231236
}
232237
}
@@ -245,10 +250,11 @@ class WaitResult final
245250
if (this->kind() == WaitResultKind::Ready) {
246251
auto & wait_set = this->get_wait_set();
247252
auto rcl_wait_set = wait_set.get_rcl_wait_set();
248-
while (next_waitable_index_ < wait_set.size_of_waitables()) {
249-
auto cur_waitable = wait_set.waitables(next_waitable_index_++);
253+
for (; next_waitable_index_ < wait_set.size_of_waitables(); ++next_waitable_index_) {
254+
auto cur_waitable = wait_set.waitables(next_waitable_index_);
250255
if (cur_waitable != nullptr && cur_waitable->is_ready(rcl_wait_set)) {
251256
waitable = cur_waitable;
257+
++next_waitable_index_;
252258
break;
253259
}
254260
}
@@ -293,6 +299,9 @@ class WaitResult final
293299
WaitSetT * wait_set_pointer_ = nullptr;
294300

295301
size_t next_timer_index_ = 0;
302+
size_t next_subscription_index_ = 0;
303+
size_t next_service_index_ = 0;
304+
size_t next_client_index_ = 0;
296305
size_t next_waitable_index_ = 0;
297306
};
298307

0 commit comments

Comments
 (0)