Commit 93ab472f authored by Marc Gravell's avatar Marc Gravell

optimize pub/sub for short (<=23 bytes) channel keys

parent c1fd3b0b
......@@ -25,7 +25,7 @@ internal unsafe static CommandBytes TrimToFit(string value)
}
}
// Uses [n=4] x UInt64 values to store a command payload,
// Uses [ChunkLength] x UInt64 values to store a command payload,
// allowing allocation free storage and efficient
// equality tests. If you're glancing at this and thinking
// "that's what fixed buffers are for", please see:
......@@ -114,7 +114,7 @@ public unsafe CommandBytes(string value)
}
}
public unsafe CommandBytes(ReadOnlySpan<byte> value)
public unsafe CommandBytes(ReadOnlySpan<byte> value, bool caseInsensitive = true)
{
if (value.Length > MaxLength) throw new ArgumentOutOfRangeException("Maximum command length exceeed: " + value.Length + " bytes");
_0 = _1 = _2 = 0L;
......@@ -122,10 +122,17 @@ public unsafe CommandBytes(ReadOnlySpan<byte> value)
{
byte* bPtr = (byte*)uPtr;
value.CopyTo(new Span<byte>(bPtr + 1, value.Length));
*bPtr = (byte)UpperCasify(value.Length, bPtr + 1);
if (caseInsensitive)
{
*bPtr = (byte)UpperCasify(value.Length, bPtr + 1);
}
else
{
*bPtr = (byte)value.Length;
}
}
}
public unsafe CommandBytes(ReadOnlySequence<byte> value)
public unsafe CommandBytes(ReadOnlySequence<byte> value, bool caseInsensitive = true)
{
if (value.Length > MaxLength) throw new ArgumentOutOfRangeException("Maximum command length exceeed");
int len = unchecked((int)value.Length);
......@@ -147,7 +154,14 @@ public unsafe CommandBytes(ReadOnlySequence<byte> value)
target = target.Slice(segment.Length);
}
}
*bPtr = (byte)UpperCasify(len, bPtr + 1);
if (caseInsensitive)
{
*bPtr = (byte)UpperCasify(len, bPtr + 1);
}
else
{
*bPtr = (byte)len;
}
}
}
private unsafe int UpperCasify(int len, byte* bPtr)
......
......@@ -49,16 +49,16 @@ public int GetInt32(int index)
public RedisChannel GetChannel(int index, RedisChannel.PatternMode mode)
=> _inner[index].AsRedisChannel(null, mode);
internal bool TryGetCommandBytes(int i, out CommandBytes command)
internal bool TryGetCommandBytes(int index, out CommandBytes command, bool caseInsensitive = true)
{
var payload = _inner[i].Payload;
var payload = _inner[index].Payload;
if (payload.Length > CommandBytes.MaxLength)
{
command = default;
return false;
}
command = payload.IsEmpty ? default : new CommandBytes(payload);
command = payload.IsEmpty ? default : new CommandBytes(payload, caseInsensitive);
return true;
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment