Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9024685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:05:11+00:00 2026-06-16T06:05:11+00:00

I have a simple performance test, that indirectly calls WriteAsync many times. It performs

  • 0

I have a simple performance test, that indirectly calls WriteAsync many times. It performs reasonably as long as WriteAsync is implemented as shown below. However, when I inline WriteByte into WriteAsync, performance degrades by about factor 7.

(To be clear: The only change that I make is replacing the statement containing the WriteByte call with the body of WriteByte.)

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.

public sealed override async Task WriteAsync(
    byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
    var writeBuffer = this.WriteBuffer;
    var pastEnd = offset + count;

    while ((offset < pastEnd) && ((writeBuffer.Count < writeBuffer.Capacity) ||
        await writeBuffer.FlushAsync(cancellationToken)))
    {
        offset = WriteByte(buffer, offset, writeBuffer);
    }

    this.TotalCount += count;
}
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 < Frame.InvalidStart)
        {
            this.crc = Crc.AddCrcCcitt(this.crc, currentByte);
            ++offset;
        }
        else
        {
            currentByte = Frame.EscapeByte;
            this.previousWasEscapeByte = true;
        }
    }

    writeBuffer[writeBuffer.Count++] = currentByte;
    return offset;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T06:05:12+00:00Added an answer on June 16, 2026 at 6:05 am

    async methods are rewritten by the compiler into a giant state machine, very similar to methods using yield return. All of your locals become fields in the state machine’s class. The compiler currently doesn’t try to optimize this at all, so any optimization is up to the coder.

    Every local which would have sat happily in a register is now being read from and written to memory. Refactoring synchronous code out of async methods and into a sync method is a very valid performance optimization — you’re just doing the reverse!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple test that runs a query 5000 times. The linq version
I have a very simple server/client performance test using boost::asio on Windows and it
I have done simple performance test on my local machine, this is python script:
I have done a simple experiment to test MongoDB's performance and disk usage. I
I have some questions about the performance of this simple python script: import sys,
I have simple php validation form that is halfway working. If you leave the
I have simple win service, that executes few tasks periodically. How should I pass
I have a very simple demo working that uses Webkit transforms and transitions for
I have a simple C# application that uses UDP multicast in a single-receiver, single-sender
I have a test in my JUnit suite which warns me about a performance

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.