samedi 26 mars 2016

Is reflection is slow/unusable "out of the box"?

Some background. I need to parse an xml response from some api, looking like kinda like this:

    <Customer> 
       ...
       <Telephone1></Telephone1>
       <Telephone2></Telephone2> 
       <Telephone3></Telephone3>
       ...
    </Customer>

I am using scalaxb to unmarshall this into a case class, but I don't want the downstream code exposed to this sort of badness, so I want to replace this "row" object with something like

 case class Customer(
   name: String,
   ...
   telephones: Seq[String]
   ...
 )

There are many different objects like this, and I don't want to be writing all these repetitions of Seq(telephone1, telephone2, telephone3 ...) for different fields by hand, but would prefer to make a generic converter, that can handle all types using reflection.

Now, this is (going to be) production code, and I am worried about performance. I have not used reflection in production for many years, not since java 1.4, I think, and back than it was unusably slow. People had to use special libraries, like CGLib, in order to get close to decent performance and scalability.

Now, my question is whether this is still the case. If I end up using reflection in scala, will the standard implementation that scala (or java) has work, or am I going to have to use tricks like CGLib, building my own method caches etc.?





Aucun commentaire:

Enregistrer un commentaire