Skip to content

Commit 1cb979e

Browse files
Ugo Matrangolomwunsch
authored andcommitted
Cache Context methods
1 parent 25aa276 commit 1cb979e

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/main/scala/com/gilt/handlebars/HandlebarsVisitor.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,15 @@ class HandlebarsVisitor[T](context: Context[T],
215215
* A wrapper around the literal context as referenced from templates.
216216
*/
217217
trait Context[+T] {
218+
import java.lang.reflect.Method
219+
218220
private val logger: Logger = LoggerFactory.getLogger(getClass)
219221

220222
// the wrapped context with which user code deals
221223
val context: T
222224

225+
val methodsCache: Map[String, Method] = (context.getClass.getMethods map { m => (m.getName + m.getParameterTypes.length, m) }).toMap
226+
223227
// Option, since the root context does
224228
// not have a parent, but all others do
225229
val parent: Option[Context[Any]]
@@ -242,11 +246,7 @@ trait Context[+T] {
242246
}
243247
}
244248

245-
def getMethod[A](name: String, args: List[A] = Nil) = {
246-
context.getClass.getMethods find { method =>
247-
method.getName == name && method.getParameterTypes.length == args.length
248-
}
249-
}
249+
def getMethod[A](name: String, args: List[A] = Nil) = methodsCache.get(name + args.length)
250250

251251
// these are lazy vals instead of functions
252252
// since they don't take up too much space

0 commit comments

Comments
 (0)