coderush profile

This commit is contained in:
Youmu Liu
2025-10-14 11:42:56 +08:00
parent a95e9b1ff7
commit 47d2bfe558
16 changed files with 243 additions and 131 deletions
@@ -1,9 +1,9 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="SamplingAdaptiveBackoff.cs"
// file="SamplingAdaptiveBackoff"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
@@ -197,8 +197,7 @@ public struct SamplingAdaptiveBackoff
bool methodChanged = bestMethod != _currentMethod;
bool candidateMatches = bestMethod == _candidateMethod;
_confidenceCounter = methodChanged ?
(candidateMatches ? (byte)(_confidenceCounter + 1) : (byte)1) : (byte)0;
_confidenceCounter = methodChanged ? (candidateMatches ? (byte)(_confidenceCounter + 1) : (byte)1) : (byte)0;
if (methodChanged && candidateMatches && _confidenceCounter >= CONFIDENCE_THRESHOLD)
{
@@ -318,19 +317,19 @@ public struct SamplingAdaptiveBackoff
public readonly double SuccessRate
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => TotalAttempts == 0 ? 0.0 : (double)SuccessfulAttempts / TotalAttempts;
}
public readonly double FallbackRate
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => TotalAttempts == 0 ? 0.0 : (double)FallbackCount / TotalAttempts;
}
public readonly long ElapsedTicks
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Environment.TickCount64 - StartTicks;
}
@@ -364,7 +363,8 @@ public struct SamplingAdaptiveBackoff
// Use branchless comparison for best performance
BackoffMethod best = score1 > score0 + SIGNIFICANT_IMPROVEMENT ?
BackoffMethod.Yield : BackoffMethod.SpinWait;
BackoffMethod.Yield :
BackoffMethod.SpinWait;
best = score2 > GetScore(best) + SIGNIFICANT_IMPROVEMENT ? BackoffMethod.Sleep0 : best;
return best;
@@ -397,11 +397,7 @@ public struct SamplingAdaptiveBackoff
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void UpdatePerformance(
BackoffMethod method,
double successRate,
double fallbackRate,
long avgLatency)
public void UpdatePerformance(BackoffMethod method, double successRate, double fallbackRate, long avgLatency)
{
ulong packed = PackPerformanceData(successRate, fallbackRate, avgLatency);
@@ -432,10 +428,7 @@ public struct SamplingAdaptiveBackoff
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ulong PackPerformanceData(
double successRate,
double fallbackRate,
long avgLatency)
private static ulong PackPerformanceData(double successRate, double fallbackRate, long avgLatency)
{
// Pack data into 64-bit value: 20 bits success rate, 20 bits fallback rate, 24 bits latency
uint success = (uint)(successRate * 1048575); // 20 bits (2^20 - 1)