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.
Guessing he thinks it'll avoid the autorelease pool

-Samuel E. Giddins

On Nov 7, 2014, at 12:35 PM, Blake Watters <notifications@github.com> wrote:

In Code/RKValueTransformers.m:

> @@ -696,7 +719,7 @@ - (NSArray *)valueTransformersForTransformingFromClass:(Class)sourceClass toClas
>  
>      if (transformers != nil) return transformers;
>  
> -    NSMutableArray *matchingTransformers = [NSMutableArray arrayWithCapacity:[self.valueTransformers count]];
> +    NSMutableArray *matchingTransformers = [[NSMutableArray alloc] initWithCapacity:[self.valueTransformers count]];
What’s the point of this change?

—
Reply to this email directly or view it on GitHub.