lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Mike Pall <mikelu-1102 <at> mike.de> writes:
> Josh Haberman wrote:
> > Any light you can shed on the core issue (whether __index and __newindex
> > that look up a method can be as efficient as straight method calls) would be
> > most helpful!
> 
> Wrapper objects always involve a certain overhead.

You've made me realize that my question was not specific enough.
Both options I am considering have the same amount of wrapping.
The only difference is the dispatch.

Here is a more specific description of the two options I am
considering.  My question is whether my explicit table lookup
in option #1 is somehow harder to optimize than then the
automatic __index table lookup in option #2.

(Benchmarking is good, but I'd like to avoid having to completely
build both if we can determine a priori how they would compare).

  -- In both cases I have the following userdata:
  -- typedef struct {
  --   // A reference to my actual data, since these objects are refcounted
  --   // inside my library.
  --   my_obj *obj;
  -- } my_userdata_t;
  --
  -- And I have this implementation of set_foo:
  local tostruct = ffi.typeof("my_userdata_t *")
  function set_foo_impl(obj, val)
    -- "obj" is a "my_userdata"
    local my_obj = tostruct(obj).obj
    my_obj.has_foo = true
    my_obj.foo = val
  end

  -- ==== The two cases I am trying to compare: ====

  -- Logically performs:
  --  getmetatable(my_obj).__newindex(my_obj, "foo", 5)
  --
  -- Where my "__newindex" implementation is:
  --  local methods = { foo = set_foo_impl }
  --  function my_newindex(obj, key, val)
  --    methods[key](obj, val)
  --  end
  my_obj.foo = 5

  -- Logically performs:
  --  getmetatable(my_obj).__index["set_foo"](my_obj, 5)
  --
  -- Where my "__index" is a table:
  --  local my_index = { set_foo = set_foo_impl }
  my_obj:set_foo(5)

Josh