From 582cdc0cf2f3be94d0590510fd7ac9797453c8ea Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Thu, 27 Feb 2025 17:32:51 +0100 Subject: [PATCH] Cleanup: Use Array for FieldInferencingInterface There is no need for amortized growth for the field interface. Array seems slightly better than Vector because it's smaller and doesn't give the impression that the size might change. Pull Request: https://projects.blender.org/blender/blender/pulls/135257 --- .../intern/node_tree_field_inferencing.cc | 31 +++++++++++-------- source/blender/blenlib/BLI_array.hh | 10 ++++++ source/blender/nodes/NOD_node_declaration.hh | 5 +-- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/source/blender/blenkernel/intern/node_tree_field_inferencing.cc b/source/blender/blenkernel/intern/node_tree_field_inferencing.cc index a1ad072fde0..e0a78831cdc 100644 --- a/source/blender/blenkernel/intern/node_tree_field_inferencing.cc +++ b/source/blender/blenkernel/intern/node_tree_field_inferencing.cc @@ -106,10 +106,10 @@ static const FieldInferencingInterface &get_dummy_field_inferencing_interface(co ResourceScope &scope) { auto &inferencing_interface = scope.construct(); - inferencing_interface.inputs.append_n_times(InputSocketFieldType::None, - node.input_sockets().size()); - inferencing_interface.outputs.append_n_times(OutputFieldDependency::ForDataSource(), - node.output_sockets().size()); + inferencing_interface.inputs = Array(node.input_sockets().size(), + InputSocketFieldType::None); + inferencing_interface.outputs = Array( + node.output_sockets().size(), OutputFieldDependency::ForDataSource()); return inferencing_interface; } @@ -140,13 +140,18 @@ static const FieldInferencingInterface &get_node_field_inferencing_interface(con } auto &inferencing_interface = scope.construct(); - for (const bNodeSocket *input_socket : node.input_sockets()) { - inferencing_interface.inputs.append(get_interface_input_field_type(node, *input_socket)); + + const Span input_sockets = node.input_sockets(); + inferencing_interface.inputs.reinitialize(input_sockets.size()); + for (const int i : input_sockets.index_range()) { + inferencing_interface.inputs[i] = get_interface_input_field_type(node, *input_sockets[i]); } - for (const bNodeSocket *output_socket : node.output_sockets()) { - inferencing_interface.outputs.append( - get_interface_output_field_dependency(node, *output_socket)); + const Span output_sockets = node.output_sockets(); + inferencing_interface.outputs.reinitialize(output_sockets.size()); + for (const int i : output_sockets.index_range()) { + inferencing_interface.outputs[i] = get_interface_output_field_dependency(node, + *output_sockets[i]); } return inferencing_interface; } @@ -716,10 +721,10 @@ bool update_field_inferencing(const bNodeTree &tree) /* Create new inferencing interface for this node group. */ std::unique_ptr new_inferencing_interface = std::make_unique(); - new_inferencing_interface->inputs.resize(tree.interface_inputs().size(), - InputSocketFieldType::IsSupported); - new_inferencing_interface->outputs.resize(tree.interface_outputs().size(), - OutputFieldDependency::ForDataSource()); + new_inferencing_interface->inputs = Array( + tree.interface_inputs().size(), InputSocketFieldType::IsSupported); + new_inferencing_interface->outputs = Array( + tree.interface_outputs().size(), OutputFieldDependency::ForDataSource()); /* Keep track of the state of all sockets. The index into this array is #SocketRef::id(). */ Array field_state_by_socket_id(tree.all_sockets().size()); diff --git a/source/blender/blenlib/BLI_array.hh b/source/blender/blenlib/BLI_array.hh index 552b9f5f52d..46a79d86475 100644 --- a/source/blender/blenlib/BLI_array.hh +++ b/source/blender/blenlib/BLI_array.hh @@ -351,6 +351,16 @@ class Array { return IndexRange(size_); } + friend bool operator==(const Array &a, const Array &b) + { + return a.as_span() == b.as_span(); + } + + friend bool operator!=(const Array &a, const Array &b) + { + return !(a == b); + } + /** * Sets the size to zero. This should only be used when you have manually destructed all elements * in the array beforehand. Use with care. diff --git a/source/blender/nodes/NOD_node_declaration.hh b/source/blender/nodes/NOD_node_declaration.hh index ee65c3d297b..8adbaec863d 100644 --- a/source/blender/nodes/NOD_node_declaration.hh +++ b/source/blender/nodes/NOD_node_declaration.hh @@ -8,6 +8,7 @@ #include #include +#include "BLI_array.hh" #include "BLI_string_ref.hh" #include "BLI_utildefines.h" #include "BLI_vector.hh" @@ -80,8 +81,8 @@ class OutputFieldDependency { * Information about how a node interacts with fields. */ struct FieldInferencingInterface { - Vector inputs; - Vector outputs; + Array inputs; + Array outputs; BLI_STRUCT_EQUALITY_OPERATORS_2(FieldInferencingInterface, inputs, outputs) };