Skipped argument evaluation for calls replaced by interceptors #10031
Unanswered
ufcpp
asked this question in
Language Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
I'd like to propose a language feature that enables removal of argument-evaluation dead code when a call is replaced by Interceptors.
Today, Interceptors can replace the call target, but argument construction/evaluation may still remain in IL even when completely unused, causing code bloat (Dead code remains in the compiled output).
Motivation
In some source-generator scenarios, the result can be computed at compile time (e.g., compile-time string conversion/formatting), making the original arguments entirely unnecessary.
However, the original argument expression may still be emitted/evaluated, unlike
ConditionalAttributewhere omitted calls also omit argument evaluation.As a concrete example:
For interpolated strings specifically, the culture-dependent formatting issue and why constant interpolated strings are hard has been discussed in csharplang:
#2951 (comment)
I tried to solve this issue using Interceptors:
https://github.com/ufcpp/SourceGeneratorDemo/blob/main/ConstantInterpolation/
This proposal focuses on providing a mechanism to remove the remaining dead argument-evaluation code in that kind of solution.
Proposal
Introduce a parameter attribute, e.g.
[Unused], that is similar to Caller Info attributes but has the opposite effect: it replaces actual arguments with default values.[Unused]parameters are expected not to be evaluated/emitted at the call site.ConditionalAttribute, any side effects in the omitted argument expressions are also eliminated.Example (conceptual)
For example, suppose we have a method like the following.
For this, the generator produces an interceptor method like the following.
Without the
[Unused]mechanism, code at the call site would still be compiled into code like the following.With this proposal, due to the
[Unused]effect on the generated interceptor parameters,evaluation/emission of the corresponding original arguments is omitted:
Why this shape
Design constraints / questions
Expected benefit
ConditionalAttributecall omission behavior.Beta Was this translation helpful? Give feedback.
All reactions