perf(grpc-data,script-runner): #999 K-11 value frames are sized once (preflight only); shard placement hashed once per key with no per-evaluation byte[]
This commit is contained in:
@@ -278,11 +278,18 @@ public sealed class OutOfProcessScriptRunner : IPhysicalScriptRunner, IAsyncDisp
|
||||
/// <returns>The shard index.</returns>
|
||||
internal static int ShardIndex(string scopedKey, int shardCount)
|
||||
{
|
||||
// FNV-1a over the key's UTF-8 bytes — encoded rune by rune into a stack buffer (#999 / review K-11): the same
|
||||
// bytes, so the same placement as before, without a byte[] per evaluation.
|
||||
var hash = 2166136261u;
|
||||
foreach (var b in Encoding.UTF8.GetBytes(scopedKey))
|
||||
Span<byte> utf8 = stackalloc byte[4];
|
||||
foreach (var rune in scopedKey.EnumerateRunes())
|
||||
{
|
||||
hash ^= b;
|
||||
hash *= 16777619u;
|
||||
var n = rune.EncodeToUtf8(utf8);
|
||||
for (var i = 0; i < n; i++)
|
||||
{
|
||||
hash ^= utf8[i];
|
||||
hash *= 16777619u;
|
||||
}
|
||||
}
|
||||
return (int)(hash % (uint)shardCount);
|
||||
}
|
||||
@@ -391,7 +398,7 @@ public sealed class OutOfProcessScriptRunner : IPhysicalScriptRunner, IAsyncDisp
|
||||
|
||||
var handedOff = false;
|
||||
SemaphoreSlim? gate = null;
|
||||
var shard = state.Suspect ? _isolation : _shards[ShardIndex(key, _shards.Length)];
|
||||
var shard = state.Suspect ? _isolation : _shards[state.ShardIndexFor(key, _shards.Length)];
|
||||
WorkerConnection? connection = null;
|
||||
try
|
||||
{
|
||||
@@ -540,7 +547,7 @@ public sealed class OutOfProcessScriptRunner : IPhysicalScriptRunner, IAsyncDisp
|
||||
var state = Admit(key, invocation.Kind, invocation.Source);
|
||||
if (state.QuarantineReason is not null || _compileFailures.ContainsKey(state.FailureKey)) return BatchPreparation.Full;
|
||||
|
||||
var shard = state.Suspect ? _isolation : _shards[ShardIndex(key, _shards.Length)];
|
||||
var shard = state.Suspect ? _isolation : _shards[state.ShardIndexFor(key, _shards.Length)];
|
||||
if (shard.IsolationGate is not null || shard.Current is not { } connection) return BatchPreparation.Full;
|
||||
if (connection.TryGetLoaded(key, state.ShaHex, SlotOrder(state, invocation.Inputs)) is not { } entry) return BatchPreparation.Full;
|
||||
|
||||
@@ -757,7 +764,7 @@ public sealed class OutOfProcessScriptRunner : IPhysicalScriptRunner, IAsyncDisp
|
||||
|
||||
/// <summary>The shard a key's NEXT evaluation goes to (attribution may just have made it a suspect).</summary>
|
||||
private WorkerShard RouteFor(string key, KeyState state)
|
||||
=> state.Suspect ? _isolation : _shards[ShardIndex(key, _shards.Length)];
|
||||
=> state.Suspect ? _isolation : _shards[state.ShardIndexFor(key, _shards.Length)];
|
||||
|
||||
private static ScriptRunOutcome Unavailable(string reason, WorkerShard shard, WorkerConnection? failed)
|
||||
=> ScriptRunOutcome.Unavailable(reason) with { Recovery = shard.WhenRecoveredAsync(failed) };
|
||||
@@ -1044,6 +1051,20 @@ public sealed class OutOfProcessScriptRunner : IPhysicalScriptRunner, IAsyncDisp
|
||||
/// <summary>The negative compile cache's key for this source (kind-scoped).</summary>
|
||||
public string FailureKey => _failureKey ??= InProcessScriptRunner.ScopedKey(Kind, ShaHex);
|
||||
|
||||
private int _shardIndex = -1;
|
||||
|
||||
/// <summary>
|
||||
/// The key's (non-suspect) shard, hashed once per key state instead of per evaluation (#999 / review K-11). The
|
||||
/// state is per scoped key and the shard count is fixed for the runner's life, so it never changes; a benign
|
||||
/// race hashes the same value twice.
|
||||
/// </summary>
|
||||
public int ShardIndexFor(string scopedKey, int shardCount)
|
||||
{
|
||||
var index = Volatile.Read(ref _shardIndex);
|
||||
if (index < 0) Volatile.Write(ref _shardIndex, index = ShardIndex(scopedKey, shardCount));
|
||||
return index;
|
||||
}
|
||||
|
||||
/// <summary>The slot order last used for this key (see <see cref="SlotOrder"/>).</summary>
|
||||
public string[]? Paths;
|
||||
public readonly List<DateTime> KillStrikes = [];
|
||||
|
||||
Reference in New Issue
Block a user