NetworkStream 上的 ReadAsync 似乎正在使用我所有的 CPU

ReadAsync on a NetworkStream appears to be using all my CPU

我有以下方法,它从 NetworkStream 异步读取和反序列化数据包。一切正常,但 CPU 分析显示我正在等待异步读取的最后一行是我的大部分 CPU 使用的来源。

我是否实现了此 badly/inefficiently,或者 NetworkStream 的异步实现存在固有错误?

public async Task<Packet> ReadAsync(CancellationToken cancellationToken)
{
    while (true)
    {
        // Read through the available bytes until we find the start of a packet
        while (start < length && buffer[start] != Packet.STX)
            start++;

        // Align the packet (and all successive bytes) with the beginning of the buffer
        if (start > 0)
        {
            if (start < length)
                Array.Copy(buffer, start, buffer, 0, length - start);

            length -= start;
            start = 0;
        }

        // Read through the available bytes until we find the end of the packet
        while (end < length && buffer[end] != Packet.ETX)
            end++;

        // If we have a whole packet in the buffer, deserialize and return it
        if (end < length)
        {
            byte[] data = new byte[end + 1];
            Array.Copy(buffer, data, end + 1);
            byte[] decoded = null;
            Packet packet = null;

            try
            {
                decoded = Packet.Decode(data);
            }
            catch (Exception ex)
            {
                throw new IOException("Could not decode packet", ex);
            }

            if (decoded != null)
            {
                try
                {
                    packet = Packet.Deserialize(decoded);
                }
                catch (Exception ex)
                {
                    throw new IOException("Could not deserialize packet", ex);
                }
            }

            Array.Copy(buffer, end + 1, buffer, 0, length - (end + 1));
            length -= end + 1;
            end = 0;

            if (packet != null)
                return packet;
        }

        // If we read all available bytes while looking for the end of a packet
        if (end == length)
        {
            if (length == buffer.Length)
                throw new InsufficientMemoryException();

            length += await Stream.ReadAsync(buffer, length, buffer.Length - length, cancellationToken);
        }
    }
}

我已将代码更新为在每次调用 ReadAsync 之间休眠,大约是上次读取所花费的时间:

var stopwatch = new Stopwatch();
var iteration = 0;

while (true)
{
    // ...
    var delay = stopwatch.Elapsed;
    stopwatch.Restart();

    if (iteration % 10 != 0)
        await Task.Delay(delay);

    length += await Stream.ReadAsync(buffer, length, buffer.Length - length, cancellationToken);
    stopwatch.Stop();
    iteration += 1;
}

这大大降低了 CPU 的使用率。这绝对是一种解决方法,因为它没有解决问题,但它确实有效。我很想听听其他人对此问题的回答或意见。