diff options
author | Jacques Lucke <jacques@blender.org> | 2021-03-22 13:57:24 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2021-03-22 14:01:07 +0300 |
commit | 01b6c4b32bf0aa3f2add0d4d51de9f777cf5c51c (patch) | |
tree | 38ea49ab0bfd0cf2ad1b8979819cac658b3db2d1 /source/blender/functions/FN_multi_function_signature.hh | |
parent | ccb372d17c2db88141dc4511550daac024121eb9 (diff) |
Functions: make multi functions smaller and cheaper to construct in many cases
Previously, the signature of a `MultiFunction` was always embedded into the function.
There are two issues with that. First, `MFSignature` is relatively large, because it contains
multiple strings and vectors. Secondly, constructing it can add overhead that should not
be necessary, because often the same signature can be reused.
The solution is to only keep a pointer to a signature in `MultiFunction` that is set during
construction. Child classes are responsible for making sure that the signature lives
long enough. In most cases, the signature is either embedded into the child class or
it is allocated statically (and is only created once).
Diffstat (limited to 'source/blender/functions/FN_multi_function_signature.hh')
-rw-r--r-- | source/blender/functions/FN_multi_function_signature.hh | 39 |
1 files changed, 21 insertions, 18 deletions
diff --git a/source/blender/functions/FN_multi_function_signature.hh b/source/blender/functions/FN_multi_function_signature.hh index e05ea1c2578..23309c9a5e6 100644 --- a/source/blender/functions/FN_multi_function_signature.hh +++ b/source/blender/functions/FN_multi_function_signature.hh @@ -44,18 +44,21 @@ struct MFSignature { class MFSignatureBuilder { private: - MFSignature &data_; + MFSignature signature_; int span_count_ = 0; int virtual_array_count_ = 0; int virtual_vector_array_count_ = 0; int vector_array_count_ = 0; public: - MFSignatureBuilder(MFSignature &data) : data_(data) + MFSignatureBuilder(std::string function_name) { - BLI_assert(data.param_names.is_empty()); - BLI_assert(data.param_types.is_empty()); - BLI_assert(data.param_data_indices.is_empty()); + signature_.function_name = std::move(function_name); + } + + MFSignature build() const + { + return std::move(signature_); } /* Input Parameter Types */ @@ -78,15 +81,15 @@ class MFSignatureBuilder { } void input(StringRef name, MFDataType data_type) { - data_.param_names.append(name); - data_.param_types.append(MFParamType(MFParamType::Input, data_type)); + signature_.param_names.append(name); + signature_.param_types.append(MFParamType(MFParamType::Input, data_type)); switch (data_type.category()) { case MFDataType::Single: - data_.param_data_indices.append(virtual_array_count_++); + signature_.param_data_indices.append(virtual_array_count_++); break; case MFDataType::Vector: - data_.param_data_indices.append(virtual_vector_array_count_++); + signature_.param_data_indices.append(virtual_vector_array_count_++); break; } } @@ -111,15 +114,15 @@ class MFSignatureBuilder { } void output(StringRef name, MFDataType data_type) { - data_.param_names.append(name); - data_.param_types.append(MFParamType(MFParamType::Output, data_type)); + signature_.param_names.append(name); + signature_.param_types.append(MFParamType(MFParamType::Output, data_type)); switch (data_type.category()) { case MFDataType::Single: - data_.param_data_indices.append(span_count_++); + signature_.param_data_indices.append(span_count_++); break; case MFDataType::Vector: - data_.param_data_indices.append(vector_array_count_++); + signature_.param_data_indices.append(vector_array_count_++); break; } } @@ -144,15 +147,15 @@ class MFSignatureBuilder { } void mutable_(StringRef name, MFDataType data_type) { - data_.param_names.append(name); - data_.param_types.append(MFParamType(MFParamType::Mutable, data_type)); + signature_.param_names.append(name); + signature_.param_types.append(MFParamType(MFParamType::Mutable, data_type)); switch (data_type.category()) { case MFDataType::Single: - data_.param_data_indices.append(span_count_++); + signature_.param_data_indices.append(span_count_++); break; case MFDataType::Vector: - data_.param_data_indices.append(vector_array_count_++); + signature_.param_data_indices.append(vector_array_count_++); break; } } @@ -163,7 +166,7 @@ class MFSignatureBuilder { * depend on the fact that the function always performers the same operation. */ void depends_on_context() { - data_.depends_on_context = true; + signature_.depends_on_context = true; } }; |