Skip to content

Commit 0f10d8d

Browse files
committed
merge in fast-hash CI/CS changes
1 parent 1e02fca commit 0f10d8d

3 files changed

Lines changed: 43 additions & 32 deletions

File tree

src/RESPite/PublicAPI/PublicAPI.Unshipped.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
[SER004]RESPite.FastHash.IsCI(System.ReadOnlySpan<byte> value) -> bool
2929
[SER004]RESPite.FastHash.IsCS(long hash, System.ReadOnlySpan<byte> value) -> bool
3030
[SER004]RESPite.FastHash.IsCS(System.ReadOnlySpan<byte> value) -> bool
31+
[SER004]RESPite.FastHash.Length.get -> int
3132
[SER004]RESPite.FastHashAttribute
3233
[SER004]RESPite.FastHashAttribute.FastHashAttribute(string! token = "") -> void
3334
[SER004]RESPite.FastHashAttribute.Token.get -> string!

src/RESPite/Shared/FastHash.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public readonly struct FastHash
2828
private readonly long _hashCI;
2929
private readonly long _hashCS;
3030
private readonly ReadOnlyMemory<byte> _value;
31+
public int Length => _value.Length;
3132

3233
public FastHash(ReadOnlySpan<byte> value) : this((ReadOnlyMemory<byte>)value.ToArray()) { }
3334
public FastHash(ReadOnlyMemory<byte> value)

src/StackExchange.Redis/ResultProcessor.cs

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ internal abstract partial class ResultProcessor
2020
{
2121
public static readonly ResultProcessor<bool>
2222
Boolean = new BooleanProcessor(),
23-
DemandOK = new ExpectBasicStringProcessor(OK.Hash, OK.Length),
24-
DemandPONG = new ExpectBasicStringProcessor(PONG.Hash, PONG.Length),
23+
DemandOK = new ExpectBasicStringProcessor(OK.Hash),
24+
DemandPONG = new ExpectBasicStringProcessor(PONG.Hash),
2525
DemandZeroOrOne = new DemandZeroOrOneProcessor(),
2626
AutoConfigure = new AutoConfigureProcessor(),
2727
TrackSubscriptions = new TrackSubscriptionsProcessor(null),
2828
Tracer = new TracerProcessor(false),
2929
EstablishConnection = new TracerProcessor(true),
30-
BackgroundSaveStarted = new ExpectBasicStringProcessor(background_saving_started.Hash, background_saving_started.Length, startsWith: true),
31-
BackgroundSaveAOFStarted = new ExpectBasicStringProcessor(background_aof_rewriting_started.Hash, background_aof_rewriting_started.Length, startsWith: true);
30+
BackgroundSaveStarted = new ExpectBasicStringProcessor(background_saving_started.Hash, startsWith: true),
31+
BackgroundSaveAOFStarted = new ExpectBasicStringProcessor(background_aof_rewriting_started.Hash, startsWith: true);
3232

3333
public static readonly ResultProcessor<byte[]?>
3434
ByteArray = new ByteArrayProcessor();
@@ -1421,46 +1421,36 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes
14211421

14221422
private sealed class ExpectBasicStringProcessor : ResultProcessor<bool>
14231423
{
1424-
private readonly long _expectedHash;
1425-
private readonly int _expectedLength;
1424+
private readonly FastHash _expected;
14261425
private readonly bool _startsWith;
14271426

1428-
public ExpectBasicStringProcessor(long expectedHash, int expectedLength, bool startsWith = false)
1427+
public ExpectBasicStringProcessor(in FastHash expected, bool startsWith = false)
14291428
{
1430-
_expectedHash = expectedHash;
1431-
_expectedLength = expectedLength;
1429+
_expected = expected;
14321430
_startsWith = startsWith;
14331431
}
14341432

14351433
protected override bool SetResultCore(PhysicalConnection connection, Message message, ref RespReader reader)
14361434
{
14371435
if (!reader.IsScalar) return false;
14381436

1437+
var expectedLength = _expected.Length;
1438+
// For exact match, length must be exact
14391439
if (_startsWith)
14401440
{
1441-
// For StartsWith, we need at least _expectedLength bytes
1442-
if (reader.ScalarLength() < _expectedLength) return false;
1443-
1444-
var bytes = reader.TryGetSpan(out var tmp) ? tmp : reader.Buffer(stackalloc byte[_expectedLength]);
1445-
var hash = bytes.Slice(0, _expectedLength).Hash64();
1446-
if (hash == _expectedHash)
1447-
{
1448-
SetResult(message, true);
1449-
return true;
1450-
}
1441+
if (reader.ScalarLength() < expectedLength) return false;
14511442
}
14521443
else
14531444
{
1454-
// For exact match, length must be exact
1455-
if (!reader.ScalarLengthIs(_expectedLength)) return false;
1445+
if (!reader.ScalarLengthIs(expectedLength)) return false;
1446+
}
14561447

1457-
var bytes = reader.TryGetSpan(out var tmp) ? tmp : reader.Buffer(stackalloc byte[_expectedLength]);
1458-
var hash = bytes.Hash64();
1459-
if (hash == _expectedHash)
1460-
{
1461-
SetResult(message, true);
1462-
return true;
1463-
}
1448+
var bytes = reader.TryGetSpan(out var tmp) ? tmp : reader.Buffer(stackalloc byte[expectedLength]);
1449+
if (_startsWith) bytes = bytes.Slice(0, expectedLength);
1450+
if (_expected.IsCS(bytes))
1451+
{
1452+
SetResult(message, true);
1453+
return true;
14641454
}
14651455

14661456
if (message.Command == RedisCommand.AUTH) connection?.BridgeCouldBeNull?.Multiplexer?.SetAuthSuspect(new RedisException("Unknown AUTH exception"));
@@ -1470,10 +1460,29 @@ protected override bool SetResultCore(PhysicalConnection connection, Message mes
14701460

14711461
#pragma warning disable SA1300, SA1134
14721462
// ReSharper disable InconsistentNaming
1473-
[FastHash] private static partial class OK { }
1474-
[FastHash] private static partial class PONG { }
1475-
[FastHash("Background saving started")] private static partial class background_saving_started { }
1476-
[FastHash("Background append only file rewriting started")] private static partial class background_aof_rewriting_started { }
1463+
[FastHash]
1464+
private static partial class OK
1465+
{
1466+
public static readonly FastHash Hash = new(U8);
1467+
}
1468+
1469+
[FastHash]
1470+
private static partial class PONG
1471+
{
1472+
public static readonly FastHash Hash = new(U8);
1473+
}
1474+
1475+
[FastHash("Background saving started")]
1476+
private static partial class background_saving_started
1477+
{
1478+
public static readonly FastHash Hash = new(U8);
1479+
}
1480+
1481+
[FastHash("Background append only file rewriting started")]
1482+
private static partial class background_aof_rewriting_started
1483+
{
1484+
public static readonly FastHash Hash = new(U8);
1485+
}
14771486
// ReSharper restore InconsistentNaming
14781487
#pragma warning restore SA1300, SA1134
14791488

0 commit comments

Comments
 (0)