Repository: microsoft/garnet
File: libs/cluster/Server/ClusterProvider.cs (C#)

Code before:
                    replicationInfo.Add(new("slave_priority", "100"));
                    replicationInfo.Add(new("slave_read_only", "1"));
                    replicationInfo.Add(new("replica_announced", "1"));
                    replicationInfo.Add(new("master_sync_last_io_seconds_ago", replicationManager.LastPrimarySyncSeconds.ToString()));
                    replicationInfo.Add(new("replication_offset_lag", replicationOffsetLag.ToString()));
                    replicationInfo.Add(new("replication_offset_max_lag", storeWrapper.serverOptions.ReplicationOffsetMaxLag.ToString()));
                }
                else
                {
                    // replica0: ip=127.0.0.1,port=7001,state=online,offset=56,lag=0
                    foreach (var ri in replicaInfo)
                        replicationInfo.Add(new($"slave{ri.count}", ri.roleInfo.ToString()));
                }
            }
            return [.. replicationInfo];
        }

        /// <inheritdoc />
        public (long replication_offset, RoleInfo[] replicaInfo) GetPrimaryInfo()
        {
            if (!serverOptions.EnableCluster)
            {
                return (0, default);
            };

            return (replicationManager.ReplicationOffset, replicationManager.GetReplicaInfo().Select(w => w.roleInfo).ToArray());
        }

        /// <inheritdoc />
        public RoleInfo GetReplicaInfo()
        {
            if (!serverOptions.EnableCluster)
            {
                return new RoleInfo()
                {
                };
            }

            var config = clusterManager.CurrentConfig;
            clusterManager.GetConnectionInfo(config.LocalNodePrimaryId, out var connection);

            var (address, port) = config.GetLocalNodePrimaryAddress();
            var info = new RoleInfo()
            {
                address = address,
                port = port,
                replication_offset = replicationManager.ReplicationOffset,
                replication_state = replicationManager.Recovering ? "sync" :
                        connection.connected ? "connected" : "connect"
            };


Review comment (refactor):
It is best to avoid allocations where necessary. ToArray performs an unecessary allocation. Also, you iterate through the list to extract the Role information. If you change the GetReplicaInfo to return List<RoleInfo> then you will not need either and can just return the list to the caller and iterate to produce the response.

Code after:
                    replicationInfo.Add(new("replica_announced", "1"));
                    replicationInfo.Add(new("master_sync_last_io_seconds_ago", replicationManager.LastPrimarySyncSeconds.ToString()));
                    replicationInfo.Add(new("replication_offset_lag", replicationOffsetLag.ToString()));
                    replicationInfo.Add(new("replication_offset_max_lag", storeWrapper.serverOptions.ReplicationOffsetMaxLag.ToString()));
                }
                else
                {
                    // replica0: ip=127.0.0.1,port=7001,state=online,offset=56,lag=0
                    for (var i = 0; i < replicaInfo.Count; i++)
                        replicationInfo.Add(new($"slave{i}", replicaInfo[i].ToString()));
                }
            }
            return [.. replicationInfo];
        }

        /// <inheritdoc />
        public (long replication_offset, List<RoleInfo> replicaInfo) GetPrimaryInfo()
        {
            if (!serverOptions.EnableCluster)
            {
                return (replicationManager.ReplicationOffset, default);
            };

            return (replicationManager.ReplicationOffset, replicationManager.GetReplicaInfo());
        }

        /// <inheritdoc />
        public RoleInfo GetReplicaInfo()
        {
            if (!serverOptions.EnableCluster)
            {
                return new RoleInfo()
                {
                };
            }

            var config = clusterManager.CurrentConfig;
            clusterManager.GetConnectionInfo(config.LocalNodePrimaryId, out var connection);

            var (address, port) = config.GetLocalNodePrimaryAddress();
            var info = new RoleInfo()
            {
                address = address,
                port = port,
                replication_offset = replicationManager.ReplicationOffset,
                replication_state = replicationManager.Recovering ? "sync" :
                        connection.connected ? "connected" : "connect"
            };

            return info;
        }