Skip to content

Commit 9429594

Browse files
committed
Revert "Make keys and argv as native lua tables"
This reverts commit c30d65e.
1 parent c30d65e commit 9429594

File tree

3 files changed

+30
-49
lines changed

3 files changed

+30
-49
lines changed

benchmark/BDN.benchmark/Resp/RespLuaRunnerStress.cs

Lines changed: 1 addition & 1 deletion
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"];
13+
readonly string[] keys = ["key1", "key2"];
1414

1515
[GlobalSetup]
1616
public void GlobalSetup()

libs/server/Lua/LuaRunner.cs

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

29-
LuaTable keyTable, argvTable;
30-
int keyLength, argvLength;
31-
3229
/// <summary>
3330
/// Creates a new runner with the source of the script
3431
/// </summary>
@@ -64,8 +61,6 @@ function redis.status_reply(text)
6461
function redis.error_reply(text)
6562
return { err = text }
6663
end
67-
KEYS = {}
68-
ARGV = {}
6964
sandbox_env = {
7065
tostring = tostring;
7166
next = next;
@@ -86,17 +81,13 @@ function redis.error_reply(text)
8681
math = math;
8782
table = table;
8883
string = string;
89-
KEYS = KEYS;
90-
ARGV = ARGV;
9184
}
9285
function load_sandboxed(source)
9386
if (not source) then return nil end
9487
return load(source, nil, nil, sandbox_env)
9588
end
9689
");
9790
sandbox_env = (LuaTable)state["sandbox_env"];
98-
keyTable = (LuaTable)state["KEYS"];
99-
argvTable = (LuaTable)state["ARGV"];
10091
}
10192

10293
/// <summary>
@@ -261,7 +252,7 @@ public object Run(int count, SessionParseState parseState)
261252
if (nKeys > 0)
262253
{
263254
// Lua uses 1-based indexing, so we allocate an extra entry in the array
264-
keys = new string[nKeys];
255+
keys = new string[nKeys + 2];
265256
for (int i = 0; i < nKeys; i++)
266257
{
267258
if (txnMode)
@@ -271,8 +262,9 @@ public object Run(int count, SessionParseState parseState)
271262
if (!respServerSession.storageSession.objectStoreLockableContext.IsNull)
272263
txnKeyEntries.AddKey(key, true, Tsavorite.core.LockType.Exclusive);
273264
}
274-
keys[i] = parseState.GetString(offset++);
265+
keys[i + 1] = parseState.GetString(offset++);
275266
}
267+
keys[nKeys + 1] = null;
276268
count -= nKeys;
277269

278270
//TODO: handle slot verification for Lua script keys
@@ -286,11 +278,12 @@ public object Run(int count, SessionParseState parseState)
286278
if (count > 0)
287279
{
288280
// Lua uses 1-based indexing, so we allocate an extra entry in the array
289-
argv = new string[count];
281+
argv = new string[count + 2];
290282
for (int i = 0; i < count; i++)
291283
{
292-
argv[i] = parseState.GetString(offset++);
284+
argv[i + 1] = parseState.GetString(offset++);
293285
}
286+
argv[count + 1] = null;
294287
}
295288

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

352345
object RunInternal(string[] keys, string[] argv)
353346
{
354-
if (keys != null)
347+
if (keys != this.keys)
355348
{
356-
if (keyLength != keys.Length)
349+
if (keys == null)
350+
{
351+
this.keys = null;
352+
sandbox_env["KEYS"] = this.keys;
353+
}
354+
else
357355
{
358-
if (keyLength > 0)
356+
if (this.keys != null && keys.Length == this.keys.Length)
357+
Array.Copy(keys, this.keys, keys.Length);
358+
else
359359
{
360-
keyTable.Dispose();
361-
keyTable = (LuaTable)state.DoString("return {}")[0];
362-
sandbox_env["KEYS"] = keyTable;
360+
this.keys = keys;
361+
sandbox_env["KEYS"] = this.keys;
363362
}
364-
keyLength = keys.Length;
365363
}
366-
for (int i = 0; i < keys.Length; i++)
367-
keyTable[i + 1] = keys[i];
368364
}
369-
else
365+
if (argv != this.argv)
370366
{
371-
if (keyLength > 0)
367+
if (argv == null)
372368
{
373-
keyTable = (LuaTable)state.DoString("return {}")[0];
374-
sandbox_env["KEYS"] = keyTable;
375-
keyLength = 0;
369+
this.argv = null;
370+
sandbox_env["ARGV"] = this.argv;
376371
}
377-
}
378-
if (argv != null)
379-
{
380-
if (argvLength != argv.Length)
372+
else
381373
{
382-
if (argvLength > 0)
374+
if (this.argv != null && argv.Length == this.argv.Length)
375+
Array.Copy(argv, this.argv, argv.Length);
376+
else
383377
{
384-
argvTable.Dispose();
385-
argvTable = (LuaTable)state.DoString("return {}")[0];
386-
sandbox_env["ARGV"] = argvTable;
378+
this.argv = argv;
379+
sandbox_env["ARGV"] = this.argv;
387380
}
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;
400381
}
401382
}
402383

test/Garnet.test/LuaScriptTests.cs

Lines changed: 1 addition & 1 deletion
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-
unpack(KEYS)
359+
360360
return redis.status_reply(table.concat(setArgs))
361361
""";
362362

0 commit comments

Comments
 (0)