Repository: microsoft/garnet
File: libs/server/Storage/Session/ObjectStore/ListOps.cs (C#)

Code before:

                        // Left push (addfirst) to destination
                        if (destinationDirection == OperationDirection.Left)
                            dstListObject.LnkList.AddFirst(element);
                        else
                            dstListObject.LnkList.AddLast(element);

                        dstListObject.UpdateSize(element);
                        newListValue = new ListObject(dstListObject.LnkList, dstListObject.Expiration, dstListObject.Size);

                        // Upsert
                        SET(destinationKey.ToArray(), newListValue, ref objectStoreLockableContext);
                    }
                    else
                    {
                        // When the source and the destination key is the same the operation is done only in the sourceList
                        if (destinationDirection == OperationDirection.Left)
                            srcListObject.LnkList.AddFirst(element);
                        else if (destinationDirection == OperationDirection.Right)
                            srcListObject.LnkList.AddLast(element);
                        newListValue = srcListObject;
                        ((ListObject)newListValue).UpdateSize(element);
                    }
                }
            }
            finally
            {
                if (createTransaction)
                    txnManager.Commit(true);
            }

            itemBroker.HandleCollectionUpdate(destinationKey.Span.ToArray());
            return GarnetStatus.OK;
        }

        /// <summary>
        /// Trim an existing list so it only contains the specified range of elements.
        /// </summary>
        /// <typeparam name="TObjectContext"></typeparam>
        /// <param name="key"></param>
        /// <param name="start"></param>
        /// <param name="stop"></param>
        /// <param name="objectStoreContext"></param>
        /// <returns>true when successful</returns>
        public unsafe bool ListTrim<TObjectContext>(ArgSlice key, int start, int stop, ref TObjectContext objectStoreContext)
            where TObjectContext : ITsavoriteContext<byte[], IGarnetObject, ObjectInput, GarnetObjectStoreOutput, long, ObjectSessionFunctions, ObjectStoreFunctions, ObjectStoreAllocator>
        {
            // Prepare the input
            var header = new RespInputHeader(GarnetObjectType.List) { ListOp = ListOperation.LTRIM };
            var input = new ObjectInput(header, start, stop);


Review comment (suggestion):
Nice catch! You can even break earlier before the `RemoveFirst`/`RemoveLast` if the directions are the same.

Code after:

                    IGarnetObject newListValue = null;
                    if (!sameKey)
                    {
                        if (srcListObject.LnkList.Count == 0)
                        {
                            _ = EXPIRE(sourceKey, TimeSpan.Zero, out _, StoreType.Object, ExpireOption.None,
                                ref lockableContext, ref objectLockableContext);
                        }

                        // Left push (addfirst) to destination
                        if (destinationDirection == OperationDirection.Left)
                            dstListObject.LnkList.AddFirst(element);
                        else
                            dstListObject.LnkList.AddLast(element);

                        dstListObject.UpdateSize(element);
                        newListValue = new ListObject(dstListObject.LnkList, dstListObject.Expiration, dstListObject.Size);

                        // Upsert
                        SET(destinationKey.ToArray(), newListValue, ref objectStoreLockableContext);
                    }
                    else
                    {
                        // When the source and the destination key is the same the operation is done only in the sourceList
                        if (destinationDirection == OperationDirection.Left)
                            srcListObject.LnkList.AddFirst(element);
                        else if (destinationDirection == OperationDirection.Right)
                            srcListObject.LnkList.AddLast(element);
                        newListValue = srcListObject;
                        ((ListObject)newListValue).UpdateSize(element);
                    }
                }
            }
            finally
            {
                if (createTransaction)
                    txnManager.Commit(true);
            }

            itemBroker.HandleCollectionUpdate(destinationKey.Span.ToArray());
            return GarnetStatus.OK;
        }

        /// <summary>
        /// Trim an existing list so it only contains the specified range of elements.
        /// </summary>
        /// <typeparam name="TObjectContext"></typeparam>
        /// <param name="key"></param>
        /// <param name="start"></param>
        /// <param name="stop"></param>