Game Development Reference
In-Depth Information
between the sender and receiver. The next step is to protect callee-saved registers
and switch the threading mode so that the garbage collector does not block
unmanaged threads. Lastly, a frame to handle exceptions is created to supervise
calls into managed code. The events equate to roughly 30 x86 instructions when
using P/Invoke (roughly 10 when marshaling is not required), and roughly 60 x86
instructions when using COM Interop . Therefore, it is important to use P/Invoke
over COM Interop whenever possible to speed up the calls between managed and
unmanaged code.
The biggest slowdown occurs during data translation, such as converting text from
ASCII to Unicode. Classes with explicit layout are extremely cheap, and primitive
types require almost no marshaling at all. Blittable types are those that can be
transferred directly across managed and unmanaged code with no marshaling at
all. These types are byte , sbyte , double , float , long , ulong , int , uint , short , and ushort .
You can also freely pass value types and single-dimensional arrays that contain
blittable types.
Minimize Exception Throwing
One of the best features of the .NET platform is the exception handling model that
is available to all applications. This model offers the ability to develop robust
applications that can handle and respond to exceptions and errors gracefully in
almost all situations. However, this model must be used carefully, or some signifi-
cant performance costs can be introduced into your application. Throwing excep-
tions is expensive in terms of performance, so throw as few as possible. You can
check how many exceptions your application throws at runtime through the use
of Performance Counters ( perfmon.exe ). Also, be aware that the .NET runtime can
throw its own exceptions. It is advisable to use Performance Counters to check
this, and use the debugger to locate the source of the exceptions.
One myth that circulates around developers working with managed code is that
try/catch blocks introduce performance overhead. The truth is that you only incur
a performance cost when an actual exception is thrown. You can use as many
try/catch blocks as you want. Do not use exceptions to control program flow.
Thoughts About NGen
The methods of a managed application are Just-in-Time compiled (JIT'ed) the
first time they are used during runtime. This dynamic compilation can lead to a
significant startup penalty if the application invokes a lot of methods during startup.
Search Nedrilad ::




Custom Search