RestKit/RKValueTransformers — pull request #16 by carllindberg (2014-11-07 17:07:11+00:00)
Title: Avoid temporary object creation during transformation

A few changes here.

First, it looks like +conformsToProtocol needs to grab a pthread_rw_lock, so it seems to be slightly more expensive than +isSubclassOfClass:. In the keyedArchiverTransformer checks, it was marginally faster to check the class first, since NSData properties are relatively rare, and it seems (given the transformer's placement in the default list) that it gets called a fair amount. Noticed the minor hotspot during https://github.com/RestKit/RestKit/issues/2065 testing, and this made a small difference.

Secondly, the class validation macros were creating local NSArray instances when given a single Class argument, which was not necessary. They were also evaluating their arguments more than once, which in several places was an NSArray creation, meaning that the NSArray would be allocated once every time the argument was used in the macro body. Changed to use a local variable to ensure it was only evaluated once.

Thirdly, changed most places where NSArrays or NSSets or similar objects were created every time through a transformer body, so that they are either static variables initialized via dispatch_once, or created once on the initial call to get the transformer with the instance being captured by the block and just being reused every time.

My test app actually doesn't use most of these transformers so the speedup was neglible, but it could avoid a substantial number of objects being created when they do get used repeatedly during a mapping operation.