Inlined Code Question
-
Below is sample code from one of my recent apps. I used this pattern a lot when designing searchable objects. I know that by appearances, there will be more than two method calls involved. However, I've read that the C# compiler inlines short blocks of code. Will the "Contained" method be inlined at compile time?
/// <summary>
/// Returns a value indicating if the path contains the specified node type.
/// </summary>
/// <param name="nodeType">Specifies the node type.</param>
/// <returns></returns>
public bool Contains(BindingPathNodeType nodeType)
{
return IndexOf(nodeType) != -1;
}/// <summary>
/// Attempts to locate the first (zero-based) index position of the specified node type.
/// </summary>
/// <param name="nodeType">Specifies the node type.</param>
/// <returns></returns>
public int IndexOf(BindingPathNodeType nodeType)
{
int intCount = parsedPathNodes.Length;
for (int i = 0; i < intCount; i++)
if (parsedPathNodes[i].NodeType == nodeType)
return i;
return -1;
}The mind is like a parachute. It doesn’t work unless it’s open.
-
Below is sample code from one of my recent apps. I used this pattern a lot when designing searchable objects. I know that by appearances, there will be more than two method calls involved. However, I've read that the C# compiler inlines short blocks of code. Will the "Contained" method be inlined at compile time?
/// <summary>
/// Returns a value indicating if the path contains the specified node type.
/// </summary>
/// <param name="nodeType">Specifies the node type.</param>
/// <returns></returns>
public bool Contains(BindingPathNodeType nodeType)
{
return IndexOf(nodeType) != -1;
}/// <summary>
/// Attempts to locate the first (zero-based) index position of the specified node type.
/// </summary>
/// <param name="nodeType">Specifies the node type.</param>
/// <returns></returns>
public int IndexOf(BindingPathNodeType nodeType)
{
int intCount = parsedPathNodes.Length;
for (int i = 0; i < intCount; i++)
if (parsedPathNodes[i].NodeType == nodeType)
return i;
return -1;
}The mind is like a parachute. It doesn’t work unless it’s open.
http://www.ademiller.com/blogs/tech/2008/08/c-inline-methods-and-optimization/[^] "How to determine what get’s inlined? The short answer is that you can’t."
I know the language. I've read a book. - _Madmatt
-
Below is sample code from one of my recent apps. I used this pattern a lot when designing searchable objects. I know that by appearances, there will be more than two method calls involved. However, I've read that the C# compiler inlines short blocks of code. Will the "Contained" method be inlined at compile time?
/// <summary>
/// Returns a value indicating if the path contains the specified node type.
/// </summary>
/// <param name="nodeType">Specifies the node type.</param>
/// <returns></returns>
public bool Contains(BindingPathNodeType nodeType)
{
return IndexOf(nodeType) != -1;
}/// <summary>
/// Attempts to locate the first (zero-based) index position of the specified node type.
/// </summary>
/// <param name="nodeType">Specifies the node type.</param>
/// <returns></returns>
public int IndexOf(BindingPathNodeType nodeType)
{
int intCount = parsedPathNodes.Length;
for (int i = 0; i < intCount; i++)
if (parsedPathNodes[i].NodeType == nodeType)
return i;
return -1;
}The mind is like a parachute. It doesn’t work unless it’s open.
# DEFINE Contains(x) (IndexOf(x) != -1)
:-D -
# DEFINE Contains(x) (IndexOf(x) != -1)
:-DWhat is this? Some kind of shorthand?
The mind is like a parachute. It doesn’t work unless it’s open.
-
Below is sample code from one of my recent apps. I used this pattern a lot when designing searchable objects. I know that by appearances, there will be more than two method calls involved. However, I've read that the C# compiler inlines short blocks of code. Will the "Contained" method be inlined at compile time?
/// <summary>
/// Returns a value indicating if the path contains the specified node type.
/// </summary>
/// <param name="nodeType">Specifies the node type.</param>
/// <returns></returns>
public bool Contains(BindingPathNodeType nodeType)
{
return IndexOf(nodeType) != -1;
}/// <summary>
/// Attempts to locate the first (zero-based) index position of the specified node type.
/// </summary>
/// <param name="nodeType">Specifies the node type.</param>
/// <returns></returns>
public int IndexOf(BindingPathNodeType nodeType)
{
int intCount = parsedPathNodes.Length;
for (int i = 0; i < intCount; i++)
if (parsedPathNodes[i].NodeType == nodeType)
return i;
return -1;
}The mind is like a parachute. It doesn’t work unless it’s open.
If BindingPathNodeType is a reference type then there seems to be nothing that prevents inlining. But, as has been said, you still don't know for sure whether it will be inlined. What has not been said though is that it also depends on whether you NGEN, and whether it's 64bit, and 64bit NGEN is completely different from anything else, IIRC not even created by the same team (instead of being developed by the CLR team, it was developed by the Visual C++ team) 'Contains' is not a leaf method, while I have read nothing about non-leaf methods not being inlined, in other compilers inlining is often done on leaf methods first.
-
If BindingPathNodeType is a reference type then there seems to be nothing that prevents inlining. But, as has been said, you still don't know for sure whether it will be inlined. What has not been said though is that it also depends on whether you NGEN, and whether it's 64bit, and 64bit NGEN is completely different from anything else, IIRC not even created by the same team (instead of being developed by the CLR team, it was developed by the Visual C++ team) 'Contains' is not a leaf method, while I have read nothing about non-leaf methods not being inlined, in other compilers inlining is often done on leaf methods first.
Thanks for the heads up. I don't NGEN any of my code so I'm probably in trouble. :) I hate having to enter the same code block twice because: 1. It looks dirtier. 2. More lines to (possibly) debug.
The mind is like a parachute. It doesn’t work unless it’s open.
-
What is this? Some kind of shorthand?
The mind is like a parachute. It doesn’t work unless it’s open.
Yeah, kinda. :~