Skip to content

Commit 500e967

Browse files
committed
Temporary revert for release "matcher: refactor the matcher to avoid unnecessary wrapper and heap allocation (envoyproxy#40249)"
This reverts commit c34a837.
1 parent 6becd01 commit 500e967

File tree

40 files changed

+389
-377
lines changed

40 files changed

+389
-377
lines changed

envoy/matcher/matcher.h

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,22 @@ class Action {
9191
}
9292
};
9393

94-
using ActionConstSharedPtr = std::shared_ptr<const Action>;
94+
using ActionPtr = std::unique_ptr<Action>;
95+
using ActionFactoryCb = std::function<ActionPtr()>;
9596

9697
template <class ActionFactoryContext> class ActionFactory : public Config::TypedFactory {
9798
public:
98-
virtual ActionConstSharedPtr
99-
createAction(const Protobuf::Message& config, ActionFactoryContext& action_factory_context,
100-
ProtobufMessage::ValidationVisitor& validation_visitor) PURE;
99+
virtual ActionFactoryCb
100+
createActionFactoryCb(const Protobuf::Message& config,
101+
ActionFactoryContext& action_factory_context,
102+
ProtobufMessage::ValidationVisitor& validation_visitor) PURE;
101103

102104
std::string category() const override { return "envoy.matching.action"; }
103105
};
104106

105107
// On match, we either return the action to perform or another match tree to match against.
106108
template <class DataType> struct OnMatch {
107-
const ActionConstSharedPtr action_;
109+
const ActionFactoryCb action_cb_;
108110
const MatchTreeSharedPtr<DataType> matcher_;
109111
bool keep_matching_{};
110112
};
@@ -128,39 +130,30 @@ template <class DataType> class OnMatchFactory {
128130
// - The match could not be completed due to lack of data (isInsufficientData() will return true.)
129131
// - The match was completed, no match found (isNoMatch() will return true.)
130132
// - The match was completed, match found (isMatch() will return true, action() will return the
131-
// ActionConstSharedPtr.)
133+
// ActionFactoryCb.)
132134
struct MatchResult {
133135
public:
134-
MatchResult(ActionConstSharedPtr cb) : result_(std::move(cb)) {}
136+
MatchResult(ActionFactoryCb cb) : result_(std::move(cb)) {}
135137
static MatchResult noMatch() { return MatchResult(NoMatch{}); }
136138
static MatchResult insufficientData() { return MatchResult(InsufficientData{}); }
137139
bool isInsufficientData() const { return absl::holds_alternative<InsufficientData>(result_); }
138140
bool isComplete() const { return !isInsufficientData(); }
139141
bool isNoMatch() const { return absl::holds_alternative<NoMatch>(result_); }
140-
bool isMatch() const { return absl::holds_alternative<ActionConstSharedPtr>(result_); }
141-
const ActionConstSharedPtr& action() const {
142-
ASSERT(isMatch());
143-
return absl::get<ActionConstSharedPtr>(result_);
144-
}
145-
// Returns the action by move. The caller must ensure that the MatchResult is not used after
146-
// this call.
147-
ActionConstSharedPtr actionByMove() {
148-
ASSERT(isMatch());
149-
return absl::get<ActionConstSharedPtr>(std::move(result_));
150-
}
142+
bool isMatch() const { return absl::holds_alternative<ActionFactoryCb>(result_); }
143+
ActionFactoryCb actionFactory() const { return absl::get<ActionFactoryCb>(result_); }
144+
ActionPtr action() const { return actionFactory()(); }
151145

152146
private:
153147
struct InsufficientData {};
154148
struct NoMatch {};
155-
using Result = absl::variant<ActionConstSharedPtr, NoMatch, InsufficientData>;
149+
using Result = absl::variant<ActionFactoryCb, NoMatch, InsufficientData>;
156150
Result result_;
157151
MatchResult(NoMatch) : result_(NoMatch{}) {}
158152
MatchResult(InsufficientData) : result_(InsufficientData{}) {}
159153
};
160154

161155
// Callback to execute against skipped matches' actions.
162-
using SkippedMatchCb = std::function<void(const ActionConstSharedPtr&)>;
163-
156+
using SkippedMatchCb = std::function<void(ActionFactoryCb)>;
164157
/**
165158
* MatchTree provides the interface for performing matches against the data provided by DataType.
166159
*/
@@ -194,19 +187,19 @@ template <class DataType> class MatchTree {
194187
// Parent result's keep_matching skips the nested result.
195188
if (on_match->keep_matching_ && nested_result.isMatch()) {
196189
if (skipped_match_cb) {
197-
skipped_match_cb(nested_result.action());
190+
skipped_match_cb(nested_result.actionFactory());
198191
}
199192
return MatchResult::noMatch();
200193
}
201194
return nested_result;
202195
}
203-
if (on_match->action_ && on_match->keep_matching_) {
196+
if (on_match->action_cb_ && on_match->keep_matching_) {
204197
if (skipped_match_cb) {
205-
skipped_match_cb(on_match->action_);
198+
skipped_match_cb(on_match->action_cb_);
206199
}
207200
return MatchResult::noMatch();
208201
}
209-
return MatchResult{on_match->action_};
202+
return MatchResult{on_match->action_cb_};
210203
}
211204
};
212205

source/common/listener_manager/filter_chain_manager_impl.cc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ class FilterChainNameActionFactory : public Matcher::ActionFactory<FilterChainAc
4949
Logger::Loggable<Logger::Id::config> {
5050
public:
5151
std::string name() const override { return "filter-chain-name"; }
52-
Matcher::ActionConstSharedPtr createAction(const Protobuf::Message& config,
53-
FilterChainActionFactoryContext&,
54-
ProtobufMessage::ValidationVisitor&) override {
55-
return std::make_shared<FilterChainNameAction>(
56-
dynamic_cast<const ProtobufWkt::StringValue&>(config).value());
52+
Matcher::ActionFactoryCb createActionFactoryCb(const Protobuf::Message& config,
53+
FilterChainActionFactoryContext&,
54+
ProtobufMessage::ValidationVisitor&) override {
55+
const auto& name = dynamic_cast<const ProtobufWkt::StringValue&>(config);
56+
return [value = name.value()]() { return std::make_unique<FilterChainNameAction>(value); };
5757
}
5858
ProtobufTypes::MessagePtr createEmptyConfigProto() override {
5959
return std::make_unique<ProtobufWkt::StringValue>();
@@ -573,8 +573,9 @@ FilterChainManagerImpl::findFilterChainUsingMatcher(const Network::ConnectionSoc
573573
Matcher::evaluateMatch<Network::MatchingData>(*matcher_, data);
574574
ASSERT(match_result.isComplete(), "Matching must complete for network streams.");
575575
if (match_result.isMatch()) {
576-
return match_result.action()->getTyped<Configuration::FilterChainBaseAction>().get(
577-
filter_chains_by_name_, info);
576+
const Matcher::ActionPtr action = match_result.action();
577+
return action->getTyped<Configuration::FilterChainBaseAction>().get(filter_chains_by_name_,
578+
info);
578579
}
579580
return default_filter_chain_.get();
580581
}

source/common/matcher/matcher.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,10 @@ class MatchTreeFactory : public OnMatchFactory<DataType> {
324324
on_match.action().typed_config(), server_factory_context_.messageValidationVisitor(),
325325
factory);
326326

327-
auto action = factory.createAction(*message, action_factory_context_,
328-
server_factory_context_.messageValidationVisitor());
329-
return [action, keep_matching = on_match.keep_matching()] {
330-
return OnMatch<DataType>{action, {}, keep_matching};
327+
auto action_factory = factory.createActionFactoryCb(
328+
*message, action_factory_context_, server_factory_context_.messageValidationVisitor());
329+
return [action_factory, keep_matching = on_match.keep_matching()] {
330+
return OnMatch<DataType>{action_factory, {}, keep_matching};
331331
};
332332
}
333333

source/common/router/config_impl.cc

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,13 +1825,14 @@ RouteConstSharedPtr VirtualHostImpl::getRouteFromEntries(const RouteCallback& cb
18251825
Matcher::evaluateMatch<Http::HttpMatchingData>(*matcher_, data);
18261826

18271827
if (match_result.isMatch()) {
1828-
const auto result = match_result.actionByMove();
1828+
const Matcher::ActionPtr result = match_result.action();
18291829
if (result->typeUrl() == RouteMatchAction::staticTypeUrl()) {
1830-
return getRouteFromRoutes(
1831-
cb, headers, stream_info, random_value,
1832-
{std::dynamic_pointer_cast<const RouteEntryImplBase>(std::move(result))});
1830+
const RouteMatchAction& route_action = result->getTyped<RouteMatchAction>();
1831+
1832+
return getRouteFromRoutes(cb, headers, stream_info, random_value, {route_action.route()});
18331833
} else if (result->typeUrl() == RouteListMatchAction::staticTypeUrl()) {
18341834
const RouteListMatchAction& action = result->getTyped<RouteListMatchAction>();
1835+
18351836
return getRouteFromRoutes(cb, headers, stream_info, random_value, action.routes());
18361837
}
18371838
PANIC("Action in router matcher should be Route or RouteList");
@@ -2139,24 +2140,24 @@ const Envoy::Config::TypedMetadata& NullConfigImpl::typedMetadata() const {
21392140
return DefaultRouteMetadataPack::get().typed_metadata_;
21402141
}
21412142

2142-
Matcher::ActionConstSharedPtr
2143-
RouteMatchActionFactory::createAction(const Protobuf::Message& config, RouteActionContext& context,
2144-
ProtobufMessage::ValidationVisitor& validation_visitor) {
2143+
Matcher::ActionFactoryCb RouteMatchActionFactory::createActionFactoryCb(
2144+
const Protobuf::Message& config, RouteActionContext& context,
2145+
ProtobufMessage::ValidationVisitor& validation_visitor) {
21452146
const auto& route_config =
21462147
MessageUtil::downcastAndValidate<const envoy::config::route::v3::Route&>(config,
21472148
validation_visitor);
21482149
auto route = THROW_OR_RETURN_VALUE(
21492150
RouteCreator::createAndValidateRoute(route_config, context.vhost, context.factory_context,
21502151
validation_visitor, false),
21512152
RouteEntryImplBaseConstSharedPtr);
2152-
return route;
2153+
2154+
return [route]() { return std::make_unique<RouteMatchAction>(route); };
21532155
}
21542156
REGISTER_FACTORY(RouteMatchActionFactory, Matcher::ActionFactory<RouteActionContext>);
21552157

2156-
Matcher::ActionConstSharedPtr
2157-
RouteListMatchActionFactory::createAction(const Protobuf::Message& config,
2158-
RouteActionContext& context,
2159-
ProtobufMessage::ValidationVisitor& validation_visitor) {
2158+
Matcher::ActionFactoryCb RouteListMatchActionFactory::createActionFactoryCb(
2159+
const Protobuf::Message& config, RouteActionContext& context,
2160+
ProtobufMessage::ValidationVisitor& validation_visitor) {
21602161
const auto& route_config =
21612162
MessageUtil::downcastAndValidate<const envoy::config::route::v3::RouteList&>(
21622163
config, validation_visitor);
@@ -2168,7 +2169,7 @@ RouteListMatchActionFactory::createAction(const Protobuf::Message& config,
21682169
validation_visitor, false),
21692170
RouteEntryImplBaseConstSharedPtr));
21702171
}
2171-
return std::make_shared<RouteListMatchAction>(std::move(routes));
2172+
return [routes]() { return std::make_unique<RouteListMatchAction>(routes); };
21722173
}
21732174
REGISTER_FACTORY(RouteListMatchActionFactory, Matcher::ActionFactory<RouteActionContext>);
21742175

source/common/router/config_impl.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,6 @@ class RouteEntryImplBase : public RouteEntryAndRoute,
619619
public Matchable,
620620
public DirectResponseEntry,
621621
public PathMatchCriterion,
622-
public Matcher::ActionBase<envoy::config::route::v3::Route>,
623622
public std::enable_shared_from_this<RouteEntryImplBase>,
624623
Logger::Loggable<Logger::Id::router> {
625624
protected:
@@ -1192,9 +1191,9 @@ class RouteMatchAction : public Matcher::ActionBase<envoy::config::route::v3::Ro
11921191
// Registered factory for RouteMatchAction.
11931192
class RouteMatchActionFactory : public Matcher::ActionFactory<RouteActionContext> {
11941193
public:
1195-
Matcher::ActionConstSharedPtr
1196-
createAction(const Protobuf::Message& config, RouteActionContext& context,
1197-
ProtobufMessage::ValidationVisitor& validation_visitor) override;
1194+
Matcher::ActionFactoryCb
1195+
createActionFactoryCb(const Protobuf::Message& config, RouteActionContext& context,
1196+
ProtobufMessage::ValidationVisitor& validation_visitor) override;
11981197
std::string name() const override { return "route"; }
11991198
ProtobufTypes::MessagePtr createEmptyConfigProto() override {
12001199
return std::make_unique<envoy::config::route::v3::Route>();
@@ -1218,9 +1217,9 @@ class RouteListMatchAction : public Matcher::ActionBase<envoy::config::route::v3
12181217
// Registered factory for RouteListMatchAction.
12191218
class RouteListMatchActionFactory : public Matcher::ActionFactory<RouteActionContext> {
12201219
public:
1221-
Matcher::ActionConstSharedPtr
1222-
createAction(const Protobuf::Message& config, RouteActionContext& context,
1223-
ProtobufMessage::ValidationVisitor& validation_visitor) override;
1220+
Matcher::ActionFactoryCb
1221+
createActionFactoryCb(const Protobuf::Message& config, RouteActionContext& context,
1222+
ProtobufMessage::ValidationVisitor& validation_visitor) override;
12241223
std::string name() const override { return "route_match_action"; }
12251224
ProtobufTypes::MessagePtr createEmptyConfigProto() override {
12261225
return std::make_unique<envoy::config::route::v3::RouteList>();

source/extensions/filters/common/rbac/engine_impl.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ namespace Filters {
1111
namespace Common {
1212
namespace RBAC {
1313

14-
Envoy::Matcher::ActionConstSharedPtr
15-
ActionFactory::createAction(const Protobuf::Message& config, ActionContext& context,
16-
ProtobufMessage::ValidationVisitor& validation_visitor) {
14+
Envoy::Matcher::ActionFactoryCb
15+
ActionFactory::createActionFactoryCb(const Protobuf::Message& config, ActionContext& context,
16+
ProtobufMessage::ValidationVisitor& validation_visitor) {
1717
const auto& action_config =
1818
MessageUtil::downcastAndValidate<const envoy::config::rbac::v3::Action&>(config,
1919
validation_visitor);
@@ -25,7 +25,7 @@ ActionFactory::createAction(const Protobuf::Message& config, ActionContext& cont
2525
context.has_log_ = true;
2626
}
2727

28-
return std::make_shared<Action>(name, action);
28+
return [name, action]() { return std::make_unique<Action>(name, action); };
2929
}
3030

3131
REGISTER_FACTORY(ActionFactory, Envoy::Matcher::ActionFactory<ActionContext>);
@@ -138,17 +138,18 @@ bool RoleBasedAccessControlMatcherEngineImpl::handleAction(
138138
Envoy::Matcher::evaluateMatch<Http::HttpMatchingData>(*matcher_, data);
139139
ASSERT(result.isComplete());
140140
if (result.isMatch()) {
141-
const auto& action = result.action()->getTyped<Action>();
141+
auto action = result.action()->getTyped<Action>();
142142
if (effective_policy_id != nullptr) {
143143
*effective_policy_id = action.name();
144144
}
145145

146146
// If there is at least an LOG action in matchers, we have to turn on and off for shared log
147147
// metadata every time when there is a connection or request.
148-
const auto rbac_action = action.action();
148+
auto rbac_action = action.action();
149149
if (has_log_) {
150150
generateLog(info, mode_, rbac_action == envoy::config::rbac::v3::RBAC::LOG);
151151
}
152+
152153
switch (rbac_action) {
153154
PANIC_ON_PROTO_ENUM_SENTINEL_VALUES;
154155
case envoy::config::rbac::v3::RBAC::ALLOW:

source/extensions/filters/common/rbac/engine_impl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ class Action : public Envoy::Matcher::ActionBase<envoy::config::rbac::v3::Action
5050

5151
class ActionFactory : public Envoy::Matcher::ActionFactory<ActionContext> {
5252
public:
53-
Envoy::Matcher::ActionConstSharedPtr
54-
createAction(const Protobuf::Message& config, ActionContext& context,
55-
ProtobufMessage::ValidationVisitor& validation_visitor) override;
53+
Envoy::Matcher::ActionFactoryCb
54+
createActionFactoryCb(const Protobuf::Message& config, ActionContext& context,
55+
ProtobufMessage::ValidationVisitor& validation_visitor) override;
5656
std::string name() const override { return "envoy.filters.rbac.action"; }
5757
ProtobufTypes::MessagePtr createEmptyConfigProto() override {
5858
return std::make_unique<envoy::config::rbac::v3::Action>();

0 commit comments

Comments
 (0)