From aa2244e7d14f0a14762680138ec95bc59a9f9a54 Mon Sep 17 00:00:00 2001 From: Andy Ayers Date: Sun, 2 Aug 2026 07:55:10 -0700 Subject: [PATCH] Avoid inlining Random.InternalSample This method contains an inherently unpredictable branch that benefits from if conversion. If inlined into a caller with a loop we lose the if conversion and performance suffers, and there does not appear to be other inlining benefit. We can reconsider if/when we've freed up if-conversion from its current "not in loop" constraint. Fixes #117787 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../System.Private.CoreLib/src/System/Random.CompatImpl.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libraries/System.Private.CoreLib/src/System/Random.CompatImpl.cs b/src/libraries/System.Private.CoreLib/src/System/Random.CompatImpl.cs index 1f64d8665d3b1a..645bce98764de8 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Random.CompatImpl.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Random.CompatImpl.cs @@ -335,6 +335,8 @@ internal void NextBytes(Span buffer) } } + // Inlining this into hot caller loops can produce branch-heavy code that is slower than the standalone method. + [MethodImpl(MethodImplOptions.NoInlining)] internal int InternalSample() { Debug.Assert(_seedArray is not null);