lua-users home
lua-l archive

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


The goal is to improve the performance of most of the object oriented
implementations (prototype or class based).
The idea is to prevent rehashing during the object instanciation.
It will done by a new function written in C, in the Table library.
A Lua version could be written :

    function table.proto (t)
        local tp = {}
        for k in pairs(t) do
            tp[k] = nil
        end
        return tp
    end

This function creates only the keys in the new table.
It is not a DeepCopy, ShallowCopy, clone function, ...
because the behaviour of this kind of function depends
on the object model.

A first implementation with Lua API was too slower.
So, I write a second implementation with low level functions.

The following code :
    local p = { a = true, b = true, c = true, d = true, e = true }
    local proto = table.proto
    for i = 1, 1000000 do
        local t = proto(p)
        t.a = i; t.b = i; t.c = i; t.d = i; t.e = i
    end

is 2x faster than :
    for i = 1, 1000000 do
        local t = {}
        t.a = i; t.b = i; t.c = i; t.d = i; t.e = i
    end

and ~10% slower than :
    for i = 1, 1000000 do
        local t = { a = i, b = i, c = i, d = i, e = i }
    end

François Perrad

Attachment: 0001-table.proto.patch
Description: Binary data

Attachment: 0001-table.proto-with-lapi.patch
Description: Binary data