Skip to content
This repository was archived by the owner on Dec 17, 2018. It is now read-only.
This repository was archived by the owner on Dec 17, 2018. It is now read-only.

'Lua.refs' grows unbounded when executing function on js object #61

@t-mw

Description

@t-mw

I'm experimenting with using lua.vm.js to run scripts multiple times per second (game application). The scripts execute functions stored on javascript objects, but this seems to cause LuaVM.Lua.refs to grow larger after each script iteration, which affects performance.

Although the number of non-null elements in LuaVM.Lua.refs remains constant, since the array grows larger and larger, the time taken to execute indexOf here becomes increasingly long.

I've prepared a reproduction below, demonstrating how the length of LuaVM.Lua.refs increases proportionally to the number of script iterations, and that this increases the time required to execute subsequent scripts. If my approach to calling functions outside of the vm could be improved, I'd appreciate any advice.

var LuaVM = require("lua.vm.js");

var vm = new LuaVM.Lua.State();


var obj = {
    test1: function() {
        return 0;
	},
    test2: function() {
        return 0;
    },
    test3: function() {
        return 0;
    },
    test4: function() {
        return 0;
    }
};

vm.execute("obj = ...", obj);
for (var i = 0; i < 1000; i++) {
    vm.execute("return obj.test1()");
    vm.execute("return obj.test2()");
    vm.execute("return obj.test3()");
    vm.execute("return obj.test4()");
}

console.log("1000 iterations - ref count: " + LuaVM.Lua.refs[vm._L].length);

benchmark("benchmark", function() {
    vm.execute("return obj.test1()");
});

for (var i = 0; i < 10000; i++) {
    vm.execute("return obj.test1()");
    vm.execute("return obj.test2()");
    vm.execute("return obj.test3()");
    vm.execute("return obj.test4()");
}

console.log("20000 iterations - ref count: " + LuaVM.Lua.refs[vm._L].length);

benchmark("benchmark", function() {
    vm.execute("return obj.test1()");
});

function benchmark(desc, fun) {
    console.time(desc);
    for (var i = 0; i < 10000; i++) {
        fun();
    }
    console.timeEnd(desc);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions