Pull up multiple performance intensive method calls

As a performance best practice, Guidewire recommends a technique called pulling up. With the pulling up technique, you examine your existing code to uncover performance intensive method calls that occur in multiple, lower-level methods. If you identify such a method call, pull it up into the higher level-method, so you call it only once. Cache the result in a local variable. Then, call the lower-level methods, and pass the result that you cached down to the lower-level methods as a context variable.

The following sample Gosu code suffers a performance problem. It pushes an expensive method call down to the lower-level routines, so the code repeats the expensive call three times.

function computeSomething() {  // Performance suffers with an expensive call pushed down.

   computeA()
  computeB()
  computeC()
}

 function computeA() {
  var expensiveResult = expensiveCall()  // Make the expensive call once.
  //do A stuff on expensiveResult
}

 function computeB() {
   var expensiveResult = expensiveCall()  // Make the same expensive call twice.
  //do B stuff on expensiveResult
}

 function computeC() {
  var expensiveResult = expensiveCall()  // Make the same expensive call three times.
  //do C stuff on expensiveResult
}

The following modified sample code improves performance. It pulls the expensive method call up to the main routine, which calls it once. Then, it passes the cached result down to the lower-level routines, as a context variable.

function computeSomething() {  // Performance improves with an expensive call pulled up.

   var expensiveResult = expensiveCall()  // Make the expensive call only once.

   computeA(expensiveResult) 
  computeB(expensiveResult)
  computeC(expensiveResult)
}

 function computeA(expensiveResult : ExpensiveResult) {  // Use the pushed down result.
  //do A stuff on expensiveResult
}

 function computeB(expensiveResult : ExpensiveResult) {  // Use the pushed down result.
  //do B stuff on expensiveResult
}

 function computeC(expensiveResult : ExpensiveResult) {  // Use the pushed down result.
  //do C stuff on expensiveResult
}