perf(runtime): O(1) attribute resolution, precomputed types, coalesced static writes, shared JSON options

This commit is contained in:
Joseph Doherty
2026-08-14 19:57:26 -04:00
parent ee193cd2bb
commit 125055d998
7 changed files with 350 additions and 83 deletions
@@ -345,6 +345,53 @@ public class SiteStorageService
await command.ExecuteNonQueryAsync();
}
/// <summary>
/// Batch counterpart of <see cref="SetStaticOverrideAsync"/> (perf remediation,
/// arch-review WP1.5): upserts a set of static attribute overrides for one instance
/// in a SINGLE connection + transaction with one prepared command re-bound per row.
/// Used by the InstanceActor's coalesced flush — mirrors NativeAlarmActor's
/// MarkDirtyUpsert/FlushDirtyUpserts (P4) pattern — so a burst of rapid SetAttribute
/// calls on an instance collapses into one batched write instead of a write per call.
/// </summary>
/// <param name="instanceName">The unique name of the instance.</param>
/// <param name="overrides">The attribute name/value pairs to upsert (latest value per name).</param>
/// <returns>A task that completes when all rows have been committed.</returns>
public async Task SetStaticOverridesAsync(
string instanceName, IReadOnlyList<(string AttributeName, string Value)> overrides)
{
if (overrides.Count == 0)
return;
await using var connection = OpenConnection();
await using var transaction = (SqliteTransaction)await connection.BeginTransactionAsync();
await using var command = connection.CreateCommand();
command.Transaction = transaction;
command.CommandText = @"
INSERT INTO static_attribute_overrides (instance_unique_name, attribute_name, override_value, updated_at)
VALUES (@name, @attr, @val, @updatedAt)
ON CONFLICT(instance_unique_name, attribute_name) DO UPDATE SET
override_value = excluded.override_value,
updated_at = excluded.updated_at";
var pName = command.Parameters.Add("@name", SqliteType.Text);
var pAttr = command.Parameters.Add("@attr", SqliteType.Text);
var pVal = command.Parameters.Add("@val", SqliteType.Text);
var pUpdatedAt = command.Parameters.Add("@updatedAt", SqliteType.Text);
pName.Value = instanceName;
var updatedAt = DateTimeOffset.UtcNow.ToString("O");
foreach (var (attributeName, value) in overrides)
{
pAttr.Value = attributeName;
pVal.Value = value;
pUpdatedAt.Value = updatedAt;
await command.ExecuteNonQueryAsync();
}
await transaction.CommitAsync();
}
/// <summary>
/// Clears all static attribute overrides for an instance.
/// Called on redeployment to reset overrides.