lua-users home
lua-l archive

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

On Tue, Sep 28, 2010 at 5:51 AM, Juris Kalnins <> wrote:
> You have to account for the overhead it will cause for all code
> that doesn't use your const values.
I agree. I estimate a 6 to 10 additional basic native machine codes
operations for each assignment  of a non constant value.
Please, read the next answer.

>> The implementation of selective constants in tables using metamethods
>> requires LUA's ifs (much more time) or a table of hashed constants
>> (much more space and more time).
> I can't imagine it being 25 times
> faster than something like the following simple metatable based
> read only object (just a sketch, this is not a tested code):
> local t = { ... } --
> local read_only = setmetatable({}, { __index = t, __newindex = bark_fn,
> __metatable = bark_fn })

First, CONST patch is not a read-only table implementation. The goal
is to turn chosen  table fields into unchangeable fields. It is
different. I wrote about this before.

When you read the lvm.c code  you see that each VM OPERATION in
interpreted executing many native machine instructions. I estimate
from 60 to 200 per interpreted instruction. A metamethod
implementation will require from 5 to 20 VM OPs. So making
calculations: 60 x 5 = 300 to 200 x 5 = 1000. Considering that CONST
patch requires 6 / 10 extra instructions, calculation again: from
300/10 to 1000/10  results in 30 to 100 times faster. Applying a 20%
discount = 25 times.

About the overhead: When Lua executes a = 1000 command, it generates a
SETGLOBAL VM OP that needs some C language ifs, some comparisons, a
couple of function calls and some assigments to be interpreted. I
estimated to add 2 ifs and 4 masks operation per aassignment. Well, 8
extra instructions in 64 gives 12,5% of overheard on writes. *** I
really need benchmarks to measure the overall performance impact.  ***
This is one of the reasons because I asked for some help. I'm a newbie
in Lua internals ...

>> Login = const(Login) -- Avoids ManInTheMiddle internal attack
> As HyperHacker very well explained in another reply,
> this does not really prevent internal attack.
I didn't see that.

But if you code  " Login = const( function () ... end) " the value
stored inside Login field cannot be changed using normal Lua anymore
and I cannot see a way to alter the original value before storing it
in Login field using regular Lua. I would be confident on value
integrity. And if you make "  Login2 = const(Login) ", Login2 will
store the same unchangeable value too.

Any way, the const(Login) solution would be - at least - as good as
the metamethod solution but it is easier to use. After all, It is a
simple function call.

> Actually, I cannot imagine doing it in Lua at all. The WoW people
> tried to create some form of access policy. And while it works, it's
> neither simple, nor generic.
Well, the Login stuff was just an example and I was talking about
internal (Lua code) "Man in the middle".

> See this for a well thought-out example of
> language-level access control (this one is capability based):
Thanks. I will read this later.