Re: Access to parameterized type

  • From: Peter Colberg <peter@xxxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Sun, 2 Sep 2012 16:24:22 -0400

On Sun, Sep 02, 2012 at 06:17:29PM +0100, Justin Cormack wrote:
> When I was experimenting with slice
> (https://github.com/justincormack/slice) I used a Lua table with
> metatable, so I could keep the type hanging around for future use (I
> could have used the data[0] thing but it would fail on a zero length
> array).

The problem with the table and a metatable with __index function is
the performance: For every index access, LuaJIT has to look into
the hash part of the table first, and only then can it fall back
to the __index metamethod.

I had this problem with my one-dimensional array class, and the only
way to get the best performance (equivalent to a raw VLA) was to use
a C struct instead of a table. This meant that besides the array data
pointer and the size no further class members were possible…

It would be great if LuaJIT had some optimisation to defer non-string
key lookups to the __index method immediately, without consulting the
hash part of the table. But maybe this is too specific.

Peter

Other related posts: