ARTICLE
Best Practices of Compact Framework
In this article I am sharing few recommendations which we are using in our day to day development activity of compact framework based applications.
Developing application for mobile devices such as pocket PC or mobile phone is very interesting, I am feeling excited while developing applications target to compact framework. Mobile devices are memory constraint devices it is very important for every developer to pay attention on efficient memory utilization and application performance. Recently I got few mails from developers to know how to leverage performance of mobile device application? In this article I am sharing few recommendations which we are using in our day to day development activity of compact framework based applications.
We are extensively using base class library collection classes to store a group of objects, if we store any value type in collection class lets say storing int into ArrayList, causes overhead of boxing and unboxing, to avoid this it is better to define a custom class and make the value type as member variable and store the class instance in an ArrayList. We can take the advantage of generics in compact framework 2.0 to avoid boxing/unboxing and up-casting/down-casting of collection objects.
Another important pointer regarding collection class is to pre-size the collection before inserting objects to it. It is recommend to define the size of collection class rather than depending upon the default size (default capacity of ArrayList is 4). Once your collection grows beyond the default capacity, it gets resized frequently depending up on the type of object storage and the auto expansion of collection is a overhead process, the recommend solution is pre-size the collection class before use. Similarly use FOR instead of FOREACH because foreach involves sequence of virtual method (GetEnumerator(), get_Current(), and MoveNext()) calls through IEnumerator interface.
Virtual method call is a performance overhead for your application because virtual methods are never inlined by JIT when a virtual method gets call, compact framework walks the class and interfaces hierarchy looking for requested method by name and call signature, other performance overhead is it is approximately two time slower then static and instance method call. If you need a virtual call the best thing is to implement functionality as much as you can in a single virtual method rather than splitting into multiple methods and do not make most frequently called methods as virtual. Similarly PInvoke and COM method calls are 6 time slower then managed instance method calls, native method call is faster than instance method call. It is recommend to design your class based on the above statistics to leverage performance.
If you are using structure and calling ToString() or Equal() method it is better to override these methods otherwise it leads to boxing & un-boxing issue.
Direct field access is faster than having property call because property is nothing but getter and setter methods. The simple properties are inlined by JIT but this assumption may not work always if it involves virtual setter/getter method call or any complex operation. In order to inline a method your application must have the IL code less then 16 bytes, no branching ( typically "if"), no local variables, no exception handler etc. It is not possible to predict 100% accuracy whether a method will be inlined or not, method inlining never occurred if your application is running under debug mode. So if we can keep performance critical methods as simple as possible then there might be a chance for method inlining.
We frequently use DateTime in our application development, to leverage the performance of DateTime store it in binary format using ticks but local times are exception to this recommendation, for DateTime serialization if you know the exact format it is better to specify parsing. Take the advantage of DateTime.ParseExact() to avoid DateTime parser to apply sequential culture specific format to date and time string. The same rule is applicable to numeric parsing also.
For GUI performance, try to reduce number of method calls during initialization and for certain controls you need to do manual modification of design generated code to leverage performance. Always try to use BeginUpdate() & EndUpdate() for ListView and Tree View. Use SuspendLayout/ResumeLayout while repositioning controls. For GUI performance I recommend to refer
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/netcfimproveformloadperf.asp
Consuming web services is overhead for compact framework based applications because compact framework uses reflection mechanism to examine the service's proxy and it never cache this result which usually full compact framework does. It is strongly recommended to not to have multiple instances of the same web service in order to increase the performance of web service. Try to maintain a single global instance of the web service, avoid sending datasets over Web Service, because it pulls in a large amount of XML and uses complex algorithm for loading data. If you are sending/receiving a DataSet, ensure the schema is serialized alternatively or manually serialize dataset as an XML string before making web service call.
For compact framework performance related information please refer