perf(saves): keep the fallback push loop branch-free

Measured (10M entities through the real chunk source, both monomorphic and
16-subclass polymorphic populations): a bare `foreach { PushToCache(entity); }`
runs at 2.3ns/entity while the same loop carrying the heavy-entity check
(interface SerializedLength read + branch) runs at 5.3-5.7ns - 2.3x slower.
Type diversity barely matters; the cost is the fatter loop body, confirming
that per-entity logic in the push loop defeats the JIT's tight-loop codegen.

Entities over 1MB are rare in practice - realistically only whole
GenericPersistence self-payloads, which are already published as dedicated
single chunks - so the fallback loop drops the check and rare thick entities
ride inside shared chunks (bounded tail, same behavior as the slot-range fast
path). The now-unused HeavyEntityThreshold constant is removed.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Kamron Batman 2026-07-13 23:42:55 -07:00
parent f7b835d7e9
commit 9780fc6634
No known key found for this signature in database
GPG key ID: 7D81DF26D9A5D94A
3 changed files with 14 additions and 29 deletions

View file

@ -41,9 +41,8 @@ public interface ISlotRangeSource
/// so the per-entity cost is a plain array store instead of a synchronized enqueue, and
/// workers pull whole chunks so they naturally load-balance: a worker busy with a thick
/// entity simply takes fewer chunks.
/// Entities whose previous serialized size exceeds <see cref="HeavyEntityThreshold"/> are
/// published as dedicated single-entity chunks so multi-megabyte payloads spread across
/// workers instead of riding inside one chunk.
/// Persistence self-payloads are published as dedicated single-entity chunks so large
/// systems spread across workers instead of riding inside one chunk.
/// Persistences that support direct parallel iteration publish slot ranges instead of
/// filled chunks, removing the per-entity handoff from the freeze entirely.
/// </summary>
@ -53,13 +52,6 @@ public sealed class SerializationChunkSource
// while the drain tail stays sub-millisecond.
private const int ChunkCapacity = 4096;
/// <summary>
/// Entities whose previous <see cref="IGenericSerializable.SerializedLength"/> exceeds this
/// should be pushed with <see cref="PushSingle"/>. Callers do the check where the entity's
/// concrete type is known, so the size read is not an interface dispatch per entity.
/// </summary>
public const int HeavyEntityThreshold = 1024 * 1024; // 1MB
internal readonly struct Chunk
{
public readonly IGenericSerializable Single;