Skip to content

Commit e1ee405

Browse files
committed
Add new benchmark to measure complex userdata method calls
1 parent f06d002 commit e1ee405

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

benches/benchmark.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,39 @@ fn userdata_call_method(c: &mut Criterion) {
366366
});
367367
}
368368

369+
// A userdata method call that goes through an implicit `__index` function
370+
fn userdata_call_method_complex(c: &mut Criterion) {
371+
struct UserData(u64);
372+
impl LuaUserData for UserData {
373+
fn register(registry: &mut LuaUserDataRegistry<Self>) {
374+
registry.add_field_method_get("val", |_, this| Ok(this.0));
375+
registry.add_method_mut("inc_by", |_, this, by: u64| {
376+
this.0 += by;
377+
Ok(this.0)
378+
});
379+
}
380+
}
381+
382+
let lua = Lua::new();
383+
let ud = lua.create_userdata(UserData(0)).unwrap();
384+
let inc_by = lua
385+
.load("function(ud, s) return ud:inc_by(s) end")
386+
.eval::<LuaFunction>()
387+
.unwrap();
388+
389+
c.bench_function("userdata [call method complex]", |b| {
390+
b.iter_batched(
391+
|| {
392+
collect_gc_twice(&lua);
393+
},
394+
|_| {
395+
inc_by.call::<()>((&ud, 1)).unwrap();
396+
},
397+
BatchSize::SmallInput,
398+
);
399+
});
400+
}
401+
369402
fn userdata_async_call_method(c: &mut Criterion) {
370403
struct UserData(i64);
371404
impl LuaUserData for UserData {
@@ -430,6 +463,7 @@ criterion_group! {
430463
userdata_create,
431464
userdata_call_index,
432465
userdata_call_method,
466+
userdata_call_method_complex,
433467
userdata_async_call_method,
434468
}
435469

0 commit comments

Comments
 (0)