obj.getobjectの同一タグ・同一フレーム問い合わせをキャッシュし往復を削減

このコミットが含まれているのは:
routersys committed 2026-06-27 19:50:28 +09:00
1 親 42e0b2b0f7
コミット 69881878e4
3ファイル変更
+82 -15

検索条件に一致するファイルはありません

+31
ファイルの表示
@@ -195,6 +195,37 @@ namespace LuaScript.Tests
Assert.Equal(90d * Math.PI / 180d, fields[NativeProtocol.Rzr]);
}
[Fact]
public void GetObject_CachesRepeatedQueryAndResetsEachRun()
{
Assert.True(LuaJitWorker.IsAvailable(NativeDir), "native/luajit.exe must be present");
var pixels = new byte[16];
int calls = 0;
double x = 10d;
Func<string, int, SceneObjectInfo?> resolver = (tag, frame) =>
{
calls++;
return tag == "a" ? new SceneObjectInfo("a", true, x, 0, 0, 1, 0, 255, 0) : null;
};
const string script =
"local a = obj.getobject(\"a\") " +
"local b = obj.getobject(\"a\") " +
"obj.x = a.x + b.x";
bool ok = _worker.Execute(script, Fields(2, 2, 0d), pixels, 2, 2, 5000, resolver, out _, out string? error);
Assert.True(ok, error);
Assert.Equal(1, calls);
var fields = Fields(2, 2, 0d);
x = 20d;
ok = _worker.Execute(script, fields, pixels, 2, 2, 5000, resolver, out _, out error);
Assert.True(ok, error);
Assert.Equal(2, calls);
Assert.Equal(40d, fields[NativeProtocol.X]);
}
[Fact]
public void GetObject_ReturnsNil_WhenUnresolved()
{
+19 -3
ファイルの表示
@@ -23,6 +23,11 @@ namespace LuaScript.Engine
private int _height;
private bool _alive;
private readonly byte[] _callbackTag = new byte[NativeProtocol.CallbackTagMax];
private readonly byte[] _lastCallbackTag = new byte[NativeProtocol.CallbackTagMax];
private int _lastCallbackTagLength = -1;
private string _lastCallbackTagText = string.Empty;
public LuaJitWorker(string nativeDir)
{
_nativeDir = nativeDir;
@@ -114,9 +119,8 @@ namespace LuaScript.Engine
{
int frame = view.ReadInt32(NativeProtocol.OffCallbackFrame);
int tagLen = Math.Clamp(view.ReadInt32(NativeProtocol.OffCallbackTagLen), 0, NativeProtocol.CallbackTagMax);
var tagBytes = new byte[tagLen];
view.ReadArray(NativeProtocol.CallbackTagOffset, tagBytes, 0, tagLen);
string tag = Encoding.UTF8.GetString(tagBytes);
view.ReadArray(NativeProtocol.CallbackTagOffset, _callbackTag, 0, tagLen);
string tag = ResolveTag(tagLen);
SceneObjectInfo? info;
try { info = resolveObject(tag, frame); }
@@ -141,6 +145,18 @@ namespace LuaScript.Engine
}
}
private string ResolveTag(int tagLen)
{
if (tagLen == _lastCallbackTagLength &&
_callbackTag.AsSpan(0, tagLen).SequenceEqual(_lastCallbackTag.AsSpan(0, tagLen)))
return _lastCallbackTagText;
_lastCallbackTagText = Encoding.UTF8.GetString(_callbackTag, 0, tagLen);
_callbackTag.AsSpan(0, tagLen).CopyTo(_lastCallbackTag);
_lastCallbackTagLength = tagLen;
return _lastCallbackTagText;
}
private void EnsureWorker(int width, int height)
{
if (_alive && _process is { HasExited: false } && _width == width && _height == height)
+32 -12
ファイルの表示
@@ -146,9 +146,27 @@ end
function obj.putpixeldata() end
local cacheTag, cacheFrame, cacheFound
local cacheExist, cacheX, cacheY, cacheZ, cacheZoom, cacheRz, cacheAlpha, cacheLayer
local function buildObject()
return {
exist = cacheExist,
x = cacheX, y = cacheY, z = cacheZ,
zoom = cacheZoom, sx = cacheZoom, sy = cacheZoom,
rx = 0, ry = 0, rz = cacheRz,
rxr = 0, ryr = 0, rzr = cacheRz * math.pi / 180,
alpha = cacheAlpha, layer = cacheLayer,
}
end
function obj.getobject(tag, frame)
if type(tag) ~= "string" then return nil end
if frame then frame = math.floor(frame) else frame = math.floor(obj.timelineframe or 0) end
if tag == cacheTag and frame == cacheFrame then
if cacheFound then return buildObject() end
return nil
end
local len = #tag
if len > CB_TAG_MAX then len = CB_TAG_MAX end
ffi.copy(base + CB_TAG_OFFSET, tag, len)
@@ -157,17 +175,17 @@ function obj.getobject(tag, frame)
i32[OFF_STATUS] = STATUS_CALLBACK
k32.SetEvent(doneEvent)
k32.WaitForSingleObject(workEvent, INFINITE)
if i32[OFF_CB_FOUND] == 0 then return nil end
local rz = cbResult[5]
local zoom = cbResult[4]
return {
exist = cbResult[0] ~= 0,
x = cbResult[1], y = cbResult[2], z = cbResult[3],
zoom = zoom, sx = zoom, sy = zoom,
rx = 0, ry = 0, rz = rz,
rxr = 0, ryr = 0, rzr = rz * math.pi / 180,
alpha = cbResult[6], layer = cbResult[7],
}
cacheTag = tag
cacheFrame = frame
if i32[OFF_CB_FOUND] == 0 then
cacheFound = false
return nil
end
cacheFound = true
cacheExist = cbResult[0] ~= 0
cacheX = cbResult[1]; cacheY = cbResult[2]; cacheZ = cbResult[3]
cacheZoom = cbResult[4]; cacheRz = cbResult[5]; cacheAlpha = cbResult[6]; cacheLayer = cbResult[7]
return buildObject()
end
local sandbox = {
@@ -245,6 +263,7 @@ while true do
if compiledChunk and code == compiledCode then
loadFields()
cacheTag = nil
math.randomseed(f64[31])
setfenv(compiledChunk, sandbox)
local ok, err = pcall(compiledChunk)
@@ -252,13 +271,14 @@ while true do
storeFields()
i32[OFF_PIXELSDIRTY] = dirty and 1 or 0
i32[OFF_STATUS] = 1
k32.SetEvent(doneEvent)
else
local msg = tostring(err)
ffi.copy(base + ERROR_OFFSET, msg, math.min(#msg, 4096))
i32[OFF_ERRORLEN] = math.min(#msg, 4096)
i32[OFF_STATUS] = 2
k32.SetEvent(doneEvent)
end
k32.SetEvent(doneEvent)
end
end