skip to Main Content

According to this Lua optimization doc: https://www.lua.org/gems/sample.pdf

Access to external locals (that is, variables that are local to an
enclosing function) is not as fast as access to local variables, but
it is still faster than access to globals. Consider the next fragment:

function foo (x)
  for i = 1, 1000000 do
    x = x + math.sin(i)
  end
  return x
end
print(foo(10))

We can optimize it by declaring sin once, outside function foo: local

sin = math.sin
function foo (x)
  for i = 1, 1000000 do
    x = x + sin(i)
  end
  return x
end
print(foo(10))

Is redis.call a global? Can we optimize it by delcaring a local variable pointing to it? Especially for tight loops that call redis.call many times.

And as a follow up are KEYS and ARGV also globals?

2

Answers


  1. Chosen as BEST ANSWER

    So the following three Redis commands all execute successfully.

    1. eval "return _G.redis.call('set', 'x', 1)" 0

    2. eval "return _G.KEYS" 1 1

    3. eval "return _G.ARGV" 0 1

    So I'm guessing they're all globals. Please someone correct me if I'm wrong...


  2. Is redis.call a global?

    And as a follow up are KEYS and ARGV also globals?

    Yes, redis, KEYS and ARGV are globals.

    Can we optimize it by delcaring a local variable pointing to it? Especially for tight loops that call redis.call many times.

    According to your reference, it seems you can optimize the Lua code with local vars. However, normally you should not run too many operations in Lua script, e.g. run a large loop and execute many redis commands. Because Redis is single-threaded, if Lua script takes too much time, it will block Redis.

    Normally, I’d like to assign elements in KEYS and ARGV array, to local vars. Not for efficiency, but for code clarity.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search