From 2cac171c72cd4941213c7382ee46a72aaf33f699 Mon Sep 17 00:00:00 2001 From: Boris Suvorov Date: Mon, 6 Jul 2015 18:41:23 -0700 Subject: [PATCH] Cache nonParameterCharacterSet in SOCKit.m to avoid memory spikes in multithreaded environments Relevant github.com pull request into SOCKit is located here: https://github.com/NimbusKit/sockit/pull/26 --- src/SOCKit.m | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/SOCKit.m b/src/SOCKit.m index 2cd36d8..0c7bafb 100644 --- a/src/SOCKit.m +++ b/src/SOCKit.m @@ -71,10 +71,15 @@ - (id)copyWithZone:(NSZone *)zone { #pragma mark - Pattern Compilation - (NSCharacterSet *)nonParameterCharacterSet { - NSMutableCharacterSet* parameterCharacterSet = [NSMutableCharacterSet alphanumericCharacterSet]; - [parameterCharacterSet addCharactersInString:@".@_"]; - NSCharacterSet* nonParameterCharacterSet = [parameterCharacterSet invertedSet]; - return nonParameterCharacterSet; + static NSCharacterSet* staticNonParameterCharacterSet = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + NSMutableCharacterSet* parameterCharacterSet = [NSMutableCharacterSet alphanumericCharacterSet]; + [parameterCharacterSet addCharactersInString:@".@_"]; + staticNonParameterCharacterSet = [parameterCharacterSet invertedSet]; + }); + + return staticNonParameterCharacterSet; } - (void)_compilePattern {