Skip to content

Commit 33c276d

Browse files
committed
Optimize userdata methods call when __index and fields_getters are nil
1 parent 87d027e commit 33c276d

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/util.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -409,11 +409,17 @@ unsafe fn init_userdata_metatable_index(state: *mut ffi::lua_State) -> Result<()
409409
}
410410
ffi::lua_pop(state, 1);
411411

412-
// Create and cache `__index` helper
412+
// Create and cache `__index` generator
413413
let code = cstr!(
414414
r#"
415415
local error, isfunction = ...
416416
return function (__index, field_getters, methods)
417+
-- Fastpath to return methods table for index access
418+
if __index == nil and field_getters == nil then
419+
return methods
420+
end
421+
422+
-- Alternatively return a function for index access
417423
return function (self, key)
418424
if field_getters ~= nil then
419425
local field_getter = field_getters[key]
@@ -463,7 +469,7 @@ pub unsafe fn init_userdata_metatable_newindex(state: *mut ffi::lua_State) -> Re
463469
}
464470
ffi::lua_pop(state, 1);
465471

466-
// Create and cache `__newindex` helper
472+
// Create and cache `__newindex` generator
467473
let code = cstr!(
468474
r#"
469475
local error, isfunction = ...

0 commit comments

Comments
 (0)