Pages

Friday, November 03, 2006

Coding for Performance

Here are some performance tips gathered from various sources and personal experience. I have sorted them by category (General, J5SE, J5EE)

General
  1. Always use the simplest classes possible to get the Job Done.

  2. Never code your own frameworks unless the performance is lacking. Reuse code and frameworks.

  3. Use open source frameworks which are established and tested.

  4. "Never do today what can be put off till tomorrow. " - Aaron Burr


    • If a class proves difficult to code, put it off until you have a rest. You can then look at it with a fresh set of eyes

    • Delegate the hard parts to code to another class

    • Do not attempt to resolve all scenarios while coding, i.e., Wait to do locale specific encodings until after the initial code is complete.


  5. Place design notes in your code. Explain the performance requirements in your comments. If there are specific SRS requirements, note the number, date, and revision of the SRS document.

  6. Avoid object creation and destruction except as necessary. Reuse existing objects.

  7. Learn Collections and use them correctly. Use "lightweight" collections and avoid "heavyweight" collections where synchronization is not required.

  8. Initialize objects using a constructor with the least amount of requirements. If you need to use a number of parameters other than the default values, consider using the inverse of the object. In other words, if the object contains an int which is initialized to zero (0), then use the object with the default value and treat initialization parameters as the exception.

  9. Use findbugs to find common errors and performance problems.

  10. Reduce the distance between objects during operation. It is better to perform complex operations locally.

  11. Use System.currentTimeMillis() for performance measurements to determine execution time

  12. Use the -verbose:gc flag on the JVM to determine if the heap size is too small.

  13. Use constants where possible by using static final in the variable declaration.

  14. Use Enum instead of integer constants. Enums are more flexible and are typesafe.

  15. Avoid casting and using instanceof

  16. Use synchronized methods instead of code blocks.

  17. Avoid synchronized calls within a synchronized method or code block.

  18. Avoid using synchronization over IO operations except as required to maintain correct operation. For example: JPA inside a servlet.

  19. Turn off auto-commit and use transactions to improve throughput.

  20. Use -Xms and -Xmx flags to set the minimum and maximum heap sizes. Try to size appropriately to prevent wasting resources.


J5SE

Looping


  1. Do not recalculate constants inside a loop.

  2. "Fast Fail" - If a method fails, or throws an exception have it exit the loop quickly. Break loops early.

  3. Use local variables in loops. javac can assign an exact location of a local variable for a method at compile time.


Strings


  1. Avoid using Strings when you are modifying them. Strings are immutable. Therefore to "modify" a String, object creation and destruction must occur. Use StringBuilder and StringBuffer when Strings must be modified.

  2. Create Strings using the short form syntax to avoid creating additional objects.

    For example use: String s1 = "ABC";

    instead of: String s1 = new String("ABC");

  3. Never use String or StringBuffer for parsing characters. Use a character array.

  4. Try to set the StringBuilder or StringBuffer to the size required, or maximum size required during initialization to prevent a performance penalty while resizing.

  5. Avoid using StringTokenizer if there is a performance requirement. Use a more specific (custom) tokenizer to split Strings. StringTokenizer is a generic utility that is synchronized internally.

  6. Use StringBuilder instead of StringBuffer unless synchronization is required.


Collections


  1. Avoid using generic object collections. Use generics with collections to avoid having to cast objects.

  2. Use a LinkedList over an ArrayList if there a large number of insertions and deletions.

  3. Use a HashMap instead of a TreeMap unless there is a requirement to maintain a sort order.

  4. Use a HashSet over a TreeSet unless there is a requirement to maintain a sort order.

  5. When using Vector, try to set the initial size to the expected maximum size to prevent having to grow the Vector. If you must grow a Vector use a reasonable value to increase the size.

  6. It is extremely important to try to appropriately size a HashTable to prevent reorganization.


J5EE


  1. Reduce the number of network operations by returning complete results rather than smaller intermediate results.

  2. If database design constraints impose a specific database, use the advantages of the database where possible.


    • If operations are performed on the database, consider using stored procedures and making JDBC calls.

    • Do not use Entity Beans unless you must, use Java Persistence API (JPA) instead.

    • Do not use Java Persistence API (JPA) unless you need it, or want to use some of its advanced capabilities.

    • Limit the subset of data required to the minimum required for your program. Do not pull a whole row of data from table when you require only a few fields.


  3. When given a choice use local interfaces and local method calls on EJBs

  4. Shorten the distance between servers. Try to maintain dependent servers as close as possible. In clustered environments, try to keep remote communications on a separate private network interface.

  5. It is generally better to use a coarser stateless session bean to avoid JNDI lookups for fine grained operations.
  6. Avoid stateful session beans except as necessary.

  7. Set timers for non-activity on stateful session beans as low as possible to prevent "dead" connections waiting to timeout.

  8. Use Data Transfer Objects (DTO) to maintain granularity. DTOs must be Serializable.

1 comments :