Did the compiler inline my method?
-
Hello, Is there a (relatively simple) way to know whether the compiler has inlined my method? (Actually I want to make sure that it did...)
No, and you will have no guarantee that a function will be inlined. Actually, it's not job of the C# compiler, but rather the JIT that does the inlining at runtime, if it decides to do so. But you have no control over it and can not rely on a function being inlined. regards
-
No, and you will have no guarantee that a function will be inlined. Actually, it's not job of the C# compiler, but rather the JIT that does the inlining at runtime, if it decides to do so. But you have no control over it and can not rely on a function being inlined. regards
Greeeg wrote:
But you have no control over it and can not rely on a function being inlined.
Well, there is one sure fire way, but that involves getting hired by Microsoft in a position on the CLR team and writing code to do this.
Deja View - the feeling that you've seen this post before.
-
No, and you will have no guarantee that a function will be inlined. Actually, it's not job of the C# compiler, but rather the JIT that does the inlining at runtime, if it decides to do so. But you have no control over it and can not rely on a function being inlined. regards
Well, thanks a lot. But since, I'm very concerned about performance, I'd appreciate more of your expertise. There are some methods that I'm writing just for modularity reasons. Each is just a single line of code. For example:
class X {
public int Value { get { return getExternalValue(); } }
private int getExternalValue() {
// call another method in just one line
}
}I am thinking that the JIT should be intelligent enough to actually inline both methods. But is this assumption true? Or should I inline the method myself? Many thanks to all helpful guys here in CP! I really appreciate your help. :rose: Know me better
-
Greeeg wrote:
But you have no control over it and can not rely on a function being inlined.
Well, there is one sure fire way, but that involves getting hired by Microsoft in a position on the CLR team and writing code to do this.
Deja View - the feeling that you've seen this post before.
Pete O'Hanlon wrote:
Well, there is one sure fire way, but that involves getting hired by Microsoft in a position on the CLR team and writing code to do this.
The first thing I'd do with this new job would be to introduce some kind of
forceinline
directive :-D -
Well, thanks a lot. But since, I'm very concerned about performance, I'd appreciate more of your expertise. There are some methods that I'm writing just for modularity reasons. Each is just a single line of code. For example:
class X {
public int Value { get { return getExternalValue(); } }
private int getExternalValue() {
// call another method in just one line
}
}I am thinking that the JIT should be intelligent enough to actually inline both methods. But is this assumption true? Or should I inline the method myself? Many thanks to all helpful guys here in CP! I really appreciate your help. :rose: Know me better
hosamaly wrote:
I am thinking that the JIT should be intelligent enough to actually inline both methods. But is this assumption true?
Nobody (apart from the Microsoft CLR team) knows exactly under which circumstances the JIT optimizes code. While the JIT probably is intelligent enough to see that the function can be inlined, it still might decide not to do so based on other parameters. So in your case, if you're really concerned about performance you probably should inline it yourself - it'll always be a compromise between modularity/readability and performance. This article[^] and this blog entry[^] talk a bit about the JIT method inlining, you may want to take a look at them. regards
-
Well, thanks a lot. But since, I'm very concerned about performance, I'd appreciate more of your expertise. There are some methods that I'm writing just for modularity reasons. Each is just a single line of code. For example:
class X {
public int Value { get { return getExternalValue(); } }
private int getExternalValue() {
// call another method in just one line
}
}I am thinking that the JIT should be intelligent enough to actually inline both methods. But is this assumption true? Or should I inline the method myself? Many thanks to all helpful guys here in CP! I really appreciate your help. :rose: Know me better
You are micro-optimising. In any normal situation it will not make any real difference if the method is inlined or not. If you really need that litte extra performance, you should probably not only inline this method, but the method that you are calling also...
Despite everything, the person most likely to be fooling you next is yourself.
-
You are micro-optimising. In any normal situation it will not make any real difference if the method is inlined or not. If you really need that litte extra performance, you should probably not only inline this method, but the method that you are calling also...
Despite everything, the person most likely to be fooling you next is yourself.
You're right. I'm just trying to write micro-optimized code that is maintainable to some level. I'm introducing a wrapper layer for some external functions, but performance is at premium, so I'm trying to optimize it as much as possible, without exposing the external interfaces to other layers.
Many thanks to all helpful guys here in CP! I really appreciate your help. :rose: Know me better
-
Pete O'Hanlon wrote:
Well, there is one sure fire way, but that involves getting hired by Microsoft in a position on the CLR team and writing code to do this.
The first thing I'd do with this new job would be to introduce some kind of
forceinline
directive :-DGreeeg wrote:
introduce some kind of forceinline directive
That would be nice.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
-
You're right. I'm just trying to write micro-optimized code that is maintainable to some level. I'm introducing a wrapper layer for some external functions, but performance is at premium, so I'm trying to optimize it as much as possible, without exposing the external interfaces to other layers.
Many thanks to all helpful guys here in CP! I really appreciate your help. :rose: Know me better
If you really need to optimise the code so much that you need the methods to be inlined, you are way beyond maintainable code. If you are still thinking of maintainable code, you should not bother with the tiny performance difference that inlining methods means.
Despite everything, the person most likely to be fooling you next is yourself.