Skip to content

Commit

Permalink
impr: Slightly speed up SentryInAppLogic (#4370)
Browse files Browse the repository at this point in the history
Slightly speed up the SentryInAppLogic by avoiding calls to
lastPathComponent and lowercaseString in loops. Both properties require
some extra work, such as iterating over the contents of the string. This
is noticeable for larger apps with many images and inAppIncludes and
inAppExcludes. For small apps, this doesn't make a difference.
  • Loading branch information
philipphofmann authored Sep 26, 2024
1 parent e2a3ac7 commit 3fda1c4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Improvements

- Speed up HTTP tracking for multiple requests in parallel (#4366)
- Slightly speed up SentryInAppLogic (#4370)

## 8.37.0-beta.1

Expand Down
36 changes: 30 additions & 6 deletions Sources/Sentry/SentryInAppLogic.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import "SentryInAppLogic.h"
#import "SentryLog.h"
#import <Foundation/Foundation.h>
#import <objc/runtime.h>

Expand All @@ -10,8 +11,19 @@ - (instancetype)initWithInAppIncludes:(NSArray<NSString *> *)inAppIncludes
inAppExcludes:(NSArray<NSString *> *)inAppExcludes
{
if (self = [super init]) {
_inAppIncludes = inAppIncludes;
_inAppExcludes = inAppExcludes;
NSMutableArray<NSString *> *includes =
[[NSMutableArray alloc] initWithCapacity:inAppIncludes.count];
for (NSString *include in inAppIncludes) {
[includes addObject:include.lowercaseString];
}
_inAppIncludes = includes;

NSMutableArray<NSString *> *excludes =
[[NSMutableArray alloc] initWithCapacity:inAppExcludes.count];
for (NSString *exclude in inAppExcludes) {
[excludes addObject:exclude.lowercaseString];
}
_inAppExcludes = excludes;
}

return self;
Expand All @@ -23,13 +35,17 @@ - (BOOL)isInApp:(nullable NSString *)imageName
return NO;
}

NSString *imageNameLastPathComponent = imageName.lastPathComponent.lowercaseString;

for (NSString *inAppInclude in self.inAppIncludes) {
if ([SentryInAppLogic isImageNameInApp:imageName inAppInclude:inAppInclude])
if ([SentryInAppLogic isImageNameLastPathComponentInApp:imageNameLastPathComponent
inAppInclude:inAppInclude])

return YES;
}

for (NSString *inAppExlude in self.inAppExcludes) {
if ([imageName.lastPathComponent.lowercaseString hasPrefix:inAppExlude.lowercaseString])
for (NSString *inAppExclude in self.inAppExcludes) {
if ([imageNameLastPathComponent hasPrefix:inAppExclude])
return NO;
}

Expand All @@ -48,7 +64,15 @@ - (BOOL)isClassInApp:(Class)targetClass

+ (BOOL)isImageNameInApp:(NSString *)imageName inAppInclude:(NSString *)inAppInclude
{
return [imageName.lastPathComponent.lowercaseString hasPrefix:inAppInclude.lowercaseString];
return [SentryInAppLogic
isImageNameLastPathComponentInApp:imageName.lastPathComponent.lowercaseString
inAppInclude:inAppInclude.lowercaseString];
}

+ (BOOL)isImageNameLastPathComponentInApp:(NSString *)imageNameLastPathComponent
inAppInclude:(NSString *)inAppInclude
{
return [imageNameLastPathComponent hasPrefix:inAppInclude];
}

@end
Expand Down

0 comments on commit 3fda1c4

Please sign in to comment.