Conversation
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem Statement
The mutation execution pipeline executes on every state transition. Three redundant per call allocations dominated latency:
lock(_lock) + _interceptors.ToArray()- copies entire list even when unchangedList<T> + .ToArray(), even if all interceptors pass filterSolution Overview
1. Volatile Snapshot Cache with Lazy Rebuild
volatileensures readers observe fully published arrays without torn references2. ArrayPool Based Filtering
List<T>allocation entirely3. ExecutionId Optimization
Guid.NewGuid().ToString()withInterlocked.Incrementhex counterPerformance Impact
Benchmark Results
Optimized Path Analysis
Key Design Decisions
Why volatile?
CPU memory barrier on read near free on modern CPUs. Eliminates lock acquisition entirely on hot path. Ensures cross thread visibility without lock.
Why lazy rebuild?
Startup overhead is negligible registration is one time. No rebuild cost if pipeline is never used. First read pays lock cost, subsequent reads are free.
Why ArrayPool?
Eliminates
List<T>allocation overhead. Reduces GC pressure from temporary buffers. Buffer reuse for filtered interceptor collections.Why not ReaderWriterLockSlim?
Writes Register/Unregister happen at startup. Reads happen billions of times at runtime. Volatile visibility + lazy rebuild outperforms any lock-based approach.
Files Changed
InterceptorPipeline.cs
volatile IMutationInterceptor[]? _snapshotCacheGetSnapshot()with double checked lockingList<T>withArrayPool<T>inGetApplicable()[MethodImpl(MethodImplOptions.AggressiveInlining)]for hot path methodsMutationEngine.cs
Guid.NewGuid().ToString()withInterlocked.Increment(ref _executionCounter).ToString("x8")private static long _executionCounterMigration
Caution
ExecutionId format change: If consuming
executionIdin logs, traces, or external systems, the format changes from GUID to hex counter.550e8400-e29b-41d4-a716-4466554400000000002aFor distributed tracing, use
CorrelationId(still GUID) instead ofexecutionId.