From 74d36eb759d7e60e9aed15430f4f5225707975c1 Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Sat, 2 Mar 2024 14:21:22 -0800 Subject: [PATCH] script optimization support for clearing tables/vectors using "delete" --- src/script_opt/CPP/Stmts.cc | 9 +++++++++ src/script_opt/ProfileFunc.cc | 7 +++++-- src/script_opt/Stmt.cc | 6 +----- src/script_opt/ZAM/Expr.cc | 2 +- src/script_opt/ZAM/Ops.in | 11 +++++++++++ src/script_opt/ZAM/Stmt.cc | 10 ++++++++++ 6 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/script_opt/CPP/Stmts.cc b/src/script_opt/CPP/Stmts.cc index ff80428849..40d9c0376f 100644 --- a/src/script_opt/CPP/Stmts.cc +++ b/src/script_opt/CPP/Stmts.cc @@ -159,6 +159,15 @@ void CPPCompile::GenAddStmt(const ExprStmt* es) { void CPPCompile::GenDeleteStmt(const ExprStmt* es) { auto op = es->StmtExpr(); + + if ( op->Tag() == EXPR_NAME ) { + if ( op->GetType()->Tag() == TYPE_TABLE ) + Emit("%s->RemoveAll();", GenExpr(op, GEN_VAL_PTR)); + else + Emit("%s->Resize(0);", GenExpr(op, GEN_VAL_PTR)); + return; + } + auto aggr = op->GetOp1(); auto aggr_gen = GenExpr(aggr, GEN_VAL_PTR); diff --git a/src/script_opt/ProfileFunc.cc b/src/script_opt/ProfileFunc.cc index df83fe1dd8..901ca70429 100644 --- a/src/script_opt/ProfileFunc.cc +++ b/src/script_opt/ProfileFunc.cc @@ -161,8 +161,11 @@ TraversalCode ProfileFunc::PreStmt(const Stmt* s) { case STMT_DELETE: { auto ad_stmt = static_cast(s); auto ad_e = ad_stmt->StmtExpr(); - auto& lhs_t = ad_e->GetOp1()->GetType(); - aggr_mods.insert(lhs_t.get()); + auto lhs = ad_e->GetOp1(); + if ( lhs ) + aggr_mods.insert(lhs->GetType().get()); + else + aggr_mods.insert(ad_e->GetType().get()); } break; default: break; diff --git a/src/script_opt/Stmt.cc b/src/script_opt/Stmt.cc index 78605e78d0..9e8945037f 100644 --- a/src/script_opt/Stmt.cc +++ b/src/script_opt/Stmt.cc @@ -255,7 +255,7 @@ StmtPtr IfStmt::DoReduce(Reducer* c) { sl = make_intrusive(red_e_stmt, ThisPtr()); if ( sl ) - return TransformMe(sl, c); + return TransformMe(std::move(sl), c); return ThisPtr(); } @@ -418,14 +418,10 @@ StmtPtr AddDelStmt::DoReduce(Reducer* c) { return ThisPtr(); } - if ( e->Tag() != EXPR_INDEX && e->Tag() != EXPR_FIELD ) - Internal("bad \"add\"/\"delete\""); - auto red_e_stmt = e->ReduceToSingletons(c); if ( red_e_stmt ) return TransformMe(make_intrusive(red_e_stmt, ThisPtr()), c); - else return ThisPtr(); } diff --git a/src/script_opt/ZAM/Expr.cc b/src/script_opt/ZAM/Expr.cc index 7f81b10ee1..cf74cccc4c 100644 --- a/src/script_opt/ZAM/Expr.cc +++ b/src/script_opt/ZAM/Expr.cc @@ -874,7 +874,7 @@ const ZAMStmt ZAMCompiler::Call(const ExprStmt* e) { if ( IsZAM_BuiltIn(c.get()) ) { auto ret = LastInst(); - insts1.back()->call_expr = c; + insts1.back()->call_expr = std::move(c); return ret; } diff --git a/src/script_opt/ZAM/Ops.in b/src/script_opt/ZAM/Ops.in index a4b2130d58..d8c60b7d65 100644 --- a/src/script_opt/ZAM/Ops.in +++ b/src/script_opt/ZAM/Ops.in @@ -2087,6 +2087,17 @@ type VC eval EvalAddStmt(z.c.ToVal(z.t)) +op ClearTable +op1-read +type V +eval frame[z.v1].table_val->RemoveAll(); + +op ClearVector +op1-read +type V +eval frame[z.v1].vector_val->Resize(0); + + op DelTable op1-read type VO diff --git a/src/script_opt/ZAM/Stmt.cc b/src/script_opt/ZAM/Stmt.cc index 9c802afb54..413fbcbc91 100644 --- a/src/script_opt/ZAM/Stmt.cc +++ b/src/script_opt/ZAM/Stmt.cc @@ -591,6 +591,16 @@ const ZAMStmt ZAMCompiler::CompileAdd(const AddStmt* as) { const ZAMStmt ZAMCompiler::CompileDel(const DelStmt* ds) { auto e = ds->StmtExprPtr(); + + if ( e->Tag() == EXPR_NAME ) { + auto n = e->AsNameExpr(); + + if ( n->GetType()->Tag() == TYPE_TABLE ) + return ClearTableV(n); + else + return ClearVectorV(n); + } + auto aggr = e->GetOp1()->AsNameExpr(); if ( e->Tag() == EXPR_FIELD ) {