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

Code before:
        /// <param name="info">Connection information for the corresponding worker.</param>
        /// <returns>Formatted string.</returns>
        public string GetNodeInfo(ushort workerId, ConnectionInfo info)
        {
            //<id>
            //<ip:port@cport[,hostname[,auxiliary_field=value]*]>
            //<flags>
            //<primary>
            //<ping-sent>
            //<pong-recv>
            //<config-epoch>
            //<link-state>
            //<slot> <slot> ... <slot>
            var nodeInfoStringBuilder = new StringBuilder();
            return nodeInfoStringBuilder
                .Append(workers[workerId].Nodeid).Append(" ")
                .Append(workers[workerId].Address).Append(":").Append(workers[workerId].Port)
                .Append("@").Append(workers[workerId].Port + 10000).Append(",").Append(workers[workerId].hostname).Append(" ")
                .Append(workerId == 1 ? "myself," : "")
                .Append(workers[workerId].Role == NodeRole.PRIMARY ? "master" : "slave").Append(" ")
                .Append(workers[workerId].Role == NodeRole.REPLICA ? workers[workerId].ReplicaOfNodeId : "-").Append(" ")
                .Append(info.ping).Append(" ")
                .Append(info.pong).Append(" ")
                .Append(workers[workerId].ConfigEpoch).Append(" ")
                .Append(info.connected || workerId == 1 ? "connected" : "disconnected")
                .Append(GetSlotRange(workerId))
                .Append(GetSpecialStates(workerId))
                .Append("\n")
                .ToString();
        }

        private string GetSpecialStates(ushort workerId)
        {
            // Only print special states for local node
            if (workerId != 1) return "";
            var specialStates = "";
            for (var slot = 0; slot < slotMap.Length; slot++)
            {
                var _workerId = slotMap[slot]._workerId;
                var _state = slotMap[slot]._state;

                if (_state == SlotState.STABLE) continue;
                if (_workerId > NumWorkers) continue;

                var _nodeId = workers[_workerId].Nodeid;
                if (_nodeId == null) continue;

                specialStates += _state switch
                {
                    SlotState.MIGRATING => $" [{slot}->-{_nodeId}]",
                    SlotState.IMPORTING => $" [{slot}-<-{_nodeId}]",

Review comment (suggestion):
Pass the current StringBuilder to `GetSlotRange` and `GetSpecialStates` so we are not creating new string builders or intermediate strings.

Code after:
        /// <param name="info">Connection information for the corresponding worker.</param>
        /// <returns>Formatted string.</returns>
        public string GetNodeInfo(ushort workerId, ConnectionInfo info)
        {
            //<id>
            //<ip:port@cport[,hostname[,auxiliary_field=value]*]>
            //<flags>
            //<primary>
            //<ping-sent>
            //<pong-recv>
            //<config-epoch>
            //<link-state>
            //<slot> <slot> ... <slot>
            var nodeInfoStringBuilder = new StringBuilder();
            GetNodeInfo(workerId, info, nodeInfoStringBuilder);
            return nodeInfoStringBuilder.ToString();
        }

        private void GetNodeInfo(ushort workerId, ConnectionInfo info, StringBuilder nodeInfoStringBuilder)
        {
            _ = nodeInfoStringBuilder
                .Append(workers[workerId].Nodeid).Append(' ')
                .Append(workers[workerId].Address).Append(':').Append(workers[workerId].Port)
                .Append('@').Append(workers[workerId].Port + 10000).Append(',').Append(workers[workerId].hostname).Append(' ')
                .Append(workerId == 1 ? "myself," : "")
                .Append(workers[workerId].Role == NodeRole.PRIMARY ? "master" : "slave").Append(' ')
                .Append(workers[workerId].Role == NodeRole.REPLICA ? workers[workerId].ReplicaOfNodeId : '-').Append(' ')
                .Append(info.ping).Append(' ')
                .Append(info.pong).Append(' ')
                .Append(workers[workerId].ConfigEpoch).Append(' ')
                .Append(info.connected || workerId == 1 ? "connected" : "disconnected");
            AppendSlotRange(nodeInfoStringBuilder, workerId);
            AppendSpecialStates(nodeInfoStringBuilder, workerId);
            _ = nodeInfoStringBuilder.Append('\n');
        }

        private void AppendSpecialStates(StringBuilder stringBuilder, uint workerId)
        {
            // Only print special states for local node
            if (workerId != 1) return;
            for (var slot = 0; slot < slotMap.Length; slot++)
            {
                var _workerId = slotMap[slot]._workerId;
                var _state = slotMap[slot]._state;

                if (_state == SlotState.STABLE) continue;
                if (_workerId > NumWorkers) continue;

                var _nodeId = workers[_workerId].Nodeid;
                if (_nodeId == null) continue;