Don't dispatch virtual methods for builtin classes (reduces overhead).
src/libsmoke/smoke.cpp
Thu Feb 18 20:57:00 CET 2010 Tobias Rautenkranz <tobias@rautenkranz.ch>
* Don't dispatch virtual methods for builtin classes (reduces overhead).
--- old-smoke/src/libsmoke/smoke.cpp 2014-10-30 08:06:50.000000000 +0100
+++ new-smoke/src/libsmoke/smoke.cpp 2014-10-30 08:06:50.000000000 +0100
@@ -21,12 +21,15 @@
CL_SMOKE_EXPORT void*
cl_smoke_get_smoke(smoke_binding binding)
{
- return get_smoke_binding(binding)->get_smoke();
+ return static_cast<NoDispatchBinding*>(binding)->get_smoke();
}
/** Creates a new Smoke binding.
* The binding is allocated on the heap an can be freed with smoke_destruct().
+ * When method dispatching is not needed, a null pointer can be passed for @a dispatch.
* @related cl_smoke::Binding
+ * @related cl_smoke::NoDispatchBinding
+ * @related cl_smoke_destruct_binding
* @param smoke pointer to a Smoke module instance
* @param destruct callback for object destruction
* @param dispatch method dispatch callback
@@ -34,24 +37,28 @@
* @return a pointer to a new Smoke binding.
*/
CL_SMOKE_EXPORT smoke_binding
-cl_smoke_init(void* smoke, void* destruct, void* dispatch)
+cl_smoke_construct_binding(void* smoke, void* destruct, void* dispatch)
{
- return new Binding(static_cast<Smoke*>(smoke),
- reinterpret_cast<Binding::destructed>(destruct),
- reinterpret_cast<Binding::dispatch_method>(dispatch));
+ if (NULL == dispatch)
+ return new NoDispatchBinding(static_cast<Smoke*>(smoke),
+ reinterpret_cast<NoDispatchBinding::destructed>(destruct));
+ else
+ return new Binding(static_cast<Smoke*>(smoke),
+ reinterpret_cast<NoDispatchBinding::destructed>(destruct),
+ reinterpret_cast<Binding::dispatch_method>(dispatch));
}
-/** Deletes the smoke binding.
- * @related cl_smoke::Binding
+/** Deletes the Smoke binding.
+ * @related cl_smoke_construct_binding
*/
CL_SMOKE_EXPORT void
-cl_smoke_destruct(smoke_binding binding)
+cl_smoke_destruct_binding(smoke_binding binding)
{
- delete get_smoke_binding(binding)->get_smoke();
- delete get_smoke_binding(binding);
+ // Destructor is virtual; thus we can do this.
+ delete static_cast<SmokeBinding*>(binding);
}
-/** Gets a Smoke modules name.
+/** Gets a Smoke module name.
* @param smoke the Smoke module
*
* @return the module name
@@ -199,8 +206,11 @@
cl_smoke_find_method(Smoke::ModuleIndex* m, void* smoke,
Smoke::Index class_index, const char* method_name)
{
- *m = get_smoke(smoke)->findMethod(get_smoke(smoke)->className(class_index),
- method_name);
+ Q_ASSERT(class_index >= 0 && class_index <= get_smoke(smoke)->numClasses);
+
+ const char* class_name = get_smoke(smoke)->className(class_index);
+ *m = get_smoke(smoke)->findMethod(class_name, method_name);
+
if(m->index > 0)
m->index = m->smoke->methodMaps[m->index].method;
}
@@ -223,7 +233,7 @@
/** Casts an object.
* @param smoke the Smoke module
- * @param object the objec
+ * @param object the object
* @param from the class index of @a object
* @param to the class index to cast to
*