Skip to content

Commit c30d65e

Browse files
committed
Make keys and argv as native lua tables
1 parent f75773c commit c30d65e

File tree

3 files changed

+49
-30
lines changed

3 files changed

+49
-30
lines changed

benchmark/BDN.benchmark/Resp/RespLuaRunnerStress.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace BDN.benchmark.Resp
1010
public unsafe class RespLuaRunnerStress
1111
{
1212
LuaRunner r1, r2, r3, r4;
13-
readonly string[] keys = ["key1", "key2"];
13+
readonly string[] keys = ["key1"];
1414

1515
[GlobalSetup]
1616
public void GlobalSetup()

libs/server/Lua/LuaRunner.cs

+47-28
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ internal sealed class LuaRunner : IDisposable
2626
readonly TxnKeyEntries txnKeyEntries;
2727
readonly bool txnMode;
2828

29+
LuaTable keyTable, argvTable;
30+
int keyLength, argvLength;
31+
2932
/// <summary>
3033
/// Creates a new runner with the source of the script
3134
/// </summary>
@@ -61,6 +64,8 @@ function redis.status_reply(text)
6164
function redis.error_reply(text)
6265
return { err = text }
6366
end
67+
KEYS = {}
68+
ARGV = {}
6469
sandbox_env = {
6570
tostring = tostring;
6671
next = next;
@@ -81,13 +86,17 @@ function redis.error_reply(text)
8186
math = math;
8287
table = table;
8388
string = string;
89+
KEYS = KEYS;
90+
ARGV = ARGV;
8491
}
8592
function load_sandboxed(source)
8693
if (not source) then return nil end
8794
return load(source, nil, nil, sandbox_env)
8895
end
8996
");
9097
sandbox_env = (LuaTable)state["sandbox_env"];
98+
keyTable = (LuaTable)state["KEYS"];
99+
argvTable = (LuaTable)state["ARGV"];
91100
}
92101

93102
/// <summary>
@@ -252,7 +261,7 @@ public object Run(int count, SessionParseState parseState)
252261
if (nKeys > 0)
253262
{
254263
// Lua uses 1-based indexing, so we allocate an extra entry in the array
255-
keys = new string[nKeys + 2];
264+
keys = new string[nKeys];
256265
for (int i = 0; i < nKeys; i++)
257266
{
258267
if (txnMode)
@@ -262,9 +271,8 @@ public object Run(int count, SessionParseState parseState)
262271
if (!respServerSession.storageSession.objectStoreLockableContext.IsNull)
263272
txnKeyEntries.AddKey(key, true, Tsavorite.core.LockType.Exclusive);
264273
}
265-
keys[i + 1] = parseState.GetString(offset++);
274+
keys[i] = parseState.GetString(offset++);
266275
}
267-
keys[nKeys + 1] = null;
268276
count -= nKeys;
269277

270278
//TODO: handle slot verification for Lua script keys
@@ -278,12 +286,11 @@ public object Run(int count, SessionParseState parseState)
278286
if (count > 0)
279287
{
280288
// Lua uses 1-based indexing, so we allocate an extra entry in the array
281-
argv = new string[count + 2];
289+
argv = new string[count];
282290
for (int i = 0; i < count; i++)
283291
{
284-
argv[i + 1] = parseState.GetString(offset++);
292+
argv[i] = parseState.GetString(offset++);
285293
}
286-
argv[count + 1] = null;
287294
}
288295

289296
if (txnMode && nKeys > 0)
@@ -344,40 +351,52 @@ object RunTransactionInternal(string[] keys, string[] argv)
344351

345352
object RunInternal(string[] keys, string[] argv)
346353
{
347-
if (keys != this.keys)
354+
if (keys != null)
348355
{
349-
if (keys == null)
350-
{
351-
this.keys = null;
352-
sandbox_env["KEYS"] = this.keys;
353-
}
354-
else
356+
if (keyLength != keys.Length)
355357
{
356-
if (this.keys != null && keys.Length == this.keys.Length)
357-
Array.Copy(keys, this.keys, keys.Length);
358-
else
358+
if (keyLength > 0)
359359
{
360-
this.keys = keys;
361-
sandbox_env["KEYS"] = this.keys;
360+
keyTable.Dispose();
361+
keyTable = (LuaTable)state.DoString("return {}")[0];
362+
sandbox_env["KEYS"] = keyTable;
362363
}
364+
keyLength = keys.Length;
363365
}
366+
for (int i = 0; i < keys.Length; i++)
367+
keyTable[i + 1] = keys[i];
364368
}
365-
if (argv != this.argv)
369+
else
366370
{
367-
if (argv == null)
371+
if (keyLength > 0)
368372
{
369-
this.argv = null;
370-
sandbox_env["ARGV"] = this.argv;
373+
keyTable = (LuaTable)state.DoString("return {}")[0];
374+
sandbox_env["KEYS"] = keyTable;
375+
keyLength = 0;
371376
}
372-
else
377+
}
378+
if (argv != null)
379+
{
380+
if (argvLength != argv.Length)
373381
{
374-
if (this.argv != null && argv.Length == this.argv.Length)
375-
Array.Copy(argv, this.argv, argv.Length);
376-
else
382+
if (argvLength > 0)
377383
{
378-
this.argv = argv;
379-
sandbox_env["ARGV"] = this.argv;
384+
argvTable.Dispose();
385+
argvTable = (LuaTable)state.DoString("return {}")[0];
386+
sandbox_env["ARGV"] = argvTable;
380387
}
388+
argvLength = argv.Length;
389+
}
390+
for (int i = 0; i < argv.Length; i++)
391+
argvTable[i + 1] = argv[i];
392+
}
393+
else
394+
{
395+
if (argvLength > 0)
396+
{
397+
argvTable = (LuaTable)state.DoString("return {}")[0];
398+
sandbox_env["ARGV"] = argvTable;
399+
argvLength = 0;
381400
}
382401
}
383402

test/Garnet.test/LuaScriptTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ public void ComplexLuaTest()
356356
for _, key in ipairs(KEYS) do
357357
table.insert(setArgs, key)
358358
end
359-
359+
unpack(KEYS)
360360
return redis.status_reply(table.concat(setArgs))
361361
""";
362362

0 commit comments

Comments
 (0)