Permalink
Browse files

removed @synchronized, performance comes first

Signed-off-by: Rifat Nabi <to.rifat@gmail.com>
  • Loading branch information...
1 parent 3539f85 commit fa22adf69d336a0983de1d1a5f7a2da534cfbd86 @torifat torifat committed Jul 7, 2012
Showing with 827 additions and 945 deletions.
  1. +44 −55 AutoCorrect.m
  2. +200 −226 AvroParser.m
  3. +46 −75 CacheManager.m
  4. +8 −6 Candidates.m
  5. +220 −237 Database.m
  6. +198 −221 RegexParser.m
  7. +111 −125 Suggestion.m
View
@@ -15,22 +15,17 @@ @implementation AutoCorrect
@synthesize autoCorrectEntries = _autoCorrectEntries;
+ (AutoCorrect *)sharedInstance {
- @synchronized (self) {
- if (sharedInstance == nil) {
- [[self alloc] init]; // assignment not done here, see allocWithZone
- }
- }
+ if (sharedInstance == nil) {
+ [[self alloc] init]; // assignment not done here, see allocWithZone
+ }
return sharedInstance;
}
+ (id)allocWithZone:(NSZone *)zone {
- @synchronized(self) {
- if (sharedInstance == nil) {
- sharedInstance = [super allocWithZone:zone];
- return sharedInstance; // assignment and return on first allocation
- }
+ if (sharedInstance == nil) {
+ sharedInstance = [super allocWithZone:zone];
+ return sharedInstance; // assignment and return on first allocation
}
-
return nil; //on subsequent allocation attempts return nil
}
@@ -55,62 +50,56 @@ - (NSUInteger)retainCount {
}
- (id)init {
- @synchronized(self) {
- self = [super init];
- if (self) {
- // Open the file
- NSString *fileName = [[NSBundle mainBundle] pathForResource:@"autodict" ofType:@"dct"];
- const char *fn = [fileName UTF8String];
- FILE *file = fopen(fn, "r");
+ self = [super init];
+ if (self) {
+ // Open the file
+ NSString *fileName = [[NSBundle mainBundle] pathForResource:@"autodict" ofType:@"dct"];
+ const char *fn = [fileName UTF8String];
+ FILE *file = fopen(fn, "r");
+
+ // Read from the file
+ char replaceBuffer[512], withBuffer[512];
+ _autoCorrectEntries = [[NSMutableArray alloc] init];
+ while(fscanf(file, "%s %[^\n]\n", replaceBuffer, withBuffer) == 2) {
+ NSString* replace = [NSString stringWithFormat:@"%s", replaceBuffer];
+ NSString* with = [NSString stringWithFormat:@"%s", withBuffer];
- // Read from the file
- char replaceBuffer[512], withBuffer[512];
- _autoCorrectEntries = [[NSMutableArray alloc] init];
- while(fscanf(file, "%s %[^\n]\n", replaceBuffer, withBuffer) == 2) {
- NSString* replace = [NSString stringWithFormat:@"%s", replaceBuffer];
- NSString* with = [NSString stringWithFormat:@"%s", withBuffer];
-
- if ([replace isEqualToString:with] == NO) {
- with = [[AvroParser sharedInstance] parse:with];
- }
-
- NSMutableDictionary* item = [[NSMutableDictionary alloc] initWithObjectsAndKeys:replace, @"replace", with, @"with", nil];
- [_autoCorrectEntries addObject:item];
- [item release];
+ if ([replace isEqualToString:with] == NO) {
+ with = [[AvroParser sharedInstance] parse:with];
}
- fclose(file);
+
+ NSMutableDictionary* item = [[NSMutableDictionary alloc] initWithObjectsAndKeys:replace, @"replace", with, @"with", nil];
+ [_autoCorrectEntries addObject:item];
+ [item release];
}
- return self;
- }
+ fclose(file);
+ }
+ return self;
}
- (void)dealloc {
- @synchronized(self) {
- [_autoCorrectEntries release];
- [super dealloc];
- }
+ [_autoCorrectEntries release];
+ [super dealloc];
}
// Instance Methods
- (NSString*)find:(NSString*)term {
- @synchronized(self) {
- term = [[AvroParser sharedInstance] fix:term];
- // Binary Search
- int left = 0, right = [_autoCorrectEntries count] -1, mid;
- while (right >= left) {
- mid = (left + right) / 2;
- NSDictionary* item = [_autoCorrectEntries objectAtIndex:mid];
- NSComparisonResult comp = [term compare:[item objectForKey:@"replace"]];
- if (comp == NSOrderedDescending) {
- left = mid + 1;
- } else if (comp == NSOrderedAscending) {
- right = mid - 1;
- } else {
- return [item objectForKey:@"with"];
- }
+ term = [[AvroParser sharedInstance] fix:term];
+ // Binary Search
+ int left = 0, right = [_autoCorrectEntries count] -1, mid;
+ while (right >= left) {
+ mid = (left + right) / 2;
+ NSDictionary* item = [_autoCorrectEntries objectAtIndex:mid];
+ NSComparisonResult comp = [term compare:[item objectForKey:@"replace"]];
+ if (comp == NSOrderedDescending) {
+ left = mid + 1;
+ } else if (comp == NSOrderedAscending) {
+ right = mid - 1;
+ } else {
+ return [item objectForKey:@"with"];
}
- return nil;
}
+ return nil;
}
@end
Oops, something went wrong.

0 comments on commit fa22adf

Please sign in to comment.