perf(ablegacy): #1031 memoise the parsed address per authored tag; bound RMW gates and refusal log by the authored set; identity-checked single-tag eviction

This commit is contained in:
Joseph Doherty
2026-12-03 15:06:28 -05:00
parent 870f7b28dd
commit eb859fda52
2 changed files with 221 additions and 26 deletions
@@ -156,4 +156,95 @@ public sealed class AbLegacyIndirectAddressingTests
// Only the live resolution's handle (plus the index word's parent runtime) remains cached.
state.Runtimes.Keys.Count(k => k.StartsWith("Ind=>", StringComparison.Ordinal)).ShouldBe(1);
}
/// <summary>
/// #1031 (review 2026-12-02 P-23, residual L7): an indirect BIT write resolves to a different parent word per
/// index value, and each word used to leave its read-modify-write gate behind. The gates are reference-counted
/// and dropped when their last user leaves, so the table does not grow with the index.
/// </summary>
[Fact]
public async Task Sweeping_index_bit_writes_leave_no_rmw_gates_behind()
{
var (drv, f) = await NewDriverAsync(new AbLegacyTagDefinition("IndBit", Host, "N7:[N7:0]/3", AbLegacyDataType.Bit, Writable: true));
short index = 10;
f.Customise = p => new FakeAbLegacyTag(p) { Value = p.TagName == "N7:0" ? index : (short)0 };
var state = drv.GetDeviceState(Host)!;
for (short i = 10; i < 60; i++)
{
index = i;
if (f.Tags.TryGetValue("N7:0", out var idx)) idx.Value = i;
(await drv.WriteAsync([new WriteRequest("IndBit", true)], CancellationToken.None))[0]
.StatusCode.ShouldBe(AbLegacyStatusMapper.Good); // positive control: every sweep step really wrote
}
// The sweep really touched many distinct words ...
f.Tags.Keys.Count(k => k.StartsWith("N7:", StringComparison.Ordinal)).ShouldBeGreaterThan(10);
// ... and no RMW gate outlives its last user (the runtime gates for the live resolution remain, bounded).
state.GateCount.ShouldBeLessThan(6);
}
[Fact]
public async Task The_rmw_gate_serialises_users_of_one_word_and_is_removed_with_its_last_user()
{
var (drv, _) = await NewDriverAsync(new AbLegacyTagDefinition("T", Host, "N7:1", AbLegacyDataType.Int));
var state = drv.GetDeviceState(Host)!;
var baseline = state.GateCount;
var first = await state.EnterRmwAsync("N7:5", CancellationToken.None);
state.GateCount.ShouldBe(baseline + 1);
var second = state.EnterRmwAsync("N7:5", CancellationToken.None);
await Task.Delay(50, TestContext.Current.CancellationToken);
second.IsCompleted.ShouldBeFalse(); // serialised behind the holder
first.Dispose();
first.Dispose(); // idempotent: must not release the waiter's turn twice
var held = await second;
state.GateCount.ShouldBe(baseline + 1); // still one gate: the waiter took over the same one
var third = state.EnterRmwAsync("N7:5", CancellationToken.None);
await Task.Delay(50, TestContext.Current.CancellationToken);
third.IsCompleted.ShouldBeFalse(); // the double dispose did not free a second slot
held.Dispose();
(await third).Dispose();
state.GateCount.ShouldBe(baseline);
}
[Fact]
public async Task A_cancelled_rmw_wait_does_not_leak_its_gate()
{
var (drv, _) = await NewDriverAsync(new AbLegacyTagDefinition("T", Host, "N7:1", AbLegacyDataType.Int));
var state = drv.GetDeviceState(Host)!;
var baseline = state.GateCount;
var holder = await state.EnterRmwAsync("N7:9", CancellationToken.None);
using var cts = new CancellationTokenSource();
var waiting = state.EnterRmwAsync("N7:9", cts.Token);
cts.Cancel();
await Should.ThrowAsync<OperationCanceledException>(async () => await waiting);
holder.Dispose();
state.GateCount.ShouldBe(baseline);
}
/// <summary>#1031 P-24: an authored tag's address is parsed once, and re-parsed only when its text changes.</summary>
[Fact]
public async Task Authored_addresses_are_parsed_once_per_tag_and_reparsed_on_change()
{
var (drv, _) = await NewDriverAsync(new AbLegacyTagDefinition("T", Host, "N7:1", AbLegacyDataType.Int));
var state = drv.GetDeviceState(Host)!;
var def = new AbLegacyTagDefinition("T", Host, "N7:1", AbLegacyDataType.Int);
var a = state.ParseAddress(def);
a.ShouldNotBeNull();
ReferenceEquals(state.ParseAddress(def), a).ShouldBeTrue();
var changed = state.ParseAddress(def with { Address = "N7:2" });
changed!.WordNumber.ShouldBe(2);
state.ParseAddress(def with { Address = "not-an-address" }).ShouldBeNull(); // a malformed address caches too
// A resolved indirect definition (synthetic "tag=>concrete" name) never enters the cache.
var resolved = def with { Name = "T=>N7:7", Address = "N7:7" };
state.ParseAddress(resolved)!.WordNumber.ShouldBe(7);
ReferenceEquals(state.ParseAddress(resolved), state.ParseAddress(resolved)).ShouldBeFalse();
}
}