Scala-reflection performance.

419 views
Skip to first unread message

Aleksey Nikiforov

unread,
Jan 25, 2013, 6:58:04 PM1/25/13
to scala-debate
Real-world use case:
Extract all the values of a target type from instances at rutime. Instance class and val names are not know at compile time.

Java based solution allowes to cache Methods. Retrieving a field value is simple and efficient: cacheMethod.invoke(instance)

Scala based solution only allows to cache MethodSymbols

Aleksey Nikiforov

unread,
Jan 25, 2013, 7:08:02 PM1/25/13
to scala-debate
...continued:

Scala based solution only allows to cache MethodSymbols. Retrieving a field is also simple:
instanceMirror.reflectField(cachedMethodSymbol).get

However the Scala based solution is over 40 times slower than Java counterpart, and is not usable in production.

It would greatly improve performance if a new FieldMirrors could be created with another instance: fieldMirror.forInstance(instance).get. As long as it is possible to cache method handles any other (possibly more elegant) solution would make Scala reflection much faster.

Jason Zaugg

unread,
Jan 26, 2013, 2:33:15 AM1/26/13
to Aleksey Nikiforov, scala-debate
This facility was added in the master branch, which is targetting 2.11.x  [1].

We can't add it to 2.10.1 because we are maintaining forward binary compatibility along the 2.10.x series, so we can't add public methods.

-jason

Reply all
Reply to author
Forward
0 new messages