lua-users home
lua-l archive

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


On 6 November 2015 at 00:54, Dibyendu Majumdar <mobile@majumdar.org.uk> wrote:
>> On 3 November 2015 at 13:26, Roberto Ierusalimschy
>> <roberto@inf.puc-rio.br> wrote:
>>> Did you consider an inline expansion of short string keys? You could
>>> do a loop unrolling of 'luaH_getstr'. (With 2 steps, I believe you
>>> get a very high percentage of hits.) It is not the same as the
>>> above expansion for integer keys, but it could be a reasonable
>>> compromise.
>>>
>>
>> No I hadn't considered that - thanks for the idea! If the parser knows
>> that the variable is a table (static typing can tell it that) - and
>> the key is a literal short string (which I believe the parser already
>> knows from the fact that it is a constant) - then yes, it should be
>> possible to generate inline code for the initial attempt to access the
>> element - and failing that either try again as you suggest or fallback
>> on calling the function luaH_getstr().
>>
>

I implemented specialized opcodes for scenarios where a local variable
is known to be a table and the key is integer or short string
constant. But that then leads to other possibilities. For instance:

(a friend warned me when I started work on Ravi that I would get
distracted into building the compiler rather than focusing on the
application it was meant for - he was right!)

> t = {}
> t.name = function(t: table) return t.name_ end
> t.name_ = 'dibyendu'
> function x(self: table)
>>   print(self:name())
>> end
> ravi.dumplua(x)

function <stdin:1,3> (6 instructions at 000000960AAE2800)
1 param, 4 slots, 1 upvalue, 1 local, 2 constants, 0 functions
        1       [1]     TOTAB           0
        2       [2]     GETTABUP        1 0 -1  ; _ENV "print"
        3       [2]     SELF_S          2 0 -2
        4       [2]     CALL            2 2 0
        5       [2]     CALL            1 0 1
        6       [3]     RETURN          0 1
constants (2) for 000000960AAE2800:
        1       "print"
        2       "name"
locals (1) for 000000960AAE2800:
        0       self    1       7
upvalues (1) for 000000960AAE2800:
        0       _ENV    0       0
> x(t)
dibyendu