[Bf-blender-cvs] [7984eca] depsgraph_cleanup: Depsgraph: Fallback to plain array for operations once depsgraph was fully built

Sergey Sharybin noreply at git.blender.org
Fri May 27 17:59:30 CEST 2016


Commit: 7984eca71781b83d9e3a8be902d964c3e8f87b8c
Author: Sergey Sharybin
Date:   Fri May 27 17:57:03 2016 +0200
Branches: depsgraph_cleanup
https://developer.blender.org/rB7984eca71781b83d9e3a8be902d964c3e8f87b8c

Depsgraph: Fallback to plain array for operations once depsgraph was fully built

Storing dozen operations in a map is not really efficient for traversal and
looping (which happens A LOT) during graph evaluation. So let's sue map during
building process to speed up some lookups but once graph was finalized we are
now switching to an array for operations.

This solves the speed regression caused by switch to a native BLI utilities
and also gives few percent extra speedup comparing to master. So it's a win.

===================================================================

M	source/blender/depsgraph/intern/builder/deg_builder.cc
M	source/blender/depsgraph/intern/debug/deg_debug_graphviz.cc
M	source/blender/depsgraph/intern/depsgraph_debug.cc
M	source/blender/depsgraph/intern/eval/deg_eval_flush.cc
M	source/blender/depsgraph/intern/nodes/deg_node.cc
M	source/blender/depsgraph/intern/nodes/deg_node.h
M	source/blender/depsgraph/intern/nodes/deg_node_component.cc
M	source/blender/depsgraph/intern/nodes/deg_node_component.h

===================================================================

diff --git a/source/blender/depsgraph/intern/builder/deg_builder.cc b/source/blender/depsgraph/intern/builder/deg_builder.cc
index 4ed8352..cf9b0ce 100644
--- a/source/blender/depsgraph/intern/builder/deg_builder.cc
+++ b/source/blender/depsgraph/intern/builder/deg_builder.cc
@@ -117,6 +117,7 @@ void deg_graph_build_finalize(Depsgraph *graph)
 			id_node->tag_update(graph);
 			id->tag &= ~LIB_TAG_DOIT;
 		}
+		id_node->finalize_build();
 	}
 	GHASH_FOREACH_END();
 }
diff --git a/source/blender/depsgraph/intern/debug/deg_debug_graphviz.cc b/source/blender/depsgraph/intern/debug/deg_debug_graphviz.cc
index c49387b..6e932d3 100644
--- a/source/blender/depsgraph/intern/debug/deg_debug_graphviz.cc
+++ b/source/blender/depsgraph/intern/debug/deg_debug_graphviz.cc
@@ -404,12 +404,10 @@ static void deg_debug_graphviz_node(const DebugContext &ctx,
 		case DEPSNODE_TYPE_EVAL_PARTICLES:
 		{
 			ComponentDepsNode *comp_node = (ComponentDepsNode *)node;
-			if (BLI_ghash_size(comp_node->operations) > 0) {
-				GHASH_FOREACH_BEGIN(const DepsNode *, op_node, comp_node->operations)
-				{
+			if (!comp_node->operations.empty()) {
+				foreach (DepsNode *op_node, comp_node->operations) {
 					deg_debug_graphviz_node(ctx, op_node);
 				}
-				GHASH_FOREACH_END();
 				deg_debug_graphviz_node_cluster_end(ctx);
 			}
 			else {
@@ -446,7 +444,7 @@ static bool deg_debug_graphviz_is_cluster(const DepsNode *node)
 		case DEPSNODE_TYPE_BONE:
 		{
 			ComponentDepsNode *comp_node = (ComponentDepsNode *)node;
-			return BLI_ghash_size(comp_node->operations) > 0;
+			return !comp_node->operations.empty();
 		}
 		default:
 			return false;
@@ -537,11 +535,9 @@ static void deg_debug_graphviz_graph_relations(const DebugContext &ctx,
 	{
 		GHASH_FOREACH_BEGIN(ComponentDepsNode *, comp_node, id_node->components)
 		{
-			GHASH_FOREACH_BEGIN(OperationDepsNode *, op_node, comp_node->operations)
-			{
+			foreach (OperationDepsNode *op_node, comp_node->operations) {
 				deg_debug_graphviz_node_relations(ctx, op_node);
 			}
-			GHASH_FOREACH_END();
 		}
 		GHASH_FOREACH_END();
 	}
diff --git a/source/blender/depsgraph/intern/depsgraph_debug.cc b/source/blender/depsgraph/intern/depsgraph_debug.cc
index 1be70ad..d3b4893 100644
--- a/source/blender/depsgraph/intern/depsgraph_debug.cc
+++ b/source/blender/depsgraph/intern/depsgraph_debug.cc
@@ -224,11 +224,9 @@ void DEG_stats_simple(const Depsgraph *graph, size_t *r_outer,
 			GHASH_FOREACH_BEGIN(DEG::ComponentDepsNode *, comp_node, id_node->components)
 			{
 				tot_outer++;
-				GHASH_FOREACH_BEGIN(DEG::OperationDepsNode *, op_node, comp_node->operations)
-				{
+				foreach (DEG::OperationDepsNode *op_node, comp_node->operations) {
 					tot_rels += op_node->inlinks.size();
 				}
-				GHASH_FOREACH_END();
 			}
 			GHASH_FOREACH_END();
 		}
diff --git a/source/blender/depsgraph/intern/eval/deg_eval_flush.cc b/source/blender/depsgraph/intern/eval/deg_eval_flush.cc
index 7883847..8731382 100644
--- a/source/blender/depsgraph/intern/eval/deg_eval_flush.cc
+++ b/source/blender/depsgraph/intern/eval/deg_eval_flush.cc
@@ -181,11 +181,9 @@ void deg_graph_flush_updates(Main *bmain, Depsgraph *graph)
 		 */
 		ComponentDepsNode *component = node->owner;
 		if ((component->flags & DEPSCOMP_FULLY_SCHEDULED) == 0) {
-			GHASH_FOREACH_BEGIN(OperationDepsNode *, op, component->operations)
-			{
+			foreach (OperationDepsNode *op, component->operations) {
 				op->flag |= DEPSOP_FLAG_NEEDS_UPDATE;
 			}
-			GHASH_FOREACH_END();
 			component->flags |= DEPSCOMP_FULLY_SCHEDULED;
 		}
 	}
diff --git a/source/blender/depsgraph/intern/nodes/deg_node.cc b/source/blender/depsgraph/intern/nodes/deg_node.cc
index 5102ffd..78293f7 100644
--- a/source/blender/depsgraph/intern/nodes/deg_node.cc
+++ b/source/blender/depsgraph/intern/nodes/deg_node.cc
@@ -261,6 +261,15 @@ void IDDepsNode::tag_update(Depsgraph *graph)
 	GHASH_FOREACH_END();
 }
 
+void IDDepsNode::finalize_build()
+{
+	GHASH_FOREACH_BEGIN(ComponentDepsNode *, comp_node, components)
+	{
+		comp_node->finalize_build();
+	}
+	GHASH_FOREACH_END();
+}
+
 DEG_DEPSNODE_DEFINE(IDDepsNode, DEPSNODE_TYPE_ID_REF, "ID Node");
 static DepsNodeFactoryImpl<IDDepsNode> DNTI_ID_REF;
 
diff --git a/source/blender/depsgraph/intern/nodes/deg_node.h b/source/blender/depsgraph/intern/nodes/deg_node.h
index d7a36d2..d79d3d2 100644
--- a/source/blender/depsgraph/intern/nodes/deg_node.h
+++ b/source/blender/depsgraph/intern/nodes/deg_node.h
@@ -167,6 +167,8 @@ struct IDDepsNode : public DepsNode {
 
 	void tag_update(Depsgraph *graph);
 
+	void finalize_build();
+
 	/* ID Block referenced. */
 	ID *id;
 
diff --git a/source/blender/depsgraph/intern/nodes/deg_node_component.cc b/source/blender/depsgraph/intern/nodes/deg_node_component.cc
index 1e0da6d..d18047c 100644
--- a/source/blender/depsgraph/intern/nodes/deg_node_component.cc
+++ b/source/blender/depsgraph/intern/nodes/deg_node_component.cc
@@ -88,9 +88,9 @@ ComponentDepsNode::ComponentDepsNode() :
     exit_operation(NULL),
     flags(0)
 {
-	operations = BLI_ghash_new(comp_node_hash_key,
-	                           comp_node_hash_key_cmp,
-	                           "Depsgraph id hash");
+	operations_map = BLI_ghash_new(comp_node_hash_key,
+	                               comp_node_hash_key_cmp,
+	                               "Depsgraph id hash");
 }
 
 /* Initialize 'component' node - from pointer data given */
@@ -105,7 +105,11 @@ void ComponentDepsNode::init(const ID * /*id*/,
 ComponentDepsNode::~ComponentDepsNode()
 {
 	clear_operations();
-	BLI_ghash_free(operations, comp_node_hash_key_free, NULL);
+	if (operations_map != NULL) {
+		BLI_ghash_free(operations_map,
+		               comp_node_hash_key_free,
+		               comp_node_hash_value_free);
+	}
 }
 
 string ComponentDepsNode::identifier() const
@@ -120,7 +124,7 @@ string ComponentDepsNode::identifier() const
 
 OperationDepsNode *ComponentDepsNode::find_operation(OperationIDKey key) const
 {
-	OperationDepsNode *node = reinterpret_cast<OperationDepsNode *>(BLI_ghash_lookup(operations, &key));
+	OperationDepsNode *node = reinterpret_cast<OperationDepsNode *>(BLI_ghash_lookup(operations_map, &key));
 	if (node != NULL) {
 		return node;
 	}
@@ -140,7 +144,7 @@ OperationDepsNode *ComponentDepsNode::find_operation(eDepsOperation_Code opcode,
 
 OperationDepsNode *ComponentDepsNode::has_operation(OperationIDKey key) const
 {
-	return reinterpret_cast<OperationDepsNode *>(BLI_ghash_lookup(operations, &key));
+	return reinterpret_cast<OperationDepsNode *>(BLI_ghash_lookup(operations_map, &key));
 }
 
 OperationDepsNode *ComponentDepsNode::has_operation(eDepsOperation_Code opcode,
@@ -159,7 +163,7 @@ OperationDepsNode *ComponentDepsNode::add_operation(eDepsOperation_Type optype,
 
 		/* register opnode in this component's operation set */
 		OperationIDKey *key = OBJECT_GUARDED_NEW(OperationIDKey, opcode, name);
-		BLI_ghash_insert(operations, key, op_node);
+		BLI_ghash_insert(operations_map, key, op_node);
 
 		/* set as entry/exit node of component (if appropriate) */
 		if (optype == DEPSOP_TYPE_INIT) {
@@ -192,21 +196,25 @@ OperationDepsNode *ComponentDepsNode::add_operation(eDepsOperation_Type optype,
 
 void ComponentDepsNode::remove_operation(eDepsOperation_Code opcode, const string &name)
 {
-	OperationDepsNode *op_node = find_operation(opcode, name);
-	if (op_node) {
-		/* unregister */
-		OperationIDKey key(opcode, name);
-		BLI_ghash_remove(operations, &key,
-		                 comp_node_hash_key_free,
-		                 comp_node_hash_key_free);
-	}
+	/* unregister */
+	OperationIDKey key(opcode, name);
+	BLI_ghash_remove(operations_map,
+	                 &key,
+	                 comp_node_hash_key_free,
+	                 comp_node_hash_key_free);
 }
 
 void ComponentDepsNode::clear_operations()
 {
-	BLI_ghash_clear(operations,
-	                comp_node_hash_key_free,
-	                comp_node_hash_value_free);
+	if (operations_map != NULL) {
+		BLI_ghash_clear(operations_map,
+		                comp_node_hash_key_free,
+		                comp_node_hash_value_free);
+	}
+	foreach (OperationDepsNode *op_node, operations) {
+		OBJECT_GUARDED_DELETE(op_node, OperationDepsNode);
+	}
+	operations.clear();
 }
 
 void ComponentDepsNode::tag_update(Depsgraph *graph)
@@ -215,11 +223,9 @@ void ComponentDepsNode::tag_update(Depsgraph *graph)
 	if (entry_op != NULL && entry_op->flag & DEPSOP_FLAG_NEEDS_UPDATE) {
 		return;
 	}
-	GHASH_FOREACH_BEGIN(OperationDepsNode *, op_node, operations)
-	{
+	foreach (OperationDepsNode *op_node, operations) {
 		op_node->tag_update(graph);
 	}
-	GHASH_FOREACH_END();
 }
 
 OperationDepsNode *ComponentDepsNode::get_entry_operation()
@@ -227,10 +233,11 @@ OperationDepsNode *ComponentDepsNode::get_entry_operation()
 	if (entry_operation) {
 		return entry_operation;
 	}
-	else if (BLI_ghash_size(operations) == 1) {
+	else if (operations_map != NULL && BLI_ghash_size(operations_map) == 1) {
 		OperationDepsNode *op_node = NULL;
 		/* TODO(sergey): This is somewhat slow. */
-		GHASH_FOREACH_BEGIN(OperationDepsNode *, tmp, operations) {
+		GHASH_FOREACH_BEGIN(OperationDepsNode *, tmp, operations_map)
+		{
 			op_node = tmp;
 		}
 		GHASH_FOREACH_END();
@@ -238,6 +245,9 @@ OperationDepsNode *ComponentDepsNode::get_entry_operation()
 		entry_operation = op_node;
 		return op_node;
 	}
+	else if(operations.size() == 1) {
+		return operations[0];
+	}
 	return NULL;
 }
 
@@ -246,10 +256,11 @@ OperationDepsNode *ComponentDepsNode::get_exit_operation()
 	if (exit_operation) {
 		return exit_operation;
 	}
-	else if (BLI_ghash_size(operations) == 1) {
+	else if (operations_map != NULL && BLI_ghash_size(operations_map) == 1) {
 		OperationDepsNode *op_node = NULL;
 		/* TODO(sergey): This is somewhat slow. */
-		GHASH_FOREACH_BEGIN(OperationDepsNode *, tmp, operations) {
+		GHASH_FOREACH_BEGIN(OperationDepsNode *, tmp, operations_map)
+		{
 			op_node = tmp;
 		}
 		GHASH_FOREACH_END();
@@ -257,9 +268,26 @@ OperationDepsNode *ComponentDepsNode::get_exit_operation()
 		exit_operation = op_node;
 		return op_node;
 	}
+	else if(operations.size() == 1) {
+		return operations[0];
+	}
 	return NULL;
 }
 
+void ComponentDepsNode::finalize_build()
+{
+	operations.reser

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list