Information Technology Reference
In-Depth Information
Because the body of each clause has been factored into its own function,
that function can be JITed on demand rather than the first time BuildMsg
is called. Yes, this example is contrived for space, and it won't make much
difference. But consider how often you write more extensive examples: an
if statement with 20 or more statements in both branches of the if state-
ment. You'll pay to JIT both clauses the first time the function is entered.
If one clause is an unlikely error condition, you'll incur a cost that you
could easily avoid. Smaller functions mean that the JIT compiler compiles
the logic that's needed, not lengthy sequences of code that won't be used
immediately. The JIT cost savings multiplies for long switch statements,
with the body of each case statement defined inline rather than in separate
functions.
Smaller and simpler functions make it easier for the JIT compiler to sup-
port enregistration . Enregistration is the process of selecting which local
variables can be stored in registers rather than on the stack. Creating fewer
local variables gives the JIT compiler a better chance to find the best can-
didates for enregistration. The simplicity of the control flow also affects
how well the JIT compiler can enregister variables. If a function has one
loop, that loop variable will likely be enregistered. However, the JIT com-
piler must make some tough choices about enregistering loop variables
when you create a function with several loops. Simpler is better. A smaller
function is more likely to contain fewer local variables and make it easier
for the JIT compiler to optimize the use of the registers.
The JIT compiler also makes decisions about inlining methods. Inlining
means to substitute the body of a function for the function call. Consider
this example:
// readonly name property:
public string Name { get ; private set ; }
// access:
string val = Obj.Name;
The body of the property accessor contains fewer instructions than the
code necessary to call the function: saving register states, executing method
prologue and epilogue code, and storing the function return value. There
would be even more work if arguments needed to be pushed on the stack
as well. There would be far fewer machine instructions if you were to use
a public field.
 
Search WWH ::




Custom Search