Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use more UTF-8 string literals #40

Merged
merged 5 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions benchmark/Resp.benchmark/RespOnlineBench.cs
Original file line number Diff line number Diff line change
Expand Up @@ -503,15 +503,15 @@ public unsafe void OpRunnerLightClient(int thread_id)
case OpType.SET:
byte* setCurr = setBuffer + 13;
RespWriteUtils.WriteBulkString(Encoding.ASCII.GetBytes(req.GenerateKey()), ref setCurr, setEnd);
RespWriteUtils.WriteBulkString(req.GenerateValueBytes(), ref setCurr, setEnd);
RespWriteUtils.WriteBulkString(req.GenerateValueBytes().Span, ref setCurr, setEnd);
client.Send(setBuffer, (int)(setCurr - setBuffer), 1);
client.CompletePendingRequests();
break;
case OpType.SETEX:
byte* setexCurr = setexBuffer + 15;
RespWriteUtils.WriteBulkString(Encoding.ASCII.GetBytes(req.GenerateKey()), ref setexCurr, setexEnd);
RespWriteUtils.WriteIntegerAsBulkString(opts.Ttl, ref setexCurr, setexEnd);
RespWriteUtils.WriteBulkString(req.GenerateValueBytes(), ref setexCurr, setexEnd);
RespWriteUtils.WriteBulkString(req.GenerateValueBytes().Span, ref setexCurr, setexEnd);
client.Send(setexBuffer, (int)(setexCurr - setexBuffer), 1);
client.CompletePendingRequests();
break;
Expand Down
4 changes: 2 additions & 2 deletions libs/client/ClientSession/GarnetClientSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ public void ExecuteForArray(params string[] command)
Flush();
}

static readonly byte[] CLUSTER = Encoding.ASCII.GetBytes("$7\r\nCLUSTER\r\n");
static readonly byte[] appendLog = Encoding.ASCII.GetBytes("appendlog");
static ReadOnlySpan<byte> CLUSTER => "$7\r\nCLUSTER\r\n"u8;
static ReadOnlySpan<byte> appendLog => "appendlog"u8;

/// <summary>
/// ClusterAppendLog
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Garnet.client
/// </summary>
public sealed unsafe partial class GarnetClientSession : IServerHook, IMessageConsumer
{
static readonly byte[] GOSSIP = Encoding.ASCII.GetBytes("GOSSIP");
static ReadOnlySpan<byte> GOSSIP => "GOSSIP"u8;

/// <summary>
/// Send gossip message to corresponding node
Expand Down Expand Up @@ -55,7 +55,7 @@ public Task<string> ExecuteGossip(Memory<byte> byteArray)
offset = curr;

//3
while (!RespWriteUtils.WriteBulkString(byteArray, ref curr, end))
while (!RespWriteUtils.WriteBulkString(byteArray.Span, ref curr, end))
{
Flush();
curr = offset;
Expand Down
22 changes: 11 additions & 11 deletions libs/client/ClientSession/GarnetClientSessionMigrationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ namespace Garnet.client
/// </summary>
public sealed unsafe partial class GarnetClientSession : IServerHook, IMessageConsumer
{
static readonly Memory<byte> AUTH = Encoding.ASCII.GetBytes("AUTH");
static readonly Memory<byte> SETSLOTSRANGE = Encoding.ASCII.GetBytes("SETSLOTSRANGE");
static readonly Memory<byte> MIGRATE = Encoding.ASCII.GetBytes("MIGRATE");
static readonly Memory<byte> DELKEY = Encoding.ASCII.GetBytes("DELKEY");
static readonly Memory<byte> GETKVPAIRINSLOT = Encoding.ASCII.GetBytes("GETKVPAIRINSLOT");
static ReadOnlySpan<byte> AUTH => "AUTH"u8;
static ReadOnlySpan<byte> SETSLOTSRANGE => "SETSLOTSRANGE"u8;
static ReadOnlySpan<byte> MIGRATE => "MIGRATE"u8;
static ReadOnlySpan<byte> DELKEY => "DELKEY"u8;
static ReadOnlySpan<byte> GETKVPAIRINSLOT => "GETKVPAIRINSLOT"u8;
PaulusParssinen marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Send AUTH command to target node to authenticate connection.
Expand Down Expand Up @@ -119,7 +119,7 @@ public Task<string> SetSlotRange(Memory<byte> state, string nodeid, List<(int, i
offset = curr;

//3
while (!RespWriteUtils.WriteBulkString(state, ref curr, end))
while (!RespWriteUtils.WriteBulkString(state.Span, ref curr, end))
{
Flush();
curr = offset;
Expand Down Expand Up @@ -207,23 +207,23 @@ public Task<string> MigrateData(string sourceNodeId, Memory<byte> replaceOption,
offset = curr;

//4
while (!RespWriteUtils.WriteBulkString(replaceOption, ref curr, end))
while (!RespWriteUtils.WriteBulkString(replaceOption.Span, ref curr, end))
{
Flush();
curr = offset;
}
offset = curr;

//5
while (!RespWriteUtils.WriteBulkString(storeType, ref curr, end))
while (!RespWriteUtils.WriteBulkString(storeType.Span, ref curr, end))
{
Flush();
curr = offset;
}
offset = curr;

//6
while (!RespWriteUtils.WriteBulkString(data, ref curr, end))
while (!RespWriteUtils.WriteBulkString(data.Span, ref curr, end))
{
Flush();
curr = offset;
Expand Down Expand Up @@ -374,15 +374,15 @@ public void SetClusterMigrate(string sourceNodeId, Memory<byte> replaceOption, M
offset = curr;

//4
while (!RespWriteUtils.WriteBulkString(replaceOption, ref curr, end))
while (!RespWriteUtils.WriteBulkString(replaceOption.Span, ref curr, end))
{
Flush();
curr = offset;
}
offset = curr;

//5
while (!RespWriteUtils.WriteBulkString(storeType, ref curr, end))
while (!RespWriteUtils.WriteBulkString(storeType.Span, ref curr, end))
{
Flush();
curr = offset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ namespace Garnet.client
/// </summary>
public sealed unsafe partial class GarnetClientSession : IServerHook, IMessageConsumer
{
static readonly byte[] initiate_replica_sync = Encoding.ASCII.GetBytes("initiate_replica_sync");
static readonly byte[] send_ckpt_metadata = Encoding.ASCII.GetBytes("send_ckpt_metadata");
static readonly byte[] send_ckpt_file_segment = Encoding.ASCII.GetBytes("send_ckpt_file_segment");
static readonly byte[] begin_replica_recover = Encoding.ASCII.GetBytes("begin_replica_recover");
static ReadOnlySpan<byte> initiate_replica_sync => "initiate_replica_sync"u8;
static ReadOnlySpan<byte> send_ckpt_metadata => "send_ckpt_metadata"u8;
static ReadOnlySpan<byte> send_ckpt_file_segment => "send_ckpt_file_segment"u8;
static ReadOnlySpan<byte> begin_replica_recover => "begin_replica_recover"u8;

/// <summary>
/// Initiate checkpoint retrieval from replica by sending replica checkpoint information and AOF address range
Expand Down Expand Up @@ -142,7 +142,7 @@ public Task<string> ExecuteSendCkptMetadata(Memory<byte> fileTokenBytes, int fil
offset = curr;

//3
while (!RespWriteUtils.WriteBulkString(fileTokenBytes, ref curr, end))
while (!RespWriteUtils.WriteBulkString(fileTokenBytes.Span, ref curr, end))
{
Flush();
curr = offset;
Expand All @@ -158,7 +158,7 @@ public Task<string> ExecuteSendCkptMetadata(Memory<byte> fileTokenBytes, int fil
offset = curr;

//5
while (!RespWriteUtils.WriteBulkString(data, ref curr, end))
while (!RespWriteUtils.WriteBulkString(data.Span, ref curr, end))
{
Flush();
curr = offset;
Expand Down Expand Up @@ -208,7 +208,7 @@ public Task<string> ExecuteSendFileSegments(Memory<byte> fileTokenBytes, int fil
offset = curr;

//3
while (!RespWriteUtils.WriteBulkString(fileTokenBytes, ref curr, end))
while (!RespWriteUtils.WriteBulkString(fileTokenBytes.Span, ref curr, end))
{
Flush();
curr = offset;
Expand Down
40 changes: 20 additions & 20 deletions libs/client/GarnetClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Garnet.client
{
struct OK_MEM : IMemoryOwner<byte>
{
static readonly Memory<byte> RESP_OK = Encoding.ASCII.GetBytes("OK");
static readonly Memory<byte> RESP_OK = "OK"u8.ToArray();
public Memory<byte> Memory => RESP_OK;
public void Dispose() { }
}
Expand All @@ -30,17 +30,17 @@ public void Dispose() { }
/// </summary>
public sealed partial class GarnetClient : IServerHook, IMessageConsumer, IDisposable
{
static readonly Memory<byte> GET = Encoding.ASCII.GetBytes("$3\r\nGET\r\n");
static readonly Memory<byte> MGET = Encoding.ASCII.GetBytes("$4\r\nMGET\r\n");
static readonly Memory<byte> SET = Encoding.ASCII.GetBytes("$3\r\nSET\r\n");
static readonly Memory<byte> DEL = Encoding.ASCII.GetBytes("$3\r\nDEL\r\n");
static readonly Memory<byte> PING = Encoding.ASCII.GetBytes("$4\r\nPING\r\n");
static readonly Memory<byte> INCR = Encoding.ASCII.GetBytes("$4\r\nINCR\r\n");
static readonly Memory<byte> INCRBY = Encoding.ASCII.GetBytes("$6\r\nINCRBY\r\n");
static readonly Memory<byte> DECR = Encoding.ASCII.GetBytes("$4\r\nDECR\r\n");
static readonly Memory<byte> DECRBY = Encoding.ASCII.GetBytes("$6\r\nDECRBY\r\n");
static readonly Memory<byte> QUIT = Encoding.ASCII.GetBytes("$4\r\nQUIT\r\n");
static readonly Memory<byte> AUTH = Encoding.ASCII.GetBytes("$4\r\nAUTH\r\n");
static readonly Memory<byte> GET = "$3\r\nGET\r\n"u8.ToArray();
static readonly Memory<byte> MGET = "$4\r\nMGET\r\n"u8.ToArray();
static readonly Memory<byte> SET = "$3\r\nSET\r\n"u8.ToArray();
static readonly Memory<byte> DEL = "$3\r\nDEL\r\n"u8.ToArray();
static readonly Memory<byte> PING = "$4\r\nPING\r\n"u8.ToArray();
static readonly Memory<byte> INCR = "$4\r\nINCR\r\n"u8.ToArray();
static readonly Memory<byte> INCRBY = "$6\r\nINCRBY\r\n"u8.ToArray();
static readonly Memory<byte> DECR = "$4\r\nDECR\r\n"u8.ToArray();
static readonly Memory<byte> DECRBY = "$6\r\nDECRBY\r\n"u8.ToArray();
static readonly Memory<byte> QUIT = "$4\r\nQUIT\r\n"u8.ToArray();
static readonly Memory<byte> AUTH = "$4\r\nAUTH\r\n"u8.ToArray();
static readonly MemoryResult<byte> RESP_OK = new(default(OK_MEM));

readonly string address;
Expand Down Expand Up @@ -525,7 +525,7 @@ async ValueTask InternalExecuteAsync(TcsWrapper tcs, Memory<byte> op, string par
byte* end = curr + totalLen;
RespWriteUtils.WriteArrayLength(arraySize, ref curr, end);

RespWriteUtils.WriteDirect(op, ref curr, end);
RespWriteUtils.WriteDirect(op.Span, ref curr, end);
if (param1 != null)
RespWriteUtils.WriteBulkString(param1, ref curr, end);
if (param2 != null)
Expand Down Expand Up @@ -649,8 +649,8 @@ async ValueTask InternalExecuteAsync(Memory<byte> op, Memory<byte> clusterOp, st
byte* end = curr + totalLen;
RespWriteUtils.WriteArrayLength(arraySize, ref curr, end);

RespWriteUtils.WriteDirect(op, ref curr, end);
RespWriteUtils.WriteBulkString(clusterOp, ref curr, end);
RespWriteUtils.WriteDirect(op.Span, ref curr, end);
RespWriteUtils.WriteBulkString(clusterOp.Span, ref curr, end);
RespWriteUtils.WriteBulkString(nodeId, ref curr, end);
RespWriteUtils.WriteArrayItem(currentAddress, ref curr, end);
RespWriteUtils.WriteArrayItem(nextAddress, ref curr, end);
Expand Down Expand Up @@ -738,11 +738,11 @@ async ValueTask InternalExecuteAsync(TcsWrapper tcs, Memory<byte> op, Memory<byt
byte* end = curr + totalLen;
RespWriteUtils.WriteArrayLength(arraySize, ref curr, end);

RespWriteUtils.WriteDirect(op, ref curr, end);
RespWriteUtils.WriteDirect(op.Span, ref curr, end);
if (!param1.IsEmpty)
RespWriteUtils.WriteBulkString(param1, ref curr, end);
RespWriteUtils.WriteBulkString(param1.Span, ref curr, end);
if (!param2.IsEmpty)
RespWriteUtils.WriteBulkString(param2, ref curr, end);
RespWriteUtils.WriteBulkString(param2.Span, ref curr, end);

Debug.Assert(curr == end);
}
Expand Down Expand Up @@ -970,11 +970,11 @@ async ValueTask InternalExecuteAsync(TcsWrapper tcs, Memory<byte> respOp, IColle
byte* curr = (byte*)networkWriter.GetPhysicalAddress(address);
byte* end = curr + totalLen;
RespWriteUtils.WriteArrayLength(arraySize, ref curr, end);
RespWriteUtils.WriteDirect(respOp, ref curr, end);
RespWriteUtils.WriteDirect(respOp.Span, ref curr, end);
if (isArray)//Write arg data
{
foreach (var arg in args)
RespWriteUtils.WriteBulkString(arg, ref curr, end);
RespWriteUtils.WriteBulkString(arg.Span, ref curr, end);
}
Debug.Assert(curr == end);
}
Expand Down
6 changes: 3 additions & 3 deletions libs/client/GarnetClientAPI/GarnetClientAdminCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespace Garnet.client
{
public sealed partial class GarnetClient
{
static readonly Memory<byte> SAVE = Encoding.ASCII.GetBytes("$4\r\nSAVE\r\n");
static readonly Memory<byte> INFO = Encoding.ASCII.GetBytes("$4\r\nINFO\r\n");
static readonly Memory<byte> REPLICAOF = Encoding.ASCII.GetBytes("$9\r\nREPLICAOF\r\n");
static readonly Memory<byte> SAVE = "$4\r\nSAVE\r\n"u8.ToArray();
static readonly Memory<byte> INFO = "$4\r\nINFO\r\n"u8.ToArray();
static readonly Memory<byte> REPLICAOF = "$9\r\nREPLICAOF\r\n"u8.ToArray();

/// <summary>
/// Take a checkpoint of the Garnet instance.
Expand Down
4 changes: 2 additions & 2 deletions libs/client/GarnetClientAPI/GarnetClientClusterCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public sealed partial class GarnetClient
/// <summary>
/// CLUSTER resp formatted
/// </summary>
public static readonly Memory<byte> CLUSTER = Encoding.ASCII.GetBytes("$7\r\nCLUSTER\r\n");
static readonly Memory<byte> FAILOVER = Encoding.ASCII.GetBytes("FAILOVER");
public static readonly Memory<byte> CLUSTER = "$7\r\nCLUSTER\r\n"u8.ToArray();
static readonly Memory<byte> FAILOVER = "FAILOVER"u8.ToArray();

/// <summary>
/// Issue cluster failover command to replica node
Expand Down
6 changes: 3 additions & 3 deletions libs/client/GarnetClientAPI/GarnetClientSortedSetCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespace Garnet.client
public sealed partial class GarnetClient
{

static readonly Memory<byte> ZCARD = Encoding.ASCII.GetBytes("$5\r\nZCARD\r\n");
static readonly Memory<byte> ZADD = Encoding.ASCII.GetBytes("$4\r\nZADD\r\n");
static readonly Memory<byte> ZREM = Encoding.ASCII.GetBytes("$4\r\nZREM\r\n");
static readonly Memory<byte> ZCARD = "$5\r\nZCARD\r\n"u8.ToArray();
static readonly Memory<byte> ZADD = "$4\r\nZADD\r\n"u8.ToArray();
static readonly Memory<byte> ZREM = "$4\r\nZREM\r\n"u8.ToArray();

/// <summary>
/// Adds/Updates a member, score in a SortedSet
Expand Down
4 changes: 2 additions & 2 deletions libs/cluster/Server/GarnetClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ namespace Garnet.cluster
{
internal static partial class GarnetClientExtensions
{
static readonly Memory<byte> GOSSIP = Encoding.ASCII.GetBytes("GOSSIP");
static readonly Memory<byte> WITHMEET = Encoding.ASCII.GetBytes("WITHMEET");
static readonly Memory<byte> GOSSIP = "GOSSIP"u8.ToArray();
static readonly Memory<byte> WITHMEET = "WITHMEET"u8.ToArray();

/// <summary>
/// Send config
Expand Down
6 changes: 3 additions & 3 deletions libs/cluster/Server/Migration/MigrateSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ namespace Garnet.cluster
/// </summary>
internal sealed unsafe partial class MigrateSession : IDisposable
{
static readonly Memory<byte> IMPORTING = Encoding.ASCII.GetBytes("IMPORTING");
static readonly Memory<byte> NODE = Encoding.ASCII.GetBytes("NODE");
static readonly Memory<byte> STABLE = Encoding.ASCII.GetBytes("STABLE");
static readonly Memory<byte> IMPORTING = "IMPORTING"u8.ToArray();
static readonly Memory<byte> NODE = "NODE"u8.ToArray();
static readonly Memory<byte> STABLE = "STABLE"u8.ToArray();

readonly ClusterProvider clusterProvider;
readonly LocalServerSession localServerSession;
Expand Down
4 changes: 2 additions & 2 deletions libs/cluster/Session/ClusterCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ private bool ProcessClusterBasicCommands(ReadOnlySpan<byte> bufSpan, ReadOnlySpa
readHead = (int)(ptr - recvBufferPtr);
if (clusterProvider.clusterManager.CurrentConfig.NumWorkers > 2)
{
var resp = Encoding.ASCII.GetBytes("-ERR The user can assign a config epoch only when the node does not know any other node.\r\n");
ReadOnlySpan<byte> resp = "-ERR The user can assign a config epoch only when the node does not know any other node.\r\n"u8;
while (!RespWriteUtils.WriteResponse(resp, ref dcurr, dend))
SendAndReset();
}
Expand Down Expand Up @@ -565,7 +565,7 @@ public bool ProcessFailoverCommands(ReadOnlySpan<byte> bufSpan, ReadOnlySpan<byt
}
else
{
resp = Encoding.ASCII.GetBytes("-ERR Replica AOF is switched off. Replication unavailable. Please restart replica with --aof option.\r\n");
resp = "-ERR Replica AOF is switched off. Replication unavailable. Please restart replica with --aof option.\r\n"u8;
}
while (!RespWriteUtils.WriteResponse(resp, ref dcurr, dend))
SendAndReset();
Expand Down
10 changes: 5 additions & 5 deletions libs/cluster/Session/ClusterSlotCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private void Redirect(ushort slot, ClusterConfig config)
if (port != 0)
resp = Encoding.ASCII.GetBytes($"-MOVED {slot} {address}:{port}\r\n");
else
resp = Encoding.ASCII.GetBytes("-CLUSTERDOWN Hash slot not served\r\n");
resp = "-CLUSTERDOWN Hash slot not served\r\n"u8.ToArray();

logger?.LogDebug("SEND: {msg}", Encoding.ASCII.GetString(resp).Replace("\n", "!").Replace("\r", "|"));
while (!RespWriteUtils.WriteDirect(resp, ref dcurr, dend))
Expand All @@ -43,7 +43,7 @@ private void Redirect(ushort slot, ClusterConfig config)

private void WriteClusterSlotVerificationMessage(ClusterConfig config, ClusterSlotVerificationResult vres, ref byte* dcurr, ref byte* dend)
{
byte[] resp = default;
ReadOnlySpan<byte> resp = default;
SlotVerifiedState state = vres.state;
ushort slot = vres.slot;
string address;
Expand All @@ -55,17 +55,17 @@ private void WriteClusterSlotVerificationMessage(ClusterConfig config, ClusterSl
resp = Encoding.ASCII.GetBytes($"-MOVED {slot} {address}:{port}\r\n");
break;
case SlotVerifiedState.MIGRATING:
resp = Encoding.ASCII.GetBytes("-MIGRATING.\r\n");
resp = "-MIGRATING.\r\n"u8;
break;
case SlotVerifiedState.CLUSTERDOWN:
resp = Encoding.ASCII.GetBytes("-CLUSTERDOWN Hash slot not served\r\n");
resp = "-CLUSTERDOWN Hash slot not served\r\n"u8;
break;
case SlotVerifiedState.ASK:
(address, port) = config.AskEndpointFromSlot(slot);
resp = Encoding.ASCII.GetBytes($"-ASK {slot} {address}:{port}\r\n");
break;
case SlotVerifiedState.CROSSLOT:
resp = Encoding.ASCII.GetBytes($"-CROSSSLOT Keys in request don't hash to the same slot\r\n");
resp = "-CROSSSLOT Keys in request don't hash to the same slot\r\n"u8;
break;
default:
throw new Exception($"Unknown SlotVerifiedState {state}");
Expand Down
2 changes: 1 addition & 1 deletion libs/common/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Garnet.common
/// </summary>
public class Generator
{
static readonly byte[] hexchars = Encoding.ASCII.GetBytes("0123456789abcdef");
static ReadOnlySpan<byte> hexchars => "0123456789abcdef"u8;

/// <summary>
/// Random hex id
Expand Down
Loading
Loading