Skip to content

Commit

Permalink
reduce branchless because branchless has more instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
hamdaankhalidmsft committed Dec 26, 2024
1 parent 63e80f8 commit 1e4e256
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
5 changes: 3 additions & 2 deletions libs/server/Storage/Functions/MainStore/RMWMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,14 @@ public bool InitialUpdater(ref SpanByte key, ref RawStringInput input, ref SpanB
case RespCommand.SET:
case RespCommand.SETEXNX:
bool withEtag = input.header.CheckWithEtagFlag();
int spaceForEtag = input.header.CheckWithEtagFlagMultiplier * Constants.EtagSize;

int spaceForEtag = 0;
if (withEtag)
{
spaceForEtag = Constants.EtagSize;
recordInfo.SetHasETag();
}


// Copy input to value
var newInputValue = input.parseState.GetArgSliceByRef(0).ReadOnlySpan;
var metadataSize = input.arg1 == 0 ? 0 : sizeof(long);
Expand Down
8 changes: 5 additions & 3 deletions libs/server/Storage/Functions/MainStore/VarLenInputMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,12 @@ public int GetRMWModifiedValueLength(ref SpanByte t, ref RawStringInput input, b
if (input.header.cmd != RespCommand.NONE)
{
var cmd = input.header.cmd;
// Branchless OR condition on A && B can be expressed as => 1-(1−A)⋅(1−B)
int withEtagOrHasEtag = 1 - (1 - input.header.CheckWithEtagFlagMultiplier) * (1 - Unsafe.As<bool, byte>(ref hasEtag));

int etagOffset = withEtagOrHasEtag * Constants.EtagSize;
int etagOffset = 0;
if (hasEtag || input.header.CheckWithEtagFlag())
{
etagOffset = Constants.EtagSize;
}

switch (cmd)
{
Expand Down

0 comments on commit 1e4e256

Please sign in to comment.