lua-users home
lua-l archive

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


On Wed, Feb 25, 2009 at 12:20 AM, Rob Hoelz <rob@hoelzro.net> wrote:
> I did a quick, nonreliable test, and it took about 14 times longer with a C
> hook.

Please share your test. My own quick unreliable test (attached) shows
slowdown closer to 1.05x, depending on instruction count, starting
from 100 instructions per hook. Maximum slowdown is 2.37 if hook is
called on every first instruction.

Note that in normal scenario instruction count in the hook is to be
greater than total count of instructions in the script.

Not setting hook
0m11.785s

1 instruction, hook called 700000008 times
0m27.900s

10  instructions, hook called 70000000 times
0m14.323s

100 instructions, hook called 7000000 times
0m12.328s

1000 instructions, hook called 700000 times
0m12.131s

10000 instructions, hook called 70000 times
0m12.181s

100000 instructions, hook called 7000 times
0m12.063s

1000000 instructions, hook called 700 times
0m12.273s

10000000 instructions, hook called 70 times
real	0m12.134s

100000000 instructions, hook called 7 times
0m12.282s

1000000000 instructions, hook called 0 times
0m12.213s

> Also, another problem I thought of: what if the script makes an IO
> call?It'll still block...

Just do not expose blocking IO to untrusted script.

Alexander.
#include <stdio.h>
#include <stdlib.h>

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

int g_counter = 0;

void handler(lua_State * L, lua_Debug * D)
{
  ++g_counter;
}

int main(int argc, char ** argv)
{
  int count = 0;
  int result = 0;
  lua_State * L = lua_open();
  luaL_openlibs(L);
  
  if (argc >= 2)
  {
    count = atoi(argv[1]);
  }
  
  if (count <= 0)
  {
    printf("not setting hook\n");
  }
  else
  {
    printf("setting hook on %d instructions\n", count);
    lua_sethook(L, handler, LUA_MASKCOUNT, count);
  }
  
  printf("running, please wait...\n");
  if (
      luaL_dostring(
          L, 
          "local j = 0\n"
          "local f = function() j = j + 1 end\n"
          "for i = 1, 1e8 do f() end\n"
        ) != 0
    )
  {
    printf("Lua error: %s\n", lua_tostring(L, -1));
    result = 1;
  }
  else
  {
    printf("done, hook called %d times\n", g_counter);
  }
  
  lua_close(L);

  return result;
}