]> granicus.if.org Git - graphviz/commitdiff
return a value instead of pointer from Blocks::totalOrder
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 12 Jun 2021 19:36:31 +0000 (12:36 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 19 Jun 2021 19:24:07 +0000 (12:24 -0700)
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
lib/vpsc/blocks.h
lib/vpsc/solve_VPSC.cpp

index f8b09e5cc8b70b02bb9364c4f48fe8c8ffa8c274..e6f99bfa4bd85b0aaa553235f6df518d936e47c4 100644 (file)
@@ -54,14 +54,14 @@ Blocks::~Blocks()
  * returns a list of variables with total ordering determined by the constraint 
  * DAG
  */
-list<Variable*> *Blocks::totalOrder() {
-       list<Variable*> *order = new list<Variable*>;
+list<Variable*> Blocks::totalOrder() {
+       list<Variable*> order;
        for(int i=0;i<nvs;i++) {
                vs[i]->visited=false;
        }
        for(int i=0;i<nvs;i++) {
                if(vs[i]->in.empty()) {
-                       dfsVisit(vs[i],*order);
+                       dfsVisit(vs[i],order);
                }
        }
        return order;
index b5b5442368aca541984fc3008861634b26e5882b..e39ce81aef82cb25ea349c1130d93be1c4ce0a7a 100644 (file)
@@ -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<Variable*> *totalOrder();
+       std::list<Variable*> totalOrder();
        void cleanup();
        double cost();
 private:
index 10bf69e039e0600cc7406de7ab34f3c37d57b49c..c1ea8c3ea3126c3e24827d248719c565ca510fe8 100644 (file)
@@ -74,8 +74,8 @@ void VPSC::printBlocks() {
 * another so that constraints internal to the block are satisfied.
 */
 void VPSC::satisfy() {
-       list<Variable*> *vs=bs.totalOrder();
-       for(Variable *v : *vs) {
+       list<Variable*> 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() {