Module:DelayedClone

From Wikipedia

Documentation for this module may be created at Module:DelayedClone/doc

dc = {};

delayedClone = function( parent, seen )
    if type( parent ) ~= 'table' then
        return parent;
    end
    
    local t = {};
    
    seen = seen or {};
    if seen[parent] then
        return seen[parent];
    end
    
    local mt = {
        __index = function( tt, name )
            assert( tt == t );
            delayedLoad(tt, parent, seen);
            return tt[name];
        end,
        __newindex = function( tt, name, value )
            assert( tt == t );
            delayedLoad(tt, parent, seen);
            tt[name] = value;
        end,
        __pairs = function( tt ) 
            assert( tt == t );
            delayedLoad(tt, parent, seen);
            return pairs( tt );
        end,
        __ipairs = function( tt ) 
            assert( tt == t );
            delayedLoad(tt, parent, seen);
            return ipairs( tt );
        end,
    }
        
    t = setmetatable( t, mt );
    seen[parent] = t;
    return t;
end

function delayedLoad(tt, var, seen)
    if var ~= nil then
        for k, v in pairs( var ) do
            rawset( tt, delayedClone( k, seen ), delayedClone( v, seen ) );
        end
    end
    setmetatable( tt, getmetatable( var ) );
end

function dc.test( frame )
    local result = "";
    
    result = "== No extra data ==\n\n"
    local clock1, clock2;
    clock1 = os.clock();
    local t;
    for i = 1,500 do
        t = mw.clone( _G );
        local a = t.mw.text.nowiki( "{{ '''This''' is a nowiki [[test]] }} requiring " );
    end 
    clock2 = os.clock();
    result = result .. t.mw.text.nowiki( "{{ '''This''' is a nowiki [[test]] }} requiring " ) .. (clock2 - clock1)*2 .. ' milliseconds including cloning the environment\n\n';
    
    local clock1, clock2;
    clock1 = os.clock();
    local t2;
    for j = 1,500 do 
        t2 = delayedClone( _G );
        local a = t2.mw.text.nowiki( "{{ '''This''' is a nowiki [[test]] }} requiring " );
    end
    clock2 = os.clock();
    result = result .. t2.mw.text.nowiki( "{{ '''This''' is a nowiki [[test]] }} requiring " ) .. (clock2 - clock1)*2 .. ' miliseconds with a delayed clone environment\n\n';
    
    result = result .. "== Extra data ==\n\n";
    local text = require( 'Module:Citation/CS1/Configuration' );
    local data = require( 'Module:Convertdata' );
    result = result .. "Load Module:Citation/CS1/Configuration\n\n"
    result = result .. "Load Module:Convertdata\n\n";   
    
    local clock1, clock2;
    clock1 = os.clock();
    local t;
    for i = 1,500 do
        t = mw.clone( _G );
        local a = t.mw.text.nowiki( "{{ '''This''' is a nowiki [[test]] }} requiring " );
    end
    clock2 = os.clock();
    result = result .. t.mw.text.nowiki( "{{ '''This''' is a nowiki [[test]] }} requiring " ) .. (clock2 - clock1)*2 .. ' milliseconds including cloning the environment\n\n';
    
    local clock1, clock2;
    clock1 = os.clock();
    local t2;
    for j = 1,500 do 
        t2 = delayedClone( _G );
        local a = t2.mw.text.nowiki( "{{ '''This''' is a nowiki [[test]] }} requiring " );
    end
    clock2 = os.clock();
    result = result .. t2.mw.text.nowiki( "{{ '''This''' is a nowiki [[test]] }} requiring " ) .. (clock2 - clock1)*2 .. ' miliseconds with a delayed clone environment\n\n';
    
    return result;
end    

return dc;