Skip to content

Commit 577d34f

Browse files
committed
More benchmarks
1 parent 134b1b0 commit 577d34f

7 files changed

Lines changed: 510 additions & 2 deletions

File tree

Benchmarks/benchmark_common.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <benchmark/benchmark.h>
1111

1212
#include <cstdint>
13+
#include <memory>
1314
#include <stdexcept>
1415
#include <string>
1516
#include <string_view>
@@ -42,6 +43,11 @@ struct Counter
4243
{
4344
value = v;
4445
}
46+
47+
static int static_add(int a, int b)
48+
{
49+
return a + b;
50+
}
4551
};
4652

4753
struct Basic
@@ -152,6 +158,16 @@ struct StatefulFunction
152158
}
153159
};
154160

161+
struct SharedObject : std::enable_shared_from_this<SharedObject>
162+
{
163+
double value = kMagicValue;
164+
165+
double get() const
166+
{
167+
return value;
168+
}
169+
};
170+
155171
inline Basic* basic_return()
156172
{
157173
static Basic value{};
@@ -163,6 +179,17 @@ inline double basic_get_var(Basic* b)
163179
return b ? b->var : 0.0;
164180
}
165181

182+
inline std::shared_ptr<SharedObject> shared_object_return()
183+
{
184+
static std::shared_ptr<SharedObject> obj = std::make_shared<SharedObject>();
185+
return obj;
186+
}
187+
188+
inline double shared_object_get_value(std::shared_ptr<SharedObject> obj)
189+
{
190+
return obj ? obj->get() : 0.0;
191+
}
192+
166193
void luaCheckOrThrow(lua_State* L, int status, std::string_view where);
167194
void luaDoStringOrThrow(lua_State* L, std::string_view code, std::string_view where);
168195

Benchmarks/benchmark_luabridge.cpp

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,25 @@ int vanilla_multi_return(lua_State* L)
2121
return 2;
2222
}
2323

24+
void registerBasicGetterSetter(lua_State* L)
25+
{
26+
luabridge::getGlobalNamespace(L)
27+
.beginClass<Basic>("c")
28+
.addConstructor<void (*)()>()
29+
.addProperty("val", &Basic::get, &Basic::set)
30+
.endClass();
31+
}
32+
33+
void registerCounter(lua_State* L)
34+
{
35+
luabridge::getGlobalNamespace(L)
36+
.beginClass<Counter>("Counter")
37+
.addConstructor<void (*)()>()
38+
.addFunction("get", &Counter::get)
39+
.addStaticFunction("static_add", &Counter::static_add)
40+
.endClass();
41+
}
42+
2443
lua_State* makeLua()
2544
{
2645
lua_State* L = luaL_newstate();
@@ -342,6 +361,120 @@ void optional_failure_measure(benchmark::State& state)
342361
benchmark::DoNotOptimize(x);
343362
}
344363

364+
void userdata_variable_write_measure(benchmark::State& state)
365+
{
366+
lua_State* L = makeLua();
367+
registerBasic(L);
368+
luaDoStringOrThrow(L, "b = c()", "vanilla userdata_write setup");
369+
luaDoStringOrThrow(L, "function write_var() b.var = 24.0 end", "vanilla userdata_write closure setup");
370+
371+
for (auto _ : state)
372+
{
373+
(void) _;
374+
lua_getglobal(L, "write_var");
375+
luaCheckOrThrow(L, lua_pcall(L, 0, 0, 0), "vanilla write_var");
376+
}
377+
}
378+
379+
void userdata_property_getter_measure(benchmark::State& state)
380+
{
381+
lua_State* L = makeLua();
382+
registerBasicGetterSetter(L);
383+
luaDoStringOrThrow(L, "b = c()", "vanilla property_getter setup");
384+
luaDoStringOrThrow(L, "function read_getter() return b.val end", "vanilla property_getter closure setup");
385+
386+
for (auto _ : state)
387+
{
388+
(void) _;
389+
lua_getglobal(L, "read_getter");
390+
luaCheckOrThrow(L, lua_pcall(L, 0, 1, 0), "vanilla read_getter");
391+
lua_pop(L, 1);
392+
}
393+
}
394+
395+
void userdata_property_setter_measure(benchmark::State& state)
396+
{
397+
lua_State* L = makeLua();
398+
registerBasicGetterSetter(L);
399+
luaDoStringOrThrow(L, "b = c()", "vanilla property_setter setup");
400+
luaDoStringOrThrow(L, "function write_setter() b.val = 24.0 end", "vanilla property_setter closure setup");
401+
402+
for (auto _ : state)
403+
{
404+
(void) _;
405+
lua_getglobal(L, "write_setter");
406+
luaCheckOrThrow(L, lua_pcall(L, 0, 0, 0), "vanilla write_setter");
407+
}
408+
}
409+
410+
void lambda_capture_measure(benchmark::State& state)
411+
{
412+
lua_State* L = makeLua();
413+
double extra = kMagicValue;
414+
luabridge::getGlobalNamespace(L).addFunction("f", std::function<double(double)>([extra](double v) { return v + extra; }));
415+
luaDoStringOrThrow(L, "function invoke_lambda() return f(24.0) end", "vanilla lambda_capture setup");
416+
417+
for (auto _ : state)
418+
{
419+
(void) _;
420+
lua_getglobal(L, "invoke_lambda");
421+
luaCheckOrThrow(L, lua_pcall(L, 0, 1, 0), "vanilla invoke_lambda");
422+
lua_pop(L, 1);
423+
}
424+
}
425+
426+
void shared_ptr_return_measure(benchmark::State& state)
427+
{
428+
setSkipped(state, "unsupported shared_ptr container in LuaBridge vanilla");
429+
}
430+
431+
void shared_ptr_pass_measure(benchmark::State& state)
432+
{
433+
setSkipped(state, "unsupported shared_ptr container in LuaBridge vanilla");
434+
}
435+
436+
void static_member_function_call_measure(benchmark::State& state)
437+
{
438+
lua_State* L = makeLua();
439+
registerCounter(L);
440+
luaDoStringOrThrow(L, "function invoke_static() return Counter.static_add(10, 32) end", "vanilla static_member_function setup");
441+
442+
for (auto _ : state)
443+
{
444+
(void) _;
445+
lua_getglobal(L, "invoke_static");
446+
luaCheckOrThrow(L, lua_pcall(L, 0, 1, 0), "vanilla invoke_static");
447+
lua_pop(L, 1);
448+
}
449+
}
450+
451+
void derived_method_call_measure(benchmark::State& state)
452+
{
453+
lua_State* L = makeLua();
454+
455+
luabridge::getGlobalNamespace(L)
456+
.beginClass<ComplexBaseA>("ComplexBaseA")
457+
.addFunction("a_func", &ComplexBaseA::a_func)
458+
.addProperty("a", &ComplexBaseA::a)
459+
.endClass()
460+
.deriveClass<ComplexAB, ComplexBaseA>("ComplexAB")
461+
.addConstructor<void (*)()>()
462+
.addFunction("ab_func", &ComplexAB::ab_func)
463+
.addProperty("ab", &ComplexAB::ab)
464+
.endClass();
465+
466+
luaDoStringOrThrow(L, "obj = ComplexAB()", "vanilla derived_method setup");
467+
luaDoStringOrThrow(L, "function call_derived() return obj:ab_func() end", "vanilla derived_method closure setup");
468+
469+
for (auto _ : state)
470+
{
471+
(void) _;
472+
lua_getglobal(L, "call_derived");
473+
luaCheckOrThrow(L, lua_pcall(L, 0, 1, 0), "vanilla call_derived");
474+
lua_pop(L, 1);
475+
}
476+
}
477+
345478
void implicit_inheritance_measure(benchmark::State& state)
346479
{
347480
setSkipped(state, "unsupported for multi inheritance in LuaBridge vanilla");
@@ -371,3 +504,11 @@ BENCHMARK(optional_success_measure)->Name("optional_success_measure");
371504
BENCHMARK(optional_half_failure_measure)->Name("optional_half_failure_measure");
372505
BENCHMARK(optional_failure_measure)->Name("optional_failure_measure");
373506
BENCHMARK(implicit_inheritance_measure)->Name("implicit_inheritance_measure");
507+
BENCHMARK(userdata_variable_write_measure)->Name("userdata_variable_write_measure");
508+
BENCHMARK(userdata_property_getter_measure)->Name("userdata_property_getter_measure");
509+
BENCHMARK(userdata_property_setter_measure)->Name("userdata_property_setter_measure");
510+
BENCHMARK(lambda_capture_measure)->Name("lambda_capture_measure");
511+
BENCHMARK(shared_ptr_return_measure)->Name("shared_ptr_return_measure");
512+
BENCHMARK(shared_ptr_pass_measure)->Name("shared_ptr_pass_measure");
513+
BENCHMARK(static_member_function_call_measure)->Name("static_member_function_call_measure");
514+
BENCHMARK(derived_method_call_measure)->Name("derived_method_call_measure");

0 commit comments

Comments
 (0)