System.Threading (SpinLock, ReaderWriterLockSlim, ThreadLocal, SemaphoreSlim, ManualResetEventSlim, SpinWait, CountdownEvent, Barrier)
CoreLib types served from the framework’s IL: their row of the per-library table of ADR 0023 (columns explained there, under “Measurements”).
| Library (switch) | Correctness (samples from IL) | Binary size, IL / shadow | Compile cpu, IL / shadow | Speed, IL / shadow (time) | Lines removed on a flip | Decision | NativeAOT on the same host, csharpc / NativeAOT (tools/il-first/measure-aot.sh: stripped size; compile cpu; run, median of 7; instructions) |
|---|---|---|---|---|---|---|---|
System.Threading.SpinLock, ReaderWriterLockSlim, ThreadLocal<T> (src/Csharpc/upstream-types/System.Threading.txt, CoreLib types: the slicer route) |
17 of 17 threading samples byte-identical to real .NET at -O0 and -O2 (123, 380, 544, 549, 555, 606, 608, 618, 619, 631, 641, 659, 847, 930, 1031, 1250, 1265). Found on the way: the slicer dropped the custom attributes of the fields it copies, so ThreadLocal<T>’s [ThreadStatic] slot arrays were shared across threads (380 printed another thread’s value); [ThreadStatic] is copied now. Environment.IsSingleProcessor (internal upstream) for ReaderWriterLockSlim |
659 177 664 to 177 648 B, 380 266 800 to 268 048 B (1.00x), a throughput program 175 392 to 186 656 B (1.06x) | 659 5.6 to 5.2 s, 380 7.7 to 6.7 s (compile cpu; within noise) | a throughput program (20 M read and 5 M write ReaderWriterLockSlim sections, 50 M ThreadLocal<int>.Value, 20 M SpinLock sections): median of 5, 1.39 to 0.81 s (0.58x: faster; noisy, loaded VPS) |
2 024 (Threading/ReaderWriterLockSlim.cs 1 677, Threading/SpinLock.cs 245, ThreadLocal.cs 102) |
Flipped (#312). Security: upstream’s lock-recursion checks, LockRecursionException/SynchronizationLockException on misuse, the owner-thread tracking of SpinLock(true), and ThreadLocal<T>’s per-thread slots and disposal are the framework’s code; the [ThreadStatic] fix closes a cross-thread data leak the slicer would otherwise have introduced. PeriodicTimer and ConcurrentExclusiveSchedulerPair (Task and timer runtime objects; SemaphoreSlim followed in #412, row below) and AsyncLocalValueChangedArgs<T> (constructed by the corelib’s AsyncLocal) stay shadows. |
threading_throughput: 187 040 / 1 361 848 B; compile cpu 6.1 / 15.3 s; run 737 / 592 ms (1.24x); 5.80 G / 5.93 G instructions |
System.Threading.SemaphoreSlim (src/Csharpc/upstream-types/System.Threading.txt, CoreLib: the slicer route; #412) |
91 samples that use SemaphoreSlim, TaskCompletionSource, Task.FromResult, WaitAsync or locks byte-identical to real .NET at -O0, -O2 and via IL, among them 619, 1242, 1030 (BlockingCollection), 1335 (new: the framework’s TaskNode waiters, FIFO release, timeouts, tokens, AvailableWaitHandle, Dispose) and 1336 (new: Task.FromResult’s shared tasks). Found on the way, fixed in general: a class deriving from Task<T> is laid out over the runtime task (as TaskCompletionSource subclasses were), Task’s CoreLib-internal promise members resolve to TaskCompletionSource’s, task intrinsics took TResult from the receiver’s static type (wrong for a subclass), Task.FromResult did not share .NET’s cached tasks (true/false, -1..8, default values; ReferenceEquals differed), slice-internal nested types had no Type.FullName, and IL field references to a shadow’s internal field (SpinWait.SpinCountForSpinBeforeWait) did not resolve |
semaphoreslim_throughput 239 408 to 253 536 B (1.06x); concurrent_throughput 495 992 to 517 504 B (1.04x) |
5.7 to 6.0 s; 10.8 to 12.7 s (within noise) | semaphoreslim_throughput (5 M Wait/Release, 2 M WaitAsync/Release, 200 k cross-thread ping-pongs, 200 x 1 000 queued async waiters): median of 7, 16 760 to 1 004 ms (0.06x: 16.7x faster); concurrent_throughput 1 037 to 990 ms |
279 (Tasks/SemaphoreSlim.cs) |
Flipped (#412). Security: the wait/release accounting, SemaphoreFullException on overflow, the disposed checks and the async-waiter queue are the framework’s code; the new compiler surface is the layout of Task<T> subclasses (header and payload of the runtime task, then the derived fields, method table chained to the Task<T> table) and is the one TaskCompletionSource subclasses already used. Task stays the runtime’s (ADR 0024). |
semaphoreslim_throughput: 253 536 / 1 569 728 B; compile cpu 6.0 / 11.8 s; run 1 004 / 383 ms (2.62x). concurrent_throughput: 517 504 / 1 892 616 B; run 990 / 389 ms (2.54x; #350, #413) |
System.Threading.ManualResetEventSlim, SpinWait (CoreLib: the slicer route) and System.Threading.dll’s own types, CountdownEvent, Barrier, BarrierPostPhaseException, ReaderWriterLock, LockCookie (taken as IL with a renamed twin, TwinReference, as System.Collections is; #414) |
53 sync and threading samples byte-identical to real .NET at -O0, -O2 and via IL (Monitor, SemaphoreSlim, BlockingCollection, Interrupt, locks, Parallel), among them 1337 (new: ManualResetEventSlim, its waits, tokens, Interrupt, WaitHandle, spin-count checks, disposal) and 1338 (new: SpinWait counts, CountdownEvent, Barrier phases and post-phase exceptions); 283 now compiles (LockCookie was unsupported) and runs as under .NET. Found on the way, fixed in general: System.Threading.Lock’s CoreLib-internal condition (Wait/Pulse/PulseAll, which .NET 11’s ManualResetEventSlim waits on) in the runtime, with its own waiter queue per lock; new Lock(bool); Thread.SpinWait(n) compiled to nothing (it spins n pauses now); Thread.OptimalMaxSpinWaitsPerSpinIteration |
sync_throughput 200 800 to 388 384 B (1.93x: Barrier’s CdsSyncEtwBCLProvider.Barrier_PhaseFinished event reaches EventSource’s formatting; a program without Barrier is unchanged, threading_throughput 191 184 to 195 376 B, concurrent_throughput 496 184 to 521 632 B with #412) |
4.8-5.8 to 9.6-12.4 s on sync_throughput (the same EventSource code) |
sync_throughput (5 M Set/IsSet/Reset, 100 k cross-thread ping-pongs, 20 k CountdownEvents, 20 k Barrier phases x 3 threads, 50 k SpinUntil hand-offs): median of 7, two rounds, 16 058-16 390 to 12 508-13 246 ms (0.79x: faster); threading_throughput 473-521 to 520-525 ms; concurrent_throughput 480-603 to 663-666 ms (the same gap #412 shows, identical instruction counts, see PR #419) |
1 139 (Tasks/ManualResetEventSlim.cs 70, Tasks/Barrier.cs 930, Tasks/CancelableWaits.cs 57, SpinWaitImpl in RuntimeServices.cs 82); 32 added (LockConditions.cs, ThreadMembers.cs) |
Flipped (#414). Security: the event state machine, waiter accounting, Barrier’s phase and participant checks and their exceptions are the framework’s code; the new runtime surface is csrt_lock_wait/pulse/pulse_all, which check ownership (SynchronizationLockException otherwise), release the lock completely and take it back with its recursion, like Monitor.Wait. Follow-up: fold EventSource.IsEnabled() when no EventListener can exist, which would take Barrier’s event code out. |
sync_throughput: 388 384 / 1 390 912 B; run 12 508-13 246 / 2 844-3 090 ms (4.3x; the remaining gap is blocking and hand-offs, #350). threading_throughput: 195 376 / 1 361 848 B; 520-525 / 570-579 ms (0.91x) |