Skip to content

Commit 3128eb3

Browse files
authored
Perf[BMQ,MQB]: simplify evaluator (#837)
Signed-off-by: Evgeny Malygin <emalygin@bloomberg.net>
1 parent 005f2fe commit 3128eb3

5 files changed

Lines changed: 82 additions & 116 deletions

File tree

src/groups/bmq/bmqeval/bmqeval_simpleevaluator.cpp

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,12 @@ bool SimpleEvaluator::evaluate(EvaluationContext& context) const
135135

136136
bdld::Datum value = d_expression->evaluate(context);
137137

138-
if (context.d_stop) {
138+
if (context.hasError()) {
139139
return false; // RETURN
140140
}
141141

142142
if (!value.isBoolean()) {
143-
context.d_stop = true;
144-
context.d_lastError = ErrorType::e_TYPE;
145-
143+
context.setError(ErrorType::e_TYPE);
146144
return false; // RETURN
147145
}
148146

@@ -182,15 +180,14 @@ SimpleEvaluator::Property::evaluate(EvaluationContext& context) const
182180
context.d_allocator);
183181

184182
if (value.isError()) {
185-
context.d_stop = true;
186-
int rc = value.theError().code();
183+
const int rc = value.theError().code();
187184

188-
if (rc >= ErrorType::e_EVALUATION_FIRST &&
189-
ErrorType::e_EVALUATION_LAST <= rc) {
190-
context.d_lastError = static_cast<ErrorType::Enum>(rc);
185+
if (ErrorType::e_EVALUATION_FIRST <= rc &&
186+
rc <= ErrorType::e_EVALUATION_LAST) {
187+
context.setError(static_cast<ErrorType::Enum>(rc));
191188
}
192189
else {
193-
context.d_lastError = ErrorType::e_UNDEFINED;
190+
context.setError(ErrorType::e_UNDEFINED);
194191
}
195192
}
196193

@@ -228,8 +225,7 @@ bdld::Datum
228225
SimpleEvaluator::UnaryMinus::evaluate(EvaluationContext& context) const
229226
{
230227
bdld::Datum expr = d_expression->evaluate(context);
231-
232-
if (context.d_stop) {
228+
if (context.hasError()) {
233229
return bdld::Datum::createNull(); // RETURN
234230
}
235231

@@ -242,9 +238,8 @@ SimpleEvaluator::UnaryMinus::evaluate(EvaluationContext& context) const
242238
value = expr.theInteger();
243239
}
244240
else {
245-
context.d_lastError = ErrorType::e_TYPE;
246-
247-
return context.stop(); // RETURN
241+
context.setError(ErrorType::e_TYPE);
242+
return bdld::Datum::createNull(); // RETURN
248243
}
249244

250245
return bdld::Datum::createInteger64(-value, context.d_allocator);
@@ -284,29 +279,27 @@ SimpleEvaluator::StringLiteral::evaluate(EvaluationContext& context) const
284279
bdld::Datum SimpleEvaluator::Or::evaluate(EvaluationContext& context) const
285280
{
286281
bdld::Datum left = d_left->evaluate(context);
287-
288-
if (context.d_stop) {
282+
if (context.hasError()) {
289283
return bdld::Datum::createNull(); // RETURN
290284
}
291285

292286
if (!left.isBoolean()) {
293-
context.d_lastError = ErrorType::e_TYPE;
294-
return context.stop(); // RETURN
287+
context.setError(ErrorType::e_TYPE);
288+
return bdld::Datum::createNull(); // RETURN
295289
}
296290

297291
if (left.theBoolean()) {
298-
return bdld::Datum::createBoolean(true); // RETURN
292+
return left; // RETURN
299293
}
300294

301295
bdld::Datum right = d_right->evaluate(context);
302-
303-
if (context.d_stop) {
296+
if (context.hasError()) {
304297
return bdld::Datum::createNull(); // RETURN
305298
}
306299

307300
if (!right.isBoolean()) {
308-
context.d_lastError = ErrorType::e_TYPE;
309-
return context.stop(); // RETURN
301+
context.setError(ErrorType::e_TYPE);
302+
return bdld::Datum::createNull(); // RETURN
310303
}
311304

312305
return right;
@@ -319,29 +312,27 @@ bdld::Datum SimpleEvaluator::Or::evaluate(EvaluationContext& context) const
319312
bdld::Datum SimpleEvaluator::And::evaluate(EvaluationContext& context) const
320313
{
321314
bdld::Datum left = d_left->evaluate(context);
322-
323-
if (context.d_stop) {
315+
if (context.hasError()) {
324316
return bdld::Datum::createNull(); // RETURN
325317
}
326318

327319
if (!left.isBoolean()) {
328-
context.d_lastError = ErrorType::e_TYPE;
329-
return context.stop(); // RETURN
320+
context.setError(ErrorType::e_TYPE);
321+
return bdld::Datum::createNull(); // RETURN
330322
}
331323

332324
if (!left.theBoolean()) {
333-
return bdld::Datum::createBoolean(false); // RETURN
325+
return left; // RETURN
334326
}
335327

336328
bdld::Datum right = d_right->evaluate(context);
337-
338-
if (context.d_stop) {
329+
if (context.hasError()) {
339330
return bdld::Datum::createNull(); // RETURN
340331
}
341332

342333
if (!right.isBoolean()) {
343-
context.d_lastError = ErrorType::e_TYPE;
344-
return context.stop(); // RETURN
334+
context.setError(ErrorType::e_TYPE);
335+
return bdld::Datum::createNull(); // RETURN
345336
}
346337

347338
return right;
@@ -354,15 +345,13 @@ bdld::Datum SimpleEvaluator::And::evaluate(EvaluationContext& context) const
354345
bdld::Datum SimpleEvaluator::Not::evaluate(EvaluationContext& context) const
355346
{
356347
bdld::Datum value = d_expression->evaluate(context);
357-
358-
if (context.d_stop) {
348+
if (context.hasError()) {
359349
return bdld::Datum::createNull(); // RETURN
360350
}
361351

362352
if (!value.isBoolean()) {
363-
context.d_lastError = ErrorType::e_TYPE;
364-
365-
return context.stop(); // RETURN
353+
context.setError(ErrorType::e_TYPE);
354+
return bdld::Datum::createNull(); // RETURN
366355
}
367356

368357
return bdld::Datum::createBoolean(!value.theBoolean());

src/groups/bmq/bmqeval/bmqeval_simpleevaluator.h

Lines changed: 30 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class SimpleEvaluator {
152152
public:
153153
virtual ~Expression();
154154

155-
/// Evaluate a Expression.
155+
/// Evaluate an Expression.
156156
virtual bdld::Datum evaluate(EvaluationContext& context) const = 0;
157157
};
158158

@@ -711,30 +711,27 @@ class CompilationContext {
711711
/// Contain the inputs of an evaluation.
712712
class EvaluationContext {
713713
private:
714-
// Where to read properties from.
715-
PropertiesReader* d_propertiesReader;
716-
717-
// The allocator to use during evaluation.
714+
/// The allocator to use during evaluation.
718715
bslma::Allocator* d_allocator;
719716

720-
// Set by `Expression::evaluate` functions when a property does not
721-
// have the type deduced during compilation. Stop evaluation and return
722-
// `true`.
723-
bool d_stop;
717+
/// Where to read properties from.
718+
PropertiesReader* d_propertiesReader;
724719

720+
/// @brief The last encountered error during evaluation or `e_OK` if there
721+
/// is no error. Note that we stop evaluation and return early if
722+
/// an error occurred
725723
ErrorType::Enum d_lastError;
726724

727725
public:
728726
// CREATORS
729-
EvaluationContext(PropertiesReader* propertiesReader,
730-
bslma::Allocator* allocator);
727+
explicit EvaluationContext(PropertiesReader* propertiesReader,
728+
bslma::Allocator* allocator);
731729

732730
void setPropertiesReader(PropertiesReader* propertiesReader);
733731

734732
void reset();
735733

736-
/// Stop execution.
737-
bdld::Datum stop();
734+
void setError(ErrorType::Enum value);
738735

739736
/// Return `true` if an error occurred and `false` otherwise.
740737
bool hasError() const;
@@ -844,14 +841,12 @@ bdld::Datum
844841
SimpleEvaluator::Comparison<Op>::evaluate(EvaluationContext& context) const
845842
{
846843
bdld::Datum left = d_left->evaluate(context);
847-
848-
if (context.d_stop) {
844+
if (context.hasError()) {
849845
return bdld::Datum::createNull(); // RETURN
850846
}
851847

852848
bdld::Datum right = d_right->evaluate(context);
853-
854-
if (context.d_stop) {
849+
if (context.hasError()) {
855850
return bdld::Datum::createNull(); // RETURN
856851
}
857852

@@ -862,9 +857,8 @@ SimpleEvaluator::Comparison<Op>::evaluate(EvaluationContext& context) const
862857
// RETURN
863858
}
864859

865-
context.d_lastError = ErrorType::e_TYPE;
866-
867-
return context.stop(); // RETURN
860+
context.setError(ErrorType::e_TYPE);
861+
return bdld::Datum::createNull(); // RETURN
868862
}
869863

870864
bsls::Types::Int64 a;
@@ -877,9 +871,8 @@ SimpleEvaluator::Comparison<Op>::evaluate(EvaluationContext& context) const
877871
a = left.theInteger();
878872
}
879873
else {
880-
context.d_lastError = ErrorType::e_TYPE;
881-
882-
return context.stop(); // RETURN
874+
context.setError(ErrorType::e_TYPE);
875+
return bdld::Datum::createNull(); // RETURN
883876
}
884877

885878
if (right.isInteger64()) {
@@ -889,9 +882,8 @@ SimpleEvaluator::Comparison<Op>::evaluate(EvaluationContext& context) const
889882
b = right.theInteger();
890883
}
891884
else {
892-
context.d_lastError = ErrorType::e_TYPE;
893-
894-
return context.stop(); // RETURN
885+
context.setError(ErrorType::e_TYPE);
886+
return bdld::Datum::createNull(); // RETURN
895887
}
896888

897889
return bdld::Datum::createBoolean(Op<bsls::Types::Int64>()(a, b));
@@ -935,14 +927,12 @@ bdld::Datum SimpleEvaluator::NumBinaryOperation<Op>::evaluate(
935927
EvaluationContext& context) const
936928
{
937929
bdld::Datum left = d_left->evaluate(context);
938-
939-
if (context.d_stop) {
930+
if (context.hasError()) {
940931
return bdld::Datum::createNull(); // RETURN
941932
}
942933

943934
bdld::Datum right = d_right->evaluate(context);
944-
945-
if (context.d_stop) {
935+
if (context.hasError()) {
946936
return bdld::Datum::createNull(); // RETURN
947937
}
948938

@@ -956,9 +946,8 @@ bdld::Datum SimpleEvaluator::NumBinaryOperation<Op>::evaluate(
956946
a = left.theInteger();
957947
}
958948
else {
959-
context.d_lastError = ErrorType::e_TYPE;
960-
961-
return context.stop(); // RETURN
949+
context.setError(ErrorType::e_TYPE);
950+
return bdld::Datum::createNull(); // RETURN
962951
}
963952

964953
if (right.isInteger64()) {
@@ -968,9 +957,8 @@ bdld::Datum SimpleEvaluator::NumBinaryOperation<Op>::evaluate(
968957
b = right.theInteger();
969958
}
970959
else {
971-
context.d_lastError = ErrorType::e_TYPE;
972-
973-
return context.stop(); // RETURN
960+
context.setError(ErrorType::e_TYPE);
961+
return bdld::Datum::createNull(); // RETURN
974962
}
975963

976964
bsls::Types::Int64 result = Op<bsls::Types::Int64>()(a, b);
@@ -1170,9 +1158,8 @@ CompilationContext::makeUnaryExpression(ArgType expr)
11701158

11711159
inline EvaluationContext::EvaluationContext(PropertiesReader* propertiesReader,
11721160
bslma::Allocator* allocator)
1173-
: d_propertiesReader(propertiesReader)
1174-
, d_allocator(allocator)
1175-
, d_stop(false)
1161+
: d_allocator(allocator)
1162+
, d_propertiesReader(propertiesReader)
11761163
, d_lastError(ErrorType::e_OK)
11771164
{
11781165
}
@@ -1185,20 +1172,18 @@ EvaluationContext::setPropertiesReader(PropertiesReader* propertiesReader)
11851172

11861173
inline void EvaluationContext::reset()
11871174
{
1188-
d_stop = false;
11891175
d_lastError = ErrorType::e_OK;
11901176
}
11911177

1192-
inline bdld::Datum EvaluationContext::stop()
1178+
inline void EvaluationContext::setError(ErrorType::Enum value)
11931179
{
1194-
d_stop = true;
1195-
1196-
return bdld::Datum::createNull();
1180+
BSLS_ASSERT_SAFE(!hasError());
1181+
d_lastError = value;
11971182
}
11981183

11991184
inline bool EvaluationContext::hasError() const
12001185
{
1201-
return d_lastError != 0;
1186+
return d_lastError != ErrorType::e_OK;
12021187
}
12031188

12041189
inline ErrorType::Enum EvaluationContext::lastError() const

src/groups/bmq/bmqp/bmqp_messageproperties.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,6 @@ MessageProperties::MessageProperties(bslma::Allocator* basicAllocator)
343343
, d_numProps(0)
344344
, d_dataOffset(0)
345345
, d_schema()
346-
, d_lastError(0)
347346
, d_originalNumProps(0)
348347
{
349348
}
@@ -362,7 +361,6 @@ MessageProperties::MessageProperties(const MessageProperties& other,
362361
, d_numProps(other.d_numProps)
363362
, d_dataOffset(other.d_dataOffset)
364363
, d_schema(other.d_schema)
365-
, d_lastError(other.d_lastError)
366364
, d_originalNumProps(other.d_originalNumProps)
367365
{
368366
if (other.d_isBlobConstructed) {
@@ -404,7 +402,6 @@ MessageProperties& MessageProperties::operator=(const MessageProperties& rhs)
404402
d_numProps = rhs.d_numProps;
405403
d_dataOffset = rhs.d_dataOffset;
406404
d_schema = rhs.d_schema;
407-
d_lastError = rhs.d_lastError;
408405
d_originalNumProps = rhs.d_originalNumProps;
409406

410407
return *this;
@@ -422,8 +419,6 @@ void MessageProperties::clear()
422419
d_dataOffset = 0;
423420
d_schema.clear();
424421

425-
d_lastError = 0;
426-
427422
d_originalNumProps = 0;
428423

429424
if (d_isBlobConstructed) {
@@ -739,7 +734,6 @@ int MessageProperties::loadProperties(bool isFirstTime,
739734
start,
740735
i);
741736
if (rc) {
742-
d_lastError = rc;
743737
return rc;
744738
}
745739

@@ -1030,10 +1024,6 @@ bsl::ostream& MessageProperties::print(bsl::ostream& stream,
10301024

10311025
MessagePropertiesIterator msgPropIter(this);
10321026

1033-
if (d_lastError) {
1034-
printer.printAttribute("lastError", d_lastError);
1035-
}
1036-
10371027
while (msgPropIter.hasNext()) {
10381028
bdlma::LocalSequentialAllocator<64> nameLsa(0);
10391029
bmqu::MemOutStream nameOs(&nameLsa);

0 commit comments

Comments
 (0)