lua-users home
lua-l archive

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

> Will it be slower than your method?

I doubt it. It might be faster. It is perhaps less general, but if it is
what you need, go for it.

It is basically the same as the first example I wrote (now resting
comfortably at <>) which
requires you to know the names of the globals you want to bind. The one I
wrote allows for more than one setcvar/getcvar which would only be useful
if you had more than one type of cvar. There are a lot of lines in what I
wrote, but if you look carefully, the end result is pretty well the same

The second one was more in response to the complaint that you cannot
dispatch based on value-type. You can, and
<> demonstrates how. It is
quite a different interface, though; I don't think they really solve the
same problem.

The second one is probably slower, although I haven't benchmarked anything.
Frankly, I doubt whether the overhead of either of them would be noticeable
in real-world examples.

I think the environment variables case shows the difference between the two
approaches. In some circumstances (CGI scripts for example) it might be
useful to make particular environment variables correspond to Lua globals,
so there would be a small documented set of such things (much like your
need, I think).

In BoundScalarGlobalsTwo, the name of the global is irrelevant. For
example, you can write:

whoami = Environment "USER"

to bind any global to any environment variable. (I didn't write the example
code but hopefully it is obvious how; in any event, the functions would
probably be written in C rather than Lua.) That has advantages, too; for
example if you are writing for both Unix and Windows NT, you want to do
some translation of environment variable names (USER / USERNAME; OS /
OSTYPE; etc.) and a line like the one above is better.

However, I still don't like it because it only works on the globals table
and at some point you are going to want to change it to::

env.whoami = Environment "USER"

which won't work.

In the end, it is much more straightforward to create an environment table
whose keys correspond to all environment variables, and I think that is a
cleaner implementation. Binding particular ones to Lua globals saves on
typing, though; some people care about that.

Anyway, thanks for giving me the chance to think about metamethods. It was

> BTW I meant to ask you guys. How are table lookups implemented in lua.

They are a well-tuned hashtable.

> I was thinking wether it would be (significantly) faster to store
> the info about cvars in an internal C hash table for the lookups.

Lua hashtable lookups are very fast. I'd be very surprised if you found
a hash table implementation that was significantly faster.

> Would there be any difference? I don't think so.

Me neither.