Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Even faster string building and fewer allocations

  • Loading branch information...
commit 341c2f2fe93df4ff4b2005ce49602042e2415c05 1 parent 9b62e7f
@jspahrsummers jspahrsummers authored
View
2  Mantle/MTLJSONAdapter.m
@@ -199,7 +199,7 @@ - (NSDictionary *)JSONDictionary {
- (NSValueTransformer *)JSONTransformerForKey:(NSString *)key {
NSParameterAssert(key != nil);
- SEL selector = MTLSelectorWithKeyPattern(key, @"JSONTransformer");
+ SEL selector = MTLSelectorWithKeyPattern(key, "JSONTransformer");
if ([self.modelClass respondsToSelector:selector]) {
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self.modelClass methodSignatureForSelector:selector]];
invocation.target = self.modelClass;
View
2  Mantle/MTLModel+NSCoding.m
@@ -127,7 +127,7 @@ - (id)decodeValueForKey:(NSString *)key withCoder:(NSCoder *)coder modelVersion:
NSParameterAssert(key != nil);
NSParameterAssert(coder != nil);
- SEL selector = MTLSelectorWithCapitalizedKeyPattern(@"decode", key, @"WithCoder:modelVersion:");
+ SEL selector = MTLSelectorWithCapitalizedKeyPattern("decode", key, "WithCoder:modelVersion:");
if ([self respondsToSelector:selector]) {
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:selector]];
invocation.target = self;
View
2  Mantle/MTLModel.m
@@ -152,7 +152,7 @@ - (NSDictionary *)dictionaryValue {
- (void)mergeValueForKey:(NSString *)key fromModel:(MTLModel *)model {
NSParameterAssert(key != nil);
- SEL selector = MTLSelectorWithCapitalizedKeyPattern(@"merge", key, @"FromModel:");
+ SEL selector = MTLSelectorWithCapitalizedKeyPattern("merge", key, "FromModel:");
if (![self respondsToSelector:selector]) {
if (model != nil) {
[self setValue:[model valueForKey:key] forKey:key];
View
4 Mantle/MTLReflection.h
@@ -16,7 +16,7 @@
//
// Returns a selector, or NULL if the input strings cannot form a valid
// selector.
-SEL MTLSelectorWithKeyPattern(NSString *key, NSString *suffix) __attribute__((pure, nonnull(1, 2)));
+SEL MTLSelectorWithKeyPattern(NSString *key, const char *suffix) __attribute__((pure, nonnull(1, 2)));
// Creates a selector from a key and a constant prefix and suffix.
//
@@ -28,4 +28,4 @@ SEL MTLSelectorWithKeyPattern(NSString *key, NSString *suffix) __attribute__((pu
//
// Returns a selector, or NULL if the input strings cannot form a valid
// selector.
-SEL MTLSelectorWithCapitalizedKeyPattern(NSString *prefix, NSString *key, NSString *suffix) __attribute__((pure, nonnull(1, 2, 3)));
+SEL MTLSelectorWithCapitalizedKeyPattern(const char *prefix, NSString *key, const char *suffix) __attribute__((pure, nonnull(1, 2, 3)));
View
40 Mantle/MTLReflection.m
@@ -9,34 +9,42 @@
#import "MTLReflection.h"
#import <objc/runtime.h>
-SEL MTLSelectorWithKeyPattern(NSString *key, NSString *suffix) {
- NSUInteger keyLength = [key lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
- NSUInteger suffixLength = [suffix lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
+SEL MTLSelectorWithKeyPattern(NSString *key, const char *suffix) {
+ NSUInteger keyLength = [key maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding];
+ NSUInteger suffixLength = strlen(suffix);
char selector[keyLength + suffixLength + 1];
- memcpy(selector, key.UTF8String, keyLength);
- memcpy(selector + keyLength, suffix.UTF8String, suffixLength);
- selector[sizeof(selector) - 1] = '\0';
+
+ BOOL success = [key getBytes:selector maxLength:keyLength usedLength:&keyLength encoding:NSUTF8StringEncoding options:0 range:NSMakeRange(0, key.length) remainingRange:NULL];
+ if (!success) return NULL;
+
+ memcpy(selector + keyLength, suffix, suffixLength);
+ selector[keyLength + suffixLength] = '\0';
return sel_registerName(selector);
}
-SEL MTLSelectorWithCapitalizedKeyPattern(NSString *prefix, NSString *key, NSString *suffix) {
- NSUInteger prefixLength = [prefix lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
- NSUInteger suffixLength = [suffix lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
+SEL MTLSelectorWithCapitalizedKeyPattern(const char *prefix, NSString *key, const char *suffix) {
+ NSUInteger prefixLength = strlen(prefix);
+ NSUInteger suffixLength = strlen(suffix);
NSString *initial = [key substringToIndex:1].uppercaseString;
- NSUInteger initialLength = [initial lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
+ NSUInteger initialLength = [initial maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding];
NSString *rest = [key substringFromIndex:1];
- NSUInteger restLength = [rest lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
+ NSUInteger restLength = [rest maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding];
char selector[prefixLength + initialLength + restLength + suffixLength + 1];
- memcpy(selector, prefix.UTF8String, prefixLength);
- memcpy(selector + prefixLength, initial.UTF8String, initialLength);
- memcpy(selector + prefixLength + initialLength, rest.UTF8String, restLength);
- memcpy(selector + prefixLength + initialLength + restLength, suffix.UTF8String, suffixLength);
- selector[sizeof(selector) - 1] = '\0';
+ memcpy(selector, prefix, prefixLength);
+
+ BOOL success = [initial getBytes:selector + prefixLength maxLength:initialLength usedLength:&initialLength encoding:NSUTF8StringEncoding options:0 range:NSMakeRange(0, initial.length) remainingRange:NULL];
+ if (!success) return NULL;
+
+ success = [rest getBytes:selector + prefixLength + initialLength maxLength:restLength usedLength:&restLength encoding:NSUTF8StringEncoding options:0 range:NSMakeRange(0, rest.length) remainingRange:NULL];
+ if (!success) return NULL;
+
+ memcpy(selector + prefixLength + initialLength + restLength, suffix, suffixLength);
+ selector[prefixLength + initialLength + restLength + suffixLength] = '\0';
return sel_registerName(selector);
}
Please sign in to comment.
Something went wrong with that request. Please try again.