Skip to content

Commit

Permalink
Speed up ros_topic_name_to_zenoh_key.
Browse files Browse the repository at this point in the history
Use stringstream here is really slow; we can speed it
up by about 100% by switching to std::to_string instead.

Signed-off-by: Chris Lalancette <[email protected]>
  • Loading branch information
clalancette committed Feb 7, 2024
1 parent 2c0ee1f commit 7ef08b4
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions rmw_zenoh_cpp/src/rmw_zenoh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <new>
#include <optional>
#include <random>
#include <sstream>
#include <string>
#include <utility>

Expand Down Expand Up @@ -82,6 +81,8 @@ namespace
z_owned_keyexpr_t ros_topic_name_to_zenoh_key(
const char * const topic_name, size_t domain_id, rcutils_allocator_t * allocator)
{
std::string d = std::to_string(domain_id);

size_t start_offset = 0;
size_t topic_name_len = strlen(topic_name);
size_t end_offset = topic_name_len;
Expand All @@ -97,18 +98,17 @@ z_owned_keyexpr_t ros_topic_name_to_zenoh_key(
}
}

std::stringstream domain_ss;
domain_ss << domain_id;
char * stripped_topic_name = rcutils_strndup(
&topic_name[start_offset], end_offset - start_offset, *allocator);
if (stripped_topic_name == nullptr) {
return z_keyexpr_null();
}
z_owned_keyexpr_t keyexpr = z_keyexpr_join(
z_keyexpr(domain_ss.str().c_str()), z_keyexpr(stripped_topic_name));

z_owned_keyexpr_t ret = z_keyexpr_join(z_keyexpr(d.c_str()), z_keyexpr(stripped_topic_name));

allocator->deallocate(stripped_topic_name, allocator->state);

return keyexpr;
return ret;
}

//==============================================================================
Expand Down

0 comments on commit 7ef08b4

Please sign in to comment.