From 4a5c9a1ac521f206b940aca5ab9d06a9db0c1445 Mon Sep 17 00:00:00 2001 From: Matthew Fernandez Date: Sat, 12 Jun 2021 12:36:31 -0700 Subject: [PATCH] return a value instead of pointer from Blocks::totalOrder This function dynamically allocated a list and returned a pointer to this, presumably under the assumption that copying this kind of object during return is expensive. However, Named Return Value Optimization (NRVO) applies to this function. So the compiler can elide the copy during return, making a value cheaper than a heap pointer here. --- lib/vpsc/blocks.cpp | 6 +++--- lib/vpsc/blocks.h | 2 +- lib/vpsc/solve_VPSC.cpp | 5 ++--- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/vpsc/blocks.cpp b/lib/vpsc/blocks.cpp index f8b09e5cc..e6f99bfa4 100644 --- a/lib/vpsc/blocks.cpp +++ b/lib/vpsc/blocks.cpp @@ -54,14 +54,14 @@ Blocks::~Blocks() * returns a list of variables with total ordering determined by the constraint * DAG */ -list *Blocks::totalOrder() { - list *order = new list; +list Blocks::totalOrder() { + list order; for(int i=0;ivisited=false; } for(int i=0;iin.empty()) { - dfsVisit(vs[i],*order); + dfsVisit(vs[i],order); } } return order; diff --git a/lib/vpsc/blocks.h b/lib/vpsc/blocks.h index b5b544236..e39ce81ae 100644 --- a/lib/vpsc/blocks.h +++ b/lib/vpsc/blocks.h @@ -42,7 +42,7 @@ public: void mergeLeft(Block *r); void mergeRight(Block *l); void split(Block *b, Block *&l, Block *&r, Constraint *c); - std::list *totalOrder(); + std::list totalOrder(); void cleanup(); double cost(); private: diff --git a/lib/vpsc/solve_VPSC.cpp b/lib/vpsc/solve_VPSC.cpp index 10bf69e03..c1ea8c3ea 100644 --- a/lib/vpsc/solve_VPSC.cpp +++ b/lib/vpsc/solve_VPSC.cpp @@ -74,8 +74,8 @@ void VPSC::printBlocks() { * another so that constraints internal to the block are satisfied. */ void VPSC::satisfy() { - list *vs=bs.totalOrder(); - for(Variable *v : *vs) { + list vs=bs.totalOrder(); + for(Variable *v : vs) { if(!v->block->deleted) { bs.mergeLeft(v->block); } @@ -91,7 +91,6 @@ void VPSC::satisfy() { throw "Unsatisfied constraint"; } } - delete vs; } void VPSC::refine() { -- 2.40.0