Repository: microsoft/garnet
File: libs/server/Storage/Functions/MainStore/RMWMethods.cs (C#)

Code before:
        /// <inheritdoc />
        public bool CopyUpdater(ref SpanByte key, ref RawStringInput input, ref SpanByte oldValue, ref SpanByte newValue, ref SpanByteAndMemory output, ref RMWInfo rmwInfo, ref RecordInfo recordInfo)
        {
            // Expired data
            if (oldValue.MetadataSize > 0 && input.header.CheckExpiry(oldValue.ExtraMetadata))
            {
                recordInfo.ClearHasETag();
                rmwInfo.Action = RMWAction.ExpireAndResume;
                // reset etag state that may have been initialized earlier
                EtagState.ResetState(ref functionsState.etagState);
                return false;
            }

            rmwInfo.ClearExtraValueLength(ref recordInfo, ref newValue, newValue.TotalSize);

            RespCommand cmd = input.header.cmd;

            bool recordHadEtagPreMutation = recordInfo.ETag;
            bool shouldUpdateEtag = recordHadEtagPreMutation;
            if (shouldUpdateEtag)
            {
                // during checkpointing we might skip the inplace calls and go directly to copy update so we need to initialize here if needed
                EtagState.SetValsForRecordWithEtag(ref functionsState.etagState, ref oldValue);
            }

            switch (cmd)
            {
                case RespCommand.SETIFGREATER:
                case RespCommand.SETIFMATCH:
                    // By now the comparison for etag against existing etag has already been done in NeedCopyUpdate
                    shouldUpdateEtag = true;
                    // Copy input to value
                    ReadOnlySpan<byte> src = input.parseState.GetArgSliceByRef(0).ReadOnlySpan;

                    // retain metadata unless metadata sent
                    int metadataSize = input.arg1 != 0 ? sizeof(long) : oldValue.MetadataSize;

                    Debug.Assert(src.Length + EtagConstants.EtagSize + metadataSize == newValue.Length);

                    newValue.ExtraMetadata = oldValue.ExtraMetadata;
                    if (input.arg1 != 0)
                    {
                        newValue.ExtraMetadata = input.arg1;
                    }

                    Span<byte> dest = newValue.AsSpan(EtagConstants.EtagSize);
                    src.CopyTo(dest);

                    long etagFromClient = input.parseState.GetLong(1);

                    functionsState.etagState.etag = etagFromClient;

Review comment (performance):
moved this code to NCU since it is more efficient to handle not creating a new record at all when working with deletion.

Code after:
        }

        // NOTE: Before doing any return from this method, please make sure you are calling reset on etagState in functionsState.
        /// <inheritdoc />
        public bool CopyUpdater(ref SpanByte key, ref RawStringInput input, ref SpanByte oldValue, ref SpanByte newValue, ref SpanByteAndMemory output, ref RMWInfo rmwInfo, ref RecordInfo recordInfo)
        {
            // Expired data
            if (oldValue.MetadataSize > 0 && input.header.CheckExpiry(oldValue.ExtraMetadata))
            {
                recordInfo.ClearHasETag();
                rmwInfo.Action = RMWAction.ExpireAndResume;
                // reset etag state that may have been initialized earlier
                EtagState.ResetState(ref functionsState.etagState);
                return false;
            }

            rmwInfo.ClearExtraValueLength(ref recordInfo, ref newValue, newValue.TotalSize);

            RespCommand cmd = input.header.cmd;

            bool recordHadEtagPreMutation = recordInfo.ETag;
            bool shouldUpdateEtag = recordHadEtagPreMutation;
            if (shouldUpdateEtag)
            {
                // during checkpointing we might skip the inplace calls and go directly to copy update so we need to initialize here if needed
                EtagState.SetValsForRecordWithEtag(ref functionsState.etagState, ref oldValue);
            }

            switch (cmd)
            {
                case RespCommand.SETIFGREATER:
                case RespCommand.SETIFMATCH:
                    // By now the comparison for etag against existing etag has already been done in NeedCopyUpdate
                    shouldUpdateEtag = true;
                    // Copy input to value
                    ReadOnlySpan<byte> src = input.parseState.GetArgSliceByRef(0).ReadOnlySpan;

                    // retain metadata unless metadata sent
                    int metadataSize = input.arg1 != 0 ? sizeof(long) : oldValue.MetadataSize;

                    Debug.Assert(src.Length + EtagConstants.EtagSize + metadataSize == newValue.Length);

                    newValue.ExtraMetadata = oldValue.ExtraMetadata;
                    if (input.arg1 != 0)
                    {
                        newValue.ExtraMetadata = input.arg1;
                    }

                    Span<byte> dest = newValue.AsSpan(EtagConstants.EtagSize);
                    src.CopyTo(dest);