Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "General tips when writing performance critical code"

Line 7: Line 7:
  
  
2. Avoid creating object via auto-boxing. For instance. do not invoke:  
+
2. Avoid creating object via autoboxing. For instance. do not invoke:  
 
  EObject copy = ECoreUtil.copy(EObject);
 
  EObject copy = ECoreUtil.copy(EObject);
 
The method invokes the eGet method on the EObject which creates new Objects. Instead, commonly copied objects such as IntegrationLabelValues now have a copy method:
 
The method invokes the eGet method on the EObject which creates new Objects. Instead, commonly copied objects such as IntegrationLabelValues now have a copy method:
 
  IntegrationLabelValue copyVal = val.copy();
 
  IntegrationLabelValue copyVal = val.copy();
This method does not do auto-boxing of the double attributes inside.
+
This method does not do autoboxing of the double attributes inside.
  
 
3. Same tip, do not call  
 
3. Same tip, do not call  
 
  double d = EObject.eGet(EAttribute) or  
 
  double d = EObject.eGet(EAttribute) or  
 
  EObject.eSet(EAttribute, double>  
 
  EObject.eSet(EAttribute, double>  
on EMF objects. You end up creating unnecessary objects. Again, IntegrationLabelValues (used heavily when running simulations) have a get and set method for doubles that does not do the auto-boxing:
+
on EMF objects. You end up creating unnecessary objects. Again, IntegrationLabelValues (used heavily when running simulations) have a get and set method for doubles that does not do the autoboxing:
 
  IntegrationLabelValue ival = ...
 
  IntegrationLabelValue ival = ...
 
  ival.set(someEAttribute, double)
 
  ival.set(someEAttribute, double)

Revision as of 14:23, 12 April 2012

Performance critical code includes any code used during execution of a STEM simulation. Here are some tips for developers to avoid creating unnecessary objects which increases the workload on the Java garbage collector. The tips were mostly compiled from instrumenting and fixing code for the 1.3.1 release:

1. Avoid creating iterators when looping. For instance, a loop such as

for(Object o:someList) 

creates a new iterator object. Instead, just loop using a variable, e.g.

for(int i=0;i<someList.size();++i) Object o = someList.get(i);


2. Avoid creating object via autoboxing. For instance. do not invoke:

EObject copy = ECoreUtil.copy(EObject);

The method invokes the eGet method on the EObject which creates new Objects. Instead, commonly copied objects such as IntegrationLabelValues now have a copy method:

IntegrationLabelValue copyVal = val.copy();

This method does not do autoboxing of the double attributes inside.

3. Same tip, do not call

double d = EObject.eGet(EAttribute) or 
EObject.eSet(EAttribute, double> 

on EMF objects. You end up creating unnecessary objects. Again, IntegrationLabelValues (used heavily when running simulations) have a get and set method for doubles that does not do the autoboxing:

IntegrationLabelValue ival = ...
ival.set(someEAttribute, double)
double v = ival.get(someEAttribute)

EObject.set(EAttribute, ...) will invoke eSet if the EAttribute is not of EType Double. A warning however, get(EAttribute) will throw an exception if EAttribute is not if EType Double. We can extend the types supported in the future if needed for new kinds of integration label values (e.g. integers, longs etc.)

4. Keep a pool of objects around instead of creating new ones. There is a new class org.eclipse.stem.core.STEMObjectPool for this purpose. You can create a new pool as such:

STEMObjectPool pool =  new STEMObjectPool(10, 5) {
 @Override
 protected Object createNewObject() {
   return new XYZ();
 }
}   

Override the createNewObject() method to specify the types of objects kept in the pool. The two parameters in the constructor (10 and 5 in this example) is the initial size of the pool and increment size when growing the pool. To retrieve an available object from the pool:

Object o = pool.get();
// Reset the object here before you use it to make sure you clear out any state left by the last user of the object.

It's important to remember to return the object to the pool when you're done with it:

pool.release(o);

5. Do not use String concatenation, e.g.

String s = "a"+"b"+"c";

Lots of String objects are created behind the scenes when doing this. Instead, use the StringBuilder() and its append(...) methods.

Back to the top