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).
This commit is contained in:
@@ -23,10 +23,12 @@ namespace blender::fn {
|
||||
CustomMF_GenericConstant::CustomMF_GenericConstant(const CPPType &type, const void *value)
|
||||
: type_(type), value_(value)
|
||||
{
|
||||
MFSignatureBuilder signature = this->get_builder("Constant " + type.name());
|
||||
MFSignatureBuilder signature{"Constant " + type.name()};
|
||||
std::stringstream ss;
|
||||
type.debug_print(value, ss);
|
||||
signature.single_output(ss.str(), type);
|
||||
signature_ = signature.build();
|
||||
this->set_signature(&signature_);
|
||||
}
|
||||
|
||||
void CustomMF_GenericConstant::call(IndexMask mask,
|
||||
@@ -73,8 +75,10 @@ static std::string gspan_to_string(GSpan array)
|
||||
CustomMF_GenericConstantArray::CustomMF_GenericConstantArray(GSpan array) : array_(array)
|
||||
{
|
||||
const CPPType &type = array.type();
|
||||
MFSignatureBuilder signature = this->get_builder("Constant " + type.name() + " Vector");
|
||||
MFSignatureBuilder signature{"Constant " + type.name() + " Vector"};
|
||||
signature.vector_output(gspan_to_string(array), type);
|
||||
signature_ = signature.build();
|
||||
this->set_signature(&signature_);
|
||||
}
|
||||
|
||||
void CustomMF_GenericConstantArray::call(IndexMask mask,
|
||||
@@ -92,13 +96,15 @@ CustomMF_DefaultOutput::CustomMF_DefaultOutput(StringRef name,
|
||||
Span<MFDataType> output_types)
|
||||
: output_amount_(output_types.size())
|
||||
{
|
||||
MFSignatureBuilder signature = this->get_builder(name);
|
||||
MFSignatureBuilder signature{name};
|
||||
for (MFDataType data_type : input_types) {
|
||||
signature.input("Input", data_type);
|
||||
}
|
||||
for (MFDataType data_type : output_types) {
|
||||
signature.output("Output", data_type);
|
||||
}
|
||||
signature_ = signature.build();
|
||||
this->set_signature(&signature_);
|
||||
}
|
||||
void CustomMF_DefaultOutput::call(IndexMask mask, MFParams params, MFContext UNUSED(context)) const
|
||||
{
|
||||
|
Reference in New Issue
Block a user