lua-users home
lua-l archive

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


It was thus said that the Great Mason Bogue once stated:
> Sean:

  Hey Mason.

> > But if I only use __index and __tostring, that is no longer a Lua
> sequence (since index 2 is missing) and thus, goes into the hash portion.
> This is wrong:
> masonbogue@masons-MacBook-Air ersa % lua
> Lua 5.4.3  Copyright (C) 1994-2021 Lua.org, PUC-Rio
> > t = {1, nil, 3}
> > #t
> 3

  That's assuming you define the metatable as:

mt =
{
  function(t,k)
    -- blah blah
  end,

  nil,

  function(t)
    -- blah blah
  end
}

And yes, under Lua 5.4.3, #mt is indeed 3.  But, a more realistic
construction, where you specify the indecies (because I don't want to have
to memorize that __index is 1, __newindex is 2, __tostring is 3, etc.):

[spc]lucy:~>lua
Lua 5.4.3  Copyright (C) 1994-2021 Lua.org, PUC-Rio
> t = { [1] = 1 , [3] = 3 }
> print(#t)
1

Even specifying [2] as nil won't work:

Lua 5.4.3  Copyright (C) 1994-2021 Lua.org, PUC-Rio
> t = { [1] = 1 , [2] = nil , [3] = 3 }
> print(#t)
1

> Oliver:
> 
> > Did you make a benchmark testing access times for integers and short
> strings?
> 
> Such a benchmark would require a substantial time investment, while someone
> familiar with Lua's implementation may highlight a problem which is not
> performance-dependent. It didn't seem reasonable to spend time putting
> together a patchset if it might be useless anyway, but if no other problems
> are identified in this thread, I might do so.

  To give a point of reference, at work, I have a program written in Lua
that process millions of SIP messages per day (and under a rather strict
deadline) and has been doing so for at least five years now.  Only once did
I have to profile and tweak the code.  Metatables has never been a
performance issue. [1]  Just saying.

  -spc

[1]	I chronicled the performance tweaks:

		http://boston.conman.org/2019/08/20.1
		http://boston.conman.org/2019/08/21.1
		http://boston.conman.org/2019/08/21.2

	The executive sumamry?  It had nothing to do with metatable lookups. 
	It had more to do with parsing.