Skip to content

Commit

Permalink
Use generics for CustomProcedure. (#725)
Browse files Browse the repository at this point in the history
To ensure high performance when executing custom procedures, the Execute method is modified to utilize generic type of GarnetApi instead of as a parameter that needs an interface call and boxing.
  • Loading branch information
yrajas authored Oct 16, 2024
1 parent 63133aa commit 14083b0
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 6 deletions.
3 changes: 2 additions & 1 deletion libs/server/Custom/CustomProcedureWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public abstract class CustomProcedure : CustomFunctions
/// Custom command implementation
/// </summary>
/// <param name="garnetApi"></param>
public abstract bool Execute(IGarnetApi garnetApi, ArgSlice input, ref MemoryResult<byte> output);
public abstract bool Execute<TGarnetApi>(TGarnetApi garnetApi, ArgSlice input, ref MemoryResult<byte> output)
where TGarnetApi : IGarnetApi;
}

class CustomProcedureWrapper
Expand Down
2 changes: 1 addition & 1 deletion main/GarnetServer/Extensions/SetStringAndList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Garnet
{
class SetStringAndList : CustomProcedure
{
public override bool Execute(IGarnetApi garnetApi, ArgSlice input, ref MemoryResult<byte> output)
public override bool Execute<TGarnetApi>(TGarnetApi garnetApi, ArgSlice input, ref MemoryResult<byte> output)
{
var offset = 0;
var key = GetNextArg(input, ref offset);
Expand Down
2 changes: 1 addition & 1 deletion main/GarnetServer/Extensions/Sum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Garnet
{
class Sum : CustomProcedure
{
public override bool Execute(IGarnetApi garnetApi, ArgSlice input, ref MemoryResult<byte> output)
public override bool Execute<TGarnetApi>(TGarnetApi garnetApi, ArgSlice input, ref MemoryResult<byte> output)
{
var offset = 0;
var sum = 0;
Expand Down
6 changes: 3 additions & 3 deletions test/Garnet.test/RespCustomCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ namespace Garnet.test
{
public class LargeGet : CustomProcedure
{
public override bool Execute(IGarnetApi garnetApi, ArgSlice input, ref MemoryResult<byte> output)
public override bool Execute<TGarnetApi>(TGarnetApi garnetApi, ArgSlice input, ref MemoryResult<byte> output)
{
static bool ResetBuffer(IGarnetApi garnetApi, ref MemoryResult<byte> output, int buffOffset)
static bool ResetBuffer(TGarnetApi garnetApi, ref MemoryResult<byte> output, int buffOffset)
{
bool status = garnetApi.ResetScratchBuffer(buffOffset);
if (!status)
Expand Down Expand Up @@ -92,7 +92,7 @@ public override void Main<TGarnetApi>(TGarnetApi garnetApi, ArgSlice input, ref

public class OutOfOrderFreeBuffer : CustomProcedure
{
public override bool Execute(IGarnetApi garnetApi, ArgSlice input, ref MemoryResult<byte> output)
public override bool Execute<TGarnetApi>(TGarnetApi garnetApi, ArgSlice input, ref MemoryResult<byte> output)
{
var offset = 0;
var key = GetNextArg(input, ref offset);
Expand Down

0 comments on commit 14083b0

Please sign in to comment.