ArpNetworking / commons

Apache License 2.0
1 stars 2 forks source link

Clone Weaving #2

Open vjkoskela opened 8 years ago

vjkoskela commented 8 years ago

The current clone functionality in OvalBuilder is implemented with Java reflection. Consequently, it is extremely slow and can significantly degrade application performance if invoked frequently. The clone should be rewritten using code weaving. This will be the least disruptive to existing use cases.

Alternatives that were considered included implementing Serializable or using another serialization library (e.g. Jackson). These would require changes to classes being cloned.

The base OvalBuilder should have a public clone method with that throws a OperationNotSupportedException (or some such). The weaver will find subclasses of OvalBuilder and override the method with generated code. Essentially this is copy-constructor generation through a builder.

The existing clone(instance, builder) contract can be expressed with a call to clone builder.clone(instance). The other variant of clone, namely clone(instance), will still need to reflectively instantiate the builder and should be marked deprecated.

vjkoskela commented 8 years ago

The cloning functionality has been upgraded to use caching of the builder constructor and getter-setter pairs between the type and builder. This has significantly improved performance (~30x). It would likely still be faster to weave the clone functionality. However, the priority of this enhancement has been decreased significantly.

vjkoskela commented 8 years ago

See #18 for how we could use javassist to inject the byte code for this instead of relying on reflection.