Note that there are some explanatory texts on larger screens.

plurals
  1. POHuge performance hit for inlining sync method into async method
    text
    copied!<p>I have a simple performance test, that indirectly calls <code>WriteAsync</code> many times. It performs reasonably as long as <code>WriteAsync</code> is implemented as shown below. However, when I inline <code>WriteByte</code> into <code>WriteAsync</code>, performance degrades by about factor 7.</p> <p>(To be clear: The <strong>only</strong> change that I make is replacing the statement containing the <code>WriteByte</code> call with the body of <code>WriteByte</code>.)</p> <p>Can anybody explain why this happens? I've had a look at the differences in the generated code with Reflector, but nothing struck me as so totally different as that it would explain the huge perf hit.</p> <pre><code>public sealed override async Task WriteAsync( byte[] buffer, int offset, int count, CancellationToken cancellationToken) { var writeBuffer = this.WriteBuffer; var pastEnd = offset + count; while ((offset &lt; pastEnd) &amp;&amp; ((writeBuffer.Count &lt; writeBuffer.Capacity) || await writeBuffer.FlushAsync(cancellationToken))) { offset = WriteByte(buffer, offset, writeBuffer); } this.TotalCount += count; } </code></pre> <pre><code>private int WriteByte(byte[] buffer, int offset, WriteBuffer writeBuffer) { var currentByte = buffer[offset]; if (this.previousWasEscapeByte) { this.previousWasEscapeByte = false; this.crc = Crc.AddCrcCcitt(this.crc, currentByte); currentByte = (byte)(currentByte ^ Frame.EscapeXor); ++offset; } else { if (currentByte &lt; Frame.InvalidStart) { this.crc = Crc.AddCrcCcitt(this.crc, currentByte); ++offset; } else { currentByte = Frame.EscapeByte; this.previousWasEscapeByte = true; } } writeBuffer[writeBuffer.Count++] = currentByte; return offset; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload