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.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.hh')
-rw-r--r-- | source/blender/functions/FN_multi_function.hh | 28 |
1 files changed, 17 insertions, 11 deletions
diff --git a/source/blender/functions/FN_multi_function.hh b/source/blender/functions/FN_multi_function.hh index d8924b3cf23..2ce65426245 100644 --- a/source/blender/functions/FN_multi_function.hh +++ b/source/blender/functions/FN_multi_function.hh @@ -53,7 +53,7 @@ namespace blender::fn { class MultiFunction { private: - MFSignature signature_; + const MFSignature *signature_ref_ = nullptr; public: virtual ~MultiFunction() @@ -74,44 +74,50 @@ class MultiFunction { int param_amount() const { - return signature_.param_types.size(); + return signature_ref_->param_types.size(); } IndexRange param_indices() const { - return signature_.param_types.index_range(); + return signature_ref_->param_types.index_range(); } MFParamType param_type(int param_index) const { - return signature_.param_types[param_index]; + return signature_ref_->param_types[param_index]; } StringRefNull param_name(int param_index) const { - return signature_.param_names[param_index]; + return signature_ref_->param_names[param_index]; } StringRefNull name() const { - return signature_.function_name; + return signature_ref_->function_name; } bool depends_on_context() const { - return signature_.depends_on_context; + return signature_ref_->depends_on_context; } const MFSignature &signature() const { - return signature_; + BLI_assert(signature_ref_ != nullptr); + return *signature_ref_; } protected: - MFSignatureBuilder get_builder(std::string function_name) + /* Make the function use the given signature. This should be called once in the constructor of + * child classes. No copy of the signature is made, so the caller has to make sure that the + * signature lives as long as the multi function. It is ok to embed the signature into the child + * class. */ + void set_signature(const MFSignature *signature) { - signature_.function_name = std::move(function_name); - return MFSignatureBuilder(signature_); + /* Take a pointer as argument, so that it is more obvious that no copy is created. */ + BLI_assert(signature != nullptr); + signature_ref_ = signature; } }; |