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

Conversation

SergeiPavlov
Copy link
Contributor

The effect is shown here:
servicetitan/dataobjects-net#156

@@ -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

@botinko
Copy link
Contributor

botinko commented Nov 6, 2023

I cannot estimate how much this change may affect the reliability of the application. As noted in the article, this optimization is not used by default. This means that the behavior of the JIT compiler may not be sufficiently tested. Also I afraid of increased JIT time because of optimization.

@SergeiPavlov
Copy link
Contributor Author

SergeiPavlov commented Nov 7, 2023

I cannot estimate how much this change may affect the reliability of the application. As noted in the article, this optimization is not used by default. This means that the behavior of the JIT compiler may not be sufficiently tested. Also I afraid of increased JIT time because of optimization.

.tail prefix tells JIT just to convert call, ret into jmp without additional code analyzis.
It means less work for JIT, because by default JIT has make a decision to do it or not

Looks like no potential flaw. This prefix is actively used by functional-language compilers like F#, where tail recursion is critical feature.

@SergeiPavlov SergeiPavlov merged commit 2ff5097 into master Nov 7, 2023
@SergeiPavlov SergeiPavlov deleted the tail branch November 7, 2023 23:59
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.

3 participants