From b489c9aa438fe6c615af0220249dc821ca094c50 Mon Sep 17 00:00:00 2001 From: Nick Lockwood Date: Mon, 10 Aug 2015 07:03:52 -0700 Subject: [PATCH] Fixed perf issue due to use of regex in +[RCTConvert NSURL:] --- React/Base/RCTConvert.h | 11 +++++++++-- React/Base/RCTConvert.m | 24 +++++++++--------------- React/Base/RCTRedBox.m | 7 ++++++- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/React/Base/RCTConvert.h b/React/Base/RCTConvert.h index ec8b8bb34..415a5968f 100644 --- a/React/Base/RCTConvert.h +++ b/React/Base/RCTConvert.h @@ -142,7 +142,14 @@ typedef BOOL css_clip_t, css_backface_visibility_t; RCT_EXTERN NSNumber *RCTConvertEnumValue(const char *, NSDictionary *, NSNumber *, id); RCT_EXTERN NSNumber *RCTConvertMultiEnumValue(const char *, NSDictionary *, NSNumber *, id); RCT_EXTERN NSArray *RCTConvertArrayValue(SEL, id); -RCT_EXTERN void RCTLogConvertError(id, const char *); + +/** + * This macro is used for logging conversion errors. This is just used to + * avoid repeating the same boilerplate for every error message. + */ +#define RCTLogConvertError(json, typeName) \ +RCTLogError(@"JSON value '%@' of type %@ cannot be converted to %@", \ +json, [json classForCoder], typeName) /** * This macro is used for creating simple converter functions that just call @@ -165,7 +172,7 @@ RCT_CUSTOM_CONVERTER(type, name, [json getter]) return code; \ } \ @catch (__unused NSException *e) { \ - RCTLogConvertError(json, #type); \ + RCTLogConvertError(json, @#type); \ json = nil; \ return code; \ } \ diff --git a/React/Base/RCTConvert.m b/React/Base/RCTConvert.m index 0e7e82326..ca091592e 100644 --- a/React/Base/RCTConvert.m +++ b/React/Base/RCTConvert.m @@ -16,12 +16,6 @@ @implementation RCTConvert -void RCTLogConvertError(id json, const char *type) -{ - RCTLogError(@"JSON value '%@' of type '%@' cannot be converted to %s", - json, [json classForCoder], type); -} - RCT_CONVERTER(id, id, self) RCT_CONVERTER(BOOL, BOOL, boolValue) @@ -53,11 +47,11 @@ RCT_CONVERTER(NSString *, NSString, description) }); NSNumber *number = [formatter numberFromString:json]; if (!number) { - RCTLogConvertError(json, "a number"); + RCTLogConvertError(json, @"a number"); } return number; } else if (json && json != (id)kCFNull) { - RCTLogConvertError(json, "a number"); + RCTLogConvertError(json, @"a number"); } return nil; } @@ -97,7 +91,7 @@ RCT_CONVERTER(NSString *, NSString, description) } // Check if it has a scheme - if ([path rangeOfString:@"[a-zA-Z][a-zA-Z._-]+:" options:NSRegularExpressionSearch].location == 0) { + if ([path rangeOfString:@":"].location != NSNotFound) { path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; URL = [NSURL URLWithString:path]; if (URL) { @@ -117,7 +111,7 @@ RCT_CONVERTER(NSString *, NSString, description) return [NSURL fileURLWithPath:path]; } @catch (__unused NSException *e) { - RCTLogConvertError(json, "a valid URL"); + RCTLogConvertError(json, @"a valid URL"); return nil; } } @@ -162,7 +156,7 @@ RCT_CONVERTER(NSString *, NSString, description) } return date; } else if (json && json != (id)kCFNull) { - RCTLogConvertError(json, "a date"); + RCTLogConvertError(json, @"a date"); } return nil; } @@ -339,7 +333,7 @@ static void RCTConvertCGStructValue(const char *type, NSArray *fields, NSDiction result[i] = [RCTConvert CGFloat:json[fields[i]]]; } } else if (RCT_DEBUG && json && json != (id)kCFNull) { - RCTLogConvertError(json, type); + RCTLogConvertError(json, @(type)); } } @@ -627,7 +621,7 @@ RCT_CGSTRUCT_CONVERTER(CGAffineTransform, (@[ } else if (RCT_DEBUG && json && json != (id)kCFNull) { - RCTLogConvertError(json, "a color"); + RCTLogConvertError(json, @"a color"); } // Default color @@ -665,7 +659,7 @@ RCT_CGSTRUCT_CONVERTER(CGAffineTransform, (@[ path = [self NSString:json[@"uri"]]; scale = [self CGFloat:json[@"scale"]]; } else { - RCTLogConvertError(json, "an image"); + RCTLogConvertError(json, @"an image"); } NSURL *URL = [self NSURL:path]; @@ -696,7 +690,7 @@ RCT_CGSTRUCT_CONVERTER(CGAffineTransform, (@[ } else if ([scheme isEqualToString:@"data"]) { image = [UIImage imageWithData:[NSData dataWithContentsOfURL:URL]]; } else { - RCTLogConvertError(json, "an image. Only local files or data URIs are supported"); + RCTLogConvertError(json, @"an image. Only local files or data URIs are supported"); } if (scale > 0) { diff --git a/React/Base/RCTRedBox.m b/React/Base/RCTRedBox.m index da8dd8284..862b360a7 100644 --- a/React/Base/RCTRedBox.m +++ b/React/Base/RCTRedBox.m @@ -205,7 +205,12 @@ RCT_NOT_IMPLEMENTED(-initWithCoder:(NSCoder *)aDecoder) } cell.textLabel.text = stackFrame[@"methodName"]; - cell.detailTextLabel.text = cell.detailTextLabel.text = [NSString stringWithFormat:@"%@:%@", [stackFrame[@"file"] lastPathComponent], stackFrame[@"lineNumber"]]; + + NSString *fileAndLine = stackFrame[@"file"]; + if (fileAndLine) { + fileAndLine = [fileAndLine stringByAppendingFormat:@":%@", stackFrame[@"lineNumber"]]; + cell.detailTextLabel.text = cell.detailTextLabel.text = fileAndLine; + } return cell; }