Skip to content

Commit

Permalink
Add cache of patterns matching discovered topics
Browse files Browse the repository at this point in the history
  • Loading branch information
maxchaos committed May 14, 2024
1 parent 3b7221e commit c13dd83
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
3 changes: 3 additions & 0 deletions rosbag_snapshot/include/rosbag_snapshot/snapshotter.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include <utility>
#include <vector>
#include <memory>
#include <unordered_map>

namespace rosbag_snapshot
{
Expand Down Expand Up @@ -136,6 +137,8 @@ struct ROSBAG_DECL SnapshotterOptions
typedef std::vector<SnapshotterTopicPatternConstPtr> patterns_t;
// List of regexs that match topics which should be tracked by the recorder.
patterns_t patterns_;
// Cache of the first pattern that has matched a given topic (or nullptr if no pattern matched).
std::unordered_map<std::string, SnapshotterTopicPatternConstPtr> matching_patterns_cache_;

SnapshotterOptions(ros::Duration default_duration_limit = ros::Duration(30), int32_t default_memory_limit = -1,
int32_t default_count_limit = -1, ros::Duration status_period = ros::Duration(1),
Expand Down
15 changes: 12 additions & 3 deletions rosbag_snapshot/src/snapshotter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ SnapshotterOptions::SnapshotterOptions(ros::Duration default_duration_limit, int
, clear_buffer_(clear_buffer)
, topics_()
, patterns_()
, matching_patterns_cache_()
{
}

Expand Down Expand Up @@ -120,10 +121,18 @@ bool SnapshotterOptions::addTopicOrPattern(std::string const& topic_or_pattern,

SnapshotterTopicPatternConstPtr SnapshotterOptions::findFirstMatchingPattern(const std::string &topic)
{
for (const auto& pattern : patterns_)
auto it = matching_patterns_cache_.find(topic);
if (it != matching_patterns_cache_.end())
return it->second;
SnapshotterTopicPatternConstPtr matching_pattern = nullptr;
for (auto& pattern : patterns_)
if (pattern->matches(topic))
return pattern;
return nullptr;
{
matching_pattern = pattern;
break;
}
matching_patterns_cache_[topic] = matching_pattern;
return matching_pattern;
}

SnapshotterTopicPattern::SnapshotterTopicPattern(const std::string &pattern,
Expand Down

0 comments on commit c13dd83

Please sign in to comment.