Skip to content

Use .tail prefix to optimize JIT-generated code #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions LazyProxy/LazyProxyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ private static TypeBuilder AddMethods(this TypeBuilder typeBuilder, Type type, F
generator.Emit(OpCodes.Ldarg, i);
}

generator.Emit(OpCodes.Tail);
Copy link
Collaborator

@hypercodeplace hypercodeplace Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering, why is the difference with OpCodes.Tailcall? (I could't find any info about OpCodes.Tail in the docs).
And also, does this optimization work for all calls or for the recursion calls only?

Copy link
Contributor Author

@SergeiPavlov SergeiPavlov Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, OpCodes.Tailcall is correct vor .NET (fixed the code)
And .Tail is for Cecil library used in DO

It works only before sequence call, ret
And this is not always recursive

I have already applied such change before and then reverted it c67bd84

My new understanding based on Debugger & StackTraces from dumps is:
Without .tail prefix the JIT optimizer SOMETIME can guess to do it, but not in 100% cases.
tail. prefix guarantees this kind of optimization

generator.Emit(OpCodes.Callvirt, method);
generator.Emit(OpCodes.Ret);

Expand Down