Skip to content

Commit

Permalink
Cache regex pattern matches for XMPP_SKIP_UPDATE_SEND pattern
Browse files Browse the repository at this point in the history
Regex pattern matching can be expensive. Hence cache the result inside
BgpXmppChannel object to avoid redundant regex evaluatiob.

Change-Id: I3fb1f5a8b4d9a3db6e25376cecd0ac774a2b9639
Partial-Bug: 1464016
  • Loading branch information
ananth-at-camphor-networks committed Jul 21, 2016
1 parent 84e4def commit 7c511ac
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
15 changes: 11 additions & 4 deletions src/bgp/bgp_xmpp_channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -541,15 +541,20 @@ class BgpXmppChannel::XmppPeer : public IPeer {
};

// Skip sending updates if the destinatin matches against the pattern.
// XX Used in test environments only
bool BgpXmppChannel::SkipUpdateSend() const {
// XXX Used in test environments only
bool BgpXmppChannel::SkipUpdateSend() {
static char *skip_env_ = getenv("XMPP_SKIP_UPDATE_SEND");
if (!skip_env_)
return false;

// Use XMPP_SKIP_UPDATE_SEND as a regex pattern to match against destination
smatch matches;
return regex_search(ToString(), matches, regex(skip_env_));
// Cache the result to avoid redundant regex evaluation
if (!skip_update_send_cached_) {
smatch matches;
skip_update_send_ = regex_search(ToString(), matches, regex(skip_env_));
skip_update_send_cached_ = true;
}
return skip_update_send_;
}

bool BgpXmppChannel::XmppPeer::SendUpdate(const uint8_t *msg, size_t msgsize) {
Expand Down Expand Up @@ -605,6 +610,8 @@ BgpXmppChannel::BgpXmppChannel(XmppChannel *channel, BgpServer *bgp_server,
deleted_(false),
defer_peer_close_(false),
membership_unavailable_(false),
skip_update_send_(false),
skip_update_send_cached_(false),
end_of_rib_timer_(NULL),
membership_response_worker_(
TaskScheduler::GetInstance()->GetTaskId("xmpp::StateMachine"),
Expand Down
4 changes: 3 additions & 1 deletion src/bgp/bgp_xmpp_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class BgpXmppChannel {
uint64_t get_tx_route_reach() const { return stats_[TX].reach; }
uint64_t get_tx_route_unreach() const { return stats_[TX].unreach; }
uint64_t get_tx_update() const { return stats_[TX].rt_updates; }
bool SkipUpdateSend() const;
bool SkipUpdateSend();

protected:
XmppChannel *channel_;
Expand Down Expand Up @@ -271,6 +271,8 @@ class BgpXmppChannel {
bool deleted_;
bool defer_peer_close_;
bool membership_unavailable_;
bool skip_update_send_;
bool skip_update_send_cached_;
Timer *end_of_rib_timer_;
WorkQueue<std::string> membership_response_worker_;
SubscribedRoutingInstanceList routing_instances_;
Expand Down

0 comments on commit 7c511ac

Please sign in to comment.