Fold a number of allocations into the if statement where they're used

This commit is contained in:
Tim Wojtulewicz 2020-03-24 13:03:55 -07:00
parent a5a08b3bf3
commit e4a36678ba

View file

@ -474,8 +474,7 @@ IntrusivePtr<Val> BinaryExpr::Eval(Frame* f) const
for ( unsigned int i = 0; i < vv->Size(); ++i ) for ( unsigned int i = 0; i < vv->Size(); ++i )
{ {
Val* vv_i = vv->Lookup(i); if ( Val* vv_i = vv->Lookup(i) )
if ( vv_i )
v_result->Assign(i, is_vec1 ? Fold(vv_i, v2.get()) v_result->Assign(i, is_vec1 ? Fold(vv_i, v2.get())
: Fold(v1.get(), vv_i)); : Fold(v1.get(), vv_i));
else else
@ -901,12 +900,10 @@ IntrusivePtr<Val> CloneExpr::Eval(Frame* f) const
if ( IsError() ) if ( IsError() )
return nullptr; return nullptr;
auto v = op->Eval(f); if ( auto v = op->Eval(f) )
return Fold(v.get());
if ( ! v ) return nullptr;
return nullptr;
return Fold(v.get());
} }
IntrusivePtr<Val> CloneExpr::Fold(Val* v) const IntrusivePtr<Val> CloneExpr::Fold(Val* v) const
@ -1031,8 +1028,7 @@ NotExpr::NotExpr(IntrusivePtr<Expr> arg_op)
if ( IsError() ) if ( IsError() )
return; return;
BroType* t = op->Type(); TypeTag bt = op->Type()->Tag();
TypeTag bt = t->Tag();
if ( ! IsIntegral(bt) && bt != TYPE_BOOL ) if ( ! IsIntegral(bt) && bt != TYPE_BOOL )
ExprError("requires an integral or boolean operand"); ExprError("requires an integral or boolean operand");
@ -1267,9 +1263,7 @@ IntrusivePtr<Val> AddToExpr::Eval(Frame* f) const
return v1; return v1;
} }
auto result = Fold(v1.get(), v2.get()); if ( auto result = Fold(v1.get(), v2.get()) )
if ( result )
{ {
op1->Assign(f, result); op1->Assign(f, result);
return result; return result;
@ -1359,9 +1353,7 @@ IntrusivePtr<Val> RemoveFromExpr::Eval(Frame* f) const
if ( ! v2 ) if ( ! v2 )
return nullptr; return nullptr;
auto result = Fold(v1.get(), v2.get()); if ( auto result = Fold(v1.get(), v2.get()) )
if ( result )
{ {
op1->Assign(f, result); op1->Assign(f, result);
return result; return result;
@ -2275,9 +2267,7 @@ IntrusivePtr<Val> AssignExpr::Eval(Frame* f) const
return nullptr; return nullptr;
} }
auto v = op2->Eval(f); if ( auto v = op2->Eval(f) )
if ( v )
{ {
op1->Assign(f, v); op1->Assign(f, v);
@ -2470,9 +2460,7 @@ IntrusivePtr<Val> IndexSliceAssignExpr::Eval(Frame* f) const
return nullptr; return nullptr;
} }
auto v = op2->Eval(f); if ( auto v = op2->Eval(f) )
if ( v )
op1->Assign(f, std::move(v)); op1->Assign(f, std::move(v));
return nullptr; return nullptr;
@ -2917,9 +2905,7 @@ void FieldExpr::Assign(Frame* f, IntrusivePtr<Val> v)
if ( IsError() ) if ( IsError() )
return; return;
auto op_v = op->Eval(f); if ( auto op_v = op->Eval(f) )
if ( op_v )
{ {
RecordVal* r = op_v->AsRecordVal(); RecordVal* r = op_v->AsRecordVal();
r->Assign(field, std::move(v)); r->Assign(field, std::move(v));
@ -2933,9 +2919,7 @@ void FieldExpr::Delete(Frame* f)
IntrusivePtr<Val> FieldExpr::Fold(Val* v) const IntrusivePtr<Val> FieldExpr::Fold(Val* v) const
{ {
Val* result = v->AsRecordVal()->Lookup(field); if ( Val* result = v->AsRecordVal()->Lookup(field) )
if ( result )
return {NewRef{}, result}; return {NewRef{}, result};
// Check for &default. // Check for &default.
@ -3358,9 +3342,7 @@ VectorConstructorExpr::VectorConstructorExpr(IntrusivePtr<ListExpr> constructor_
return; return;
} }
auto t = merge_type_list(op->AsListExpr()); if ( auto t = merge_type_list(op->AsListExpr()) )
if ( t )
SetType(make_intrusive<VectorType>(std::move(t))); SetType(make_intrusive<VectorType>(std::move(t)));
else else
{ {
@ -3442,12 +3424,11 @@ void FieldAssignExpr::EvalIntoAggregate(const BroType* t, Val* aggr, Frame* f)
if ( IsError() ) if ( IsError() )
return; return;
RecordVal* rec = aggr->AsRecordVal(); if ( auto v = op->Eval(f) )
const RecordType* rt = t->AsRecordType();
auto v = op->Eval(f);
if ( v )
{ {
RecordVal* rec = aggr->AsRecordVal();
const RecordType* rt = t->AsRecordType();
int idx = rt->FieldOffset(field_name.c_str()); int idx = rt->FieldOffset(field_name.c_str());
if ( idx < 0 ) if ( idx < 0 )
@ -3546,8 +3527,7 @@ IntrusivePtr<Val> ArithCoerceExpr::Fold(Val* v) const
for ( unsigned int i = 0; i < vv->Size(); ++i ) for ( unsigned int i = 0; i < vv->Size(); ++i )
{ {
Val* elt = vv->Lookup(i); if ( Val* elt = vv->Lookup(i) )
if ( elt )
result->Assign(i, FoldSingleVal(elt, t)); result->Assign(i, FoldSingleVal(elt, t));
else else
result->Assign(i, 0); result->Assign(i, 0);
@ -3671,14 +3651,10 @@ RecordCoerceExpr::~RecordCoerceExpr()
IntrusivePtr<Val> RecordCoerceExpr::InitVal(const BroType* t, IntrusivePtr<Val> aggr) const IntrusivePtr<Val> RecordCoerceExpr::InitVal(const BroType* t, IntrusivePtr<Val> aggr) const
{ {
auto v = Eval(nullptr); if ( auto v = Eval(nullptr) )
if ( v )
{ {
RecordVal* rv = v->AsRecordVal(); RecordVal* rv = v->AsRecordVal();
auto ar = rv->CoerceTo(t->AsRecordType(), aggr.release()); if ( auto ar = rv->CoerceTo(t->AsRecordType(), aggr.release()) )
if ( ar )
return ar; return ar;
} }
@ -3723,9 +3699,7 @@ IntrusivePtr<Val> RecordCoerceExpr::Fold(Val* v) const
field_type->Tag() == TYPE_RECORD && field_type->Tag() == TYPE_RECORD &&
! same_type(rhs_type, field_type) ) ! same_type(rhs_type, field_type) )
{ {
auto new_val = rhs->AsRecordVal()->CoerceTo(field_type->AsRecordType()); if ( auto new_val = rhs->AsRecordVal()->CoerceTo(field_type->AsRecordType()) )
if ( new_val )
rhs = std::move(new_val); rhs = std::move(new_val);
} }
else if ( BothArithmetic(rhs_type->Tag(), field_type->Tag()) && else if ( BothArithmetic(rhs_type->Tag(), field_type->Tag()) &&
@ -3741,10 +3715,7 @@ IntrusivePtr<Val> RecordCoerceExpr::Fold(Val* v) const
} }
else else
{ {
const Attr* def = if ( const Attr* def = Type()->AsRecordType()->FieldDecl(i)->FindAttr(ATTR_DEFAULT) )
Type()->AsRecordType()->FieldDecl(i)->FindAttr(ATTR_DEFAULT);
if ( def )
{ {
auto def_val = def->AttrExpr()->Eval(nullptr); auto def_val = def->AttrExpr()->Eval(nullptr);
BroType* def_type = def_val->Type(); BroType* def_type = def_val->Type();
@ -3864,17 +3835,14 @@ IntrusivePtr<Val> FlattenExpr::Fold(Val* v) const
for ( int i = 0; i < num_fields; ++i ) for ( int i = 0; i < num_fields; ++i )
{ {
Val* fv = rv->Lookup(i); if ( Val* fv = rv->Lookup(i) )
if ( fv )
{ {
l->Append(fv->Ref()); l->Append(fv->Ref());
continue; continue;
} }
const RecordType* rv_t = rv->Type()->AsRecordType(); const RecordType* rv_t = rv->Type()->AsRecordType();
const Attr* fa = rv_t->FieldDecl(i)->FindAttr(ATTR_DEFAULT); if ( const Attr* fa = rv_t->FieldDecl(i)->FindAttr(ATTR_DEFAULT) )
if ( fa )
l->Append(fa->AttrExpr()->Eval(nullptr).release()); l->Append(fa->AttrExpr()->Eval(nullptr).release());
else else
@ -4219,12 +4187,9 @@ IntrusivePtr<Val> CallExpr::Eval(Frame* f) const
// Check for that. // Check for that.
if ( f ) if ( f )
{ {
trigger::Trigger* trigger = f->GetTrigger(); if ( trigger::Trigger* trigger = f->GetTrigger() )
if ( trigger )
{ {
Val* v = trigger->Lookup(this); if ( Val* v = trigger->Lookup(this) )
if ( v )
{ {
DBG_LOG(DBG_NOTIFIERS, DBG_LOG(DBG_NOTIFIERS,
"%s: provides cached function result", "%s: provides cached function result",