Repository: microsoft/garnet
File: libs/server/AOF/AofProcessor.cs (C#)

Code before:
                    ObjectStoreRMW(objectStoreBasicContext, objectStoreInput, entryPtr, bufferPtr, buffer.Length);
                    break;
                case AofEntryType.ObjectStoreUpsert:
                    ObjectStoreUpsert(objectStoreBasicContext, storeWrapper.GarnetObjectSerializer, entryPtr, bufferPtr, buffer.Length);
                    break;
                case AofEntryType.ObjectStoreDelete:
                    ObjectStoreDelete(objectStoreBasicContext, entryPtr);
                    break;
                case AofEntryType.StoredProcedure:
                    RunStoredProc(header.procedureId, customProcInput, entryPtr);
                    break;
                default:
                    throw new GarnetException($"Unknown AOF header operation type {header.opType}");
            }
            return true;

            void RunStoredProc(byte id, CustomProcedureInput customProcInput, byte* ptr)
            {
                var curr = ptr + sizeof(AofHeader);

                // Reconstructing CustomProcedureInput

                // input
                customProcInput.DeserializeFrom(curr);

                try
                {
                    // Mark this txn run as a read-write session if we are replaying as a replica
                    // This is necessary to ensure that the stored procedure can perform write operations if needed
                    if (replayAsReplica)
                        respServerSession.clusterSession.SetReadWriteSession();
                    respServerSession.RunTransactionProc(id, ref customProcInput, ref output);
                }
                finally
                {
                    if (replayAsReplica)
                        respServerSession.clusterSession.SetReadOnlySession();
                }
            }
        }

        private void SwitchActiveDatabaseContext(GarnetDatabase db, bool initialSetup = false)
        {
            // Switch the session's context to match the specified database, if necessary
            if (respServerSession.activeDbId != db.Id)
            {
                var switchDbSuccessful = respServerSession.TrySwitchActiveDatabaseSession(db.Id);
                Debug.Assert(switchDbSuccessful);
            }

            // Switch the storage context to match the session, if necessary

Review comment (suggestion):
As discussed, this is on the critical path and would want to avoid the try/finally as well as repeated set and unset of ReadWriteSession. The replica's replay session should instead allow transaction execution initiated via AOF at the replica.

Code after:
                    break;
                case AofEntryType.StoreDelete:
                    StoreDelete(basicContext, entryPtr);
                    break;
                case AofEntryType.ObjectStoreRMW:
                    ObjectStoreRMW(objectStoreBasicContext, objectStoreInput, entryPtr, bufferPtr, buffer.Length);
                    break;
                case AofEntryType.ObjectStoreUpsert:
                    ObjectStoreUpsert(objectStoreBasicContext, storeWrapper.GarnetObjectSerializer, entryPtr, bufferPtr, buffer.Length);
                    break;
                case AofEntryType.ObjectStoreDelete:
                    ObjectStoreDelete(objectStoreBasicContext, entryPtr);
                    break;
                case AofEntryType.StoredProcedure:
                    RunStoredProc(header.procedureId, customProcInput, entryPtr);
                    break;
                default:
                    throw new GarnetException($"Unknown AOF header operation type {header.opType}");
            }
            return true;

            void RunStoredProc(byte id, CustomProcedureInput customProcInput, byte* ptr)
            {
                var curr = ptr + sizeof(AofHeader);

                // Reconstructing CustomProcedureInput

                // input
                customProcInput.DeserializeFrom(curr);

                // Run the stored procedure with the reconstructed input
                respServerSession.RunTransactionProc(id, ref customProcInput, ref output);
            }
        }

        private void SwitchActiveDatabaseContext(GarnetDatabase db, bool initialSetup = false)
        {
            // Switch the session's context to match the specified database, if necessary
            if (respServerSession.activeDbId != db.Id)
            {
                var switchDbSuccessful = respServerSession.TrySwitchActiveDatabaseSession(db.Id);
                Debug.Assert(switchDbSuccessful);
            }

            // Switch the storage context to match the session, if necessary
            if (this.activeDbId != db.Id || initialSetup)
            {
                var session = respServerSession.storageSession.basicContext.Session;
                basicContext = session.BasicContext;
                var objectStoreSession = respServerSession.storageSession.objectStoreBasicContext.Session;
                if (objectStoreSession is not null)