[Bf-blender-cvs] [a22763349a2] functions: use static size information to increase performance

Jacques Lucke noreply at git.blender.org
Sun Feb 10 20:25:23 CET 2019


Commit: a22763349a28e9e37f2576a3553eff06a8e7caa7
Author: Jacques Lucke
Date:   Thu Jan 24 18:43:16 2019 +0100
Branches: functions
https://developer.blender.org/rBa22763349a28e9e37f2576a3553eff06a8e7caa7

use static size information to increase performance

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

M	source/blender/blenlib/BLI_small_buffer.hpp
M	source/blender/functions/FN_functions.h
M	source/blender/functions/FN_functions.hpp
M	source/blender/functions/intern/c_wrapper.cpp
M	source/blender/functions/intern/function.cpp
M	source/blender/modifiers/intern/MOD_functiondeform.c

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

diff --git a/source/blender/blenlib/BLI_small_buffer.hpp b/source/blender/blenlib/BLI_small_buffer.hpp
index 34d2745db37..28ddff0c05a 100644
--- a/source/blender/blenlib/BLI_small_buffer.hpp
+++ b/source/blender/blenlib/BLI_small_buffer.hpp
@@ -14,13 +14,27 @@ namespace BLI {
 		SmallBuffer(uint size)
 			: SmallVector<char, N>(size) { }
 
-		void copy_in(uint dst, void *src, uint amount)
+		inline void copy_in(uint dst, void *src, uint amount)
 		{
 			BLI_assert(dst + amount <= this->size());
 			memcpy(this->begin() + dst, src, amount);
 		}
 
-		void copy_out(void *dst, uint src, uint amount) const
+		inline void copy_out(void *dst, uint src, uint amount) const
+		{
+			BLI_assert(src + amount <= this->size());
+			memcpy(dst, this->begin() + src, amount);
+		}
+
+		template<uint amount>
+		inline void copy_in(uint dst, void *src)
+		{
+			BLI_assert(dst + amount <= this->size());
+			memcpy(this->begin() + dst, src, amount);
+		}
+
+		template<uint amount>
+		inline void copy_out(uint dst, void *src) const
 		{
 			BLI_assert(src + amount <= this->size());
 			memcpy(dst, this->begin() + src, amount);
diff --git a/source/blender/functions/FN_functions.h b/source/blender/functions/FN_functions.h
index a0dc893325e..a6be74dcf56 100644
--- a/source/blender/functions/FN_functions.h
+++ b/source/blender/functions/FN_functions.h
@@ -25,8 +25,9 @@ FnInputsRef FN_inputs_new(FunctionRef fn);
 void FN_inputs_free(FnInputsRef fn_in);
 
 /* Set a function input by index. Returns true on success. */
-void FN_inputs_set_index(FnInputsRef fn_in, uint index, void *src);
-
+void FN_inputs_set(FnInputsRef fn_in, uint index, void *src);
+void FN_inputs_set_float(FnInputsRef fn_in, uint index, float value);
+void FN_inputs_set_float_vector_3(FnInputsRef fn_in, uint index, float vector[3]);
 
 /* Create a container to store function outputs. */
 FnOutputsRef FN_outputs_new(FunctionRef fn);
@@ -35,7 +36,8 @@ FnOutputsRef FN_outputs_new(FunctionRef fn);
 void FN_outputs_free(FnOutputsRef fn_out);
 
 /* Extract the result of an executed function by index. */
-void FN_outputs_get_index(FnOutputsRef fn_out, uint index, void *dst);
+void FN_outputs_get(FnOutputsRef fn_out, uint index, void *dst);
+void FN_outputs_get_float_vector_3(FnOutputsRef fn_out, uint index, float dst[3]);
 
 const char *FN_type_name(FnTypeRef type);
 
diff --git a/source/blender/functions/FN_functions.hpp b/source/blender/functions/FN_functions.hpp
index 2ba0fed987b..0c66e6990c2 100644
--- a/source/blender/functions/FN_functions.hpp
+++ b/source/blender/functions/FN_functions.hpp
@@ -31,8 +31,34 @@ namespace FN {
 	public:
 		ValueArray() {};
 		ValueArray(const SmallTypeVector &types);
-		void set(uint index, void *src);
-		void get(uint index, void *dst) const;
+
+		inline void set(uint index, void *src)
+		{
+			BLI_assert(index < this->types.size());
+			uint size = this->offsets[index + 1] - this->offsets[index];
+			this->storage.copy_in(this->offsets[index], src, size);
+		}
+
+		inline void get(uint index, void *dst) const
+		{
+			BLI_assert(index < this->offsets.size());
+			uint size = this->offsets[index + 1] - this->offsets[index];
+			this->storage.copy_out(dst, this->offsets[index], size);
+		}
+
+		template<uint size>
+		inline void set_static(uint index, void *src)
+		{
+			BLI_assert(index < this->types.size());
+			this->storage.copy_in<size>(this->offsets[index], src);
+		}
+
+		template<uint size>
+		inline void get_static(uint index, void *dst) const
+		{
+			BLI_assert(index < this->offsets.size());
+			this->storage.copy_out(dst, this->offsets[index], size);
+		}
 
 	private:
 		const SmallTypeVector types;
@@ -40,32 +66,20 @@ namespace FN {
 		SmallBuffer<> storage;
 	};
 
-	class Inputs {
+	class Inputs : public ValueArray {
 	public:
 		Inputs(const Function &fn);
 
-		inline void set(uint index, void *src)
-		{ this->values.set(index, src); }
-		inline void get(uint index, void *dst) const
-		{ this->values.get(index, dst); }
-
 	private:
 		const Function &fn;
-		ValueArray values;
 	};
 
-	class Outputs {
+	class Outputs : public ValueArray {
 	public:
 		Outputs(const Function &fn);
 
-		inline void set(uint index, void *src)
-		{ this->values.set(index, src); }
-		inline void get(uint index, void *dst) const
-		{ this->values.get(index, dst); }
-
 	private:
 		const Function &fn;
-		ValueArray values;
 	};
 
 	class Signature {
diff --git a/source/blender/functions/intern/c_wrapper.cpp b/source/blender/functions/intern/c_wrapper.cpp
index f6e1b5ca0ff..2c9660db3d6 100644
--- a/source/blender/functions/intern/c_wrapper.cpp
+++ b/source/blender/functions/intern/c_wrapper.cpp
@@ -28,16 +28,31 @@ void FN_outputs_free(FnOutputsRef fn_out)
 	delete (FN::Outputs *)fn_out;
 }
 
-void FN_inputs_set_index(FnInputsRef fn_in, uint index, void *src)
+void FN_inputs_set(FnInputsRef fn_in, uint index, void *src)
 {
 	((FN::Inputs *)fn_in)->set(index, src);
 }
 
-void FN_outputs_get_index(FnOutputsRef fn_out, uint index, void *dst)
+void FN_inputs_set_float(FnInputsRef fn_in, uint index, float value)
+{
+	((FN::Inputs *)fn_in)->set_static<sizeof(float)>(index, (void *)&value);
+}
+
+void FN_inputs_set_float_vector_3(FnInputsRef fn_in, uint index, float value[3])
+{
+	((FN::Inputs *)fn_in)->set_static<sizeof(float) * 3>(index, (void *)value);
+}
+
+void FN_outputs_get(FnOutputsRef fn_out, uint index, void *dst)
 {
 	((FN::Outputs *)fn_out)->get(index, dst);
 }
 
+void FN_outputs_get_float_vector_3(FnOutputsRef fn_out, uint index, float dst[3])
+{
+	((FN::Outputs *)fn_out)->get_static<sizeof(float) * 3>(index, (void *)dst);
+}
+
 const char *FN_type_name(FnTypeRef type)
 {
 	return ((FN::Type *)type)->name().c_str();
@@ -101,8 +116,8 @@ public:
 	{
 		float vec[3];
 		float control;
-		fn_in.get(0, vec);
-		fn_in.get(1, &control);
+		fn_in.get_static<sizeof(float) * 3>(0, (void *)vec);
+		fn_in.get_static<sizeof(float)>(1, (void *)&control);
 
 		float result[3];
 
@@ -110,7 +125,7 @@ public:
 		result[1] = vec[1];
 		result[2] = vec[2];
 
-		fn_out.set(0, result);
+		fn_out.set_static<sizeof(float) * 3>(0, result);
 
 		return true;
 	}
diff --git a/source/blender/functions/intern/function.cpp b/source/blender/functions/intern/function.cpp
index a985b132606..0bd989c28fc 100644
--- a/source/blender/functions/intern/function.cpp
+++ b/source/blender/functions/intern/function.cpp
@@ -13,12 +13,11 @@ const uint Type::size() const
 }
 
 
-
 Inputs::Inputs(const Function &fn)
-	: fn(fn), values(fn.signature().inputs()) { }
+	: ValueArray(fn.signature().inputs()), fn(fn) { }
 
 Outputs::Outputs(const Function &fn)
-	: fn(fn), values(fn.signature().outputs()) { }
+	: ValueArray(fn.signature().outputs()), fn(fn) { }
 
 
 
@@ -34,23 +33,6 @@ ValueArray::ValueArray(const SmallTypeVector &types)
 	this->storage = SmallBuffer<>(total_size);
 }
 
-void ValueArray::set(uint index, void *src)
-{
-	BLI_assert(index < this->types.size());
-	uint size = this->offsets[index + 1] - this->offsets[index];
-	this->storage.copy_in(this->offsets[index], src, size);
-}
-
-void ValueArray::get(uint index, void *dst) const
-{
-	BLI_assert(index < this->offsets.size());
-	this->storage.copy_out(
-		dst,
-		this->offsets[index],
-		this->types[index]->size());
-}
-
-
 Function::~Function()
 {
 }
diff --git a/source/blender/modifiers/intern/MOD_functiondeform.c b/source/blender/modifiers/intern/MOD_functiondeform.c
index 6bfff5f987e..8319dd416dc 100644
--- a/source/blender/modifiers/intern/MOD_functiondeform.c
+++ b/source/blender/modifiers/intern/MOD_functiondeform.c
@@ -61,14 +61,14 @@ static void deformVerts(
 	FnInputsRef fn_in = FN_inputs_new(fn);
 	FnOutputsRef fn_out = FN_outputs_new(fn);
 
-	FN_inputs_set_index(fn_in, 1, &fdmd->control1);
+	FN_inputs_set_float(fn_in, 1, fdmd->control1);
 
 	clock_t start = clock();
 
 	for (int i = 0; i < numVerts; i++) {
-		FN_inputs_set_index(fn_in, 0, vertexCos + i);
+		FN_inputs_set_float_vector_3(fn_in, 0, vertexCos[i]);
 		FN_function_call(fn, fn_in, fn_out);
-		FN_outputs_get_index(fn_out, 0, vertexCos + i);
+		FN_outputs_get_float_vector_3(fn_out, 0, vertexCos[i]);
 	}
 
 	clock_t end = clock();



More information about the Bf-blender-cvs mailing list