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 7653051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:55:39+00:00 2026-05-31T11:55:39+00:00

Is there an attribute I can use to tell the compiler that a method

  • 0

Is there an attribute I can use to tell the compiler that a method must always be optimized, even if the global /o+ compiler switch is not set?

The reason I ask is because I’m toying with the idea of dynamically creating a method based on the IL code of an existing method; the manipulation I want to do is reasonably easy when the code is optimized, but becomes significantly harder in non-optimized code, because of the extra instructions generated by the compiler.


EDIT: more details about the non-optimizations that bother me…

Let’s consider the following implementation of the factorial function:

static long FactorialRec(int n, long acc)
{
    if (n == 0)
        return acc;
    return FactorialRec(n - 1, acc * n);
}

(Note: I know there are better ways to compute the factorial, this is just an example)

The IL generated with optimizations enabled is quite straightforward:

IL_0000:  ldarg.0     
IL_0001:  brtrue.s    IL_0005
IL_0003:  ldarg.1     
IL_0004:  ret         
IL_0005:  ldarg.0     
IL_0006:  ldc.i4.1    
IL_0007:  sub         
IL_0008:  ldarg.1     
IL_0009:  ldarg.0     
IL_000A:  conv.i8     
IL_000B:  mul         
IL_000C:  call        UserQuery.FactorialRec
IL_0011:  ret         

But the unoptimized version is quite different

IL_0000:  nop         
IL_0001:  ldarg.0     
IL_0002:  ldc.i4.0    
IL_0003:  ceq         
IL_0005:  ldc.i4.0    
IL_0006:  ceq         
IL_0008:  stloc.1     
IL_0009:  ldloc.1     
IL_000A:  brtrue.s    IL_0010
IL_000C:  ldarg.1     
IL_000D:  stloc.0     
IL_000E:  br.s        IL_001F
IL_0010:  ldarg.0     
IL_0011:  ldc.i4.1    
IL_0012:  sub         
IL_0013:  ldarg.1     
IL_0014:  ldarg.0     
IL_0015:  conv.i8     
IL_0016:  mul         
IL_0017:  call        UserQuery.FactorialRec
IL_001C:  stloc.0     
IL_001D:  br.s        IL_001F
IL_001F:  ldloc.0     
IL_0020:  ret         

It is designed to have only one exit point, at the end. The value to return is stored in a local variable.

Why is this an issue? I want to dynamically generate a method that includes tail call optimization. The optimized method can easily be modified by adding the tail. prefix before the recursive call, since there nothing after the call except ret. But with the unoptimized version, I’m not so sure… the result of the recursive call is stored in a local variable, then there’s a useless branch that just jumps to the next instruction, the the local variable is loaded and returned. So I have no easy way of checking that the recursive call really is the last instruction, so I can’t be sure that tail call optimization can be applied.

  • 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-05-31T11:55:41+00:00Added an answer on May 31, 2026 at 11:55 am

    If the method you’ll be using as your template for the dynamic method is relatively simple – and without dependencies on other methods. Then just put it in it’s own assembly and turn on optimization for just that assembly.

    As far as the original issue, since MSIL is a stack based language. And the specs guarantee stack state at the ret statement you can be 100% sure that you can add a tail prefix without issue. However, it’s also unlikely to actually add any benefit as I haven’t really seen the JIT use the tail prefix to actually optimize the finally jitted code.

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

Sidebar

Related Questions

Is there any attribute associated with the starting time I can get from the
How can I override class attribute access in python? P.S. Is there a way
Is there a way that I can add non-static data to dataannotation attributes (either
Is there a pragma or debugger attribute which will allow the debugger to not
HTML5 has a new global attribute, hidden , which can be used to hide
Is there an attribute I can decorate a single property on my model to
Can anyone tell me why I am getting error when I am validating even
Is there a way to tell EF not to update SSDL for the single
Is there a way I can change attributes on a model without ever firing
There is no align attribute with font tag because It is an inline element.

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.