Skip to content

perf(generator): return the request task instead of awaiting it - #2302

Merged
ChrisPulman merged 2 commits into
mainfrom
perf/generated-method-task-propagation
Aug 13, 2026
Merged

perf(generator): return the request task instead of awaiting it#2302
ChrisPulman merged 2 commits into
mainfrom
perf/generated-method-task-propagation

Conversation

@glennawatson

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Performance optimisation in the source generator.

What is the new behavior?

No generated Refit method carries an async state machine; every method hands its task straight back to the caller.

  • The reflection fallback path emits a plain task-returning method. A method the generator cannot build inline now emits return (Task<T>)refitFunc(this.Client, args); rather than return await ((Task<T>)refitFunc(...)).ConfigureAwait(false); inside an async method. The awaited form allocated a state machine per call whose only job was to re-wrap the task the request builder had already produced.
  • Both generated request paths now agree. Inline request building already returned its task, so the emitted shape no longer depends on whether a method happens to be inline-eligible.
  • The emitters lost the async plumbing that had become constant. The return-shape helper now yields a statement prefix instead of an async flag plus a configure-await suffix, and the method-opening and return-statement emitters drop the parameters that fell out with it.

What is the current behavior?

Task-returning methods on the reflection fallback path are emitted async and await the request builder's task before returning it, allocating a second state machine on every call to that method.

What might this PR break?

  • Exceptions raised before dispatch now surface synchronously on the fallback path. A missing request builder, or a failure inside BuildRestResultFuncForMethod, previously surfaced as a faulted task; it now throws from the call itself. Callers that invoke a fallback method without awaiting it, and rely on catching around the await, need to catch around the call. Inline-built methods already behaved this way.
  • Stack traces no longer contain the generated method frame for fallback methods. The failed call is still identifiable from the exception: ApiExceptionBase.HttpMethod, .Uri, and the Refit.MethodName / Refit.RelativePathTemplate request options.

Checklist

  • I have read the Contribute guide
  • Tests have been added or updated (for bug fixes / features)
  • Docs have been added or updated (for bug fixes / features)
  • Changes target the main branch
  • PR title follows Conventional Commits

Additional information

The fallback emission shape had no test coverage, so the change would have gone unnoticed by the suite. SwitchOnFallbackReturnsTheRequestBuilderTaskWithoutAwaiting now pins it.

A sweep of the runtime for the same pattern found nothing to remove. Every remaining async method needs its state machine: a using or try scope that has to outlive the await, sequential awaits, work after the await, or an async iterator. Two methods forward a single call but convert ValueTask<T> to Task<T> across the boundary, where eliding would mean an unconditional AsTask() allocation in place of a state-machine box that is only allocated when the await actually suspends.

- Emit the reflection fallback path without async/await so a generated
  method hands back the request builder's task. Awaiting allocated a
  second state machine per call whose only job was to re-wrap that task.
- Drop the async and configure-await plumbing from the method-opening and
  return-statement emitters, which is now constant either way.
- Pin the fallback emission shape with a test; it had no coverage.
@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Aug 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.91%. Comparing base (51368ea) to head (e5ea23f).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2302      +/-   ##
==========================================
- Coverage   99.91%   99.91%   -0.01%     
==========================================
  Files         192      192              
  Lines       10031    10029       -2     
  Branches     1925     1924       -1     
==========================================
- Hits        10022    10020       -2     
  Partials        9        9              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ChrisPulman
ChrisPulman merged commit b455f65 into main Aug 13, 2026
18 checks passed
@ChrisPulman
ChrisPulman deleted the perf/generated-method-task-propagation branch August 13, 2026 06:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants