From 4158ef02370b9cfe3a41ceefeca4ad79d4923493 Mon Sep 17 00:00:00 2001 From: iska Date: Sat, 11 Apr 2015 22:06:46 +0200 Subject: [PATCH] Use a accumulator-character-token in the tokenizer to reduce the initialization overhead Initializing character tokens, and the NSString objects they use, results in a relatively high overhead. The tokenizer now accumulated all successive characters in on accumulator token until a non-character token is emitted. This change reduces the execution time of the performance test on the local machine by 48% (reference to baseline) --- HTMLKit/HTMLTokenizer.m | 16 ++++++++++------ HTMLKitTests/HTMLKitTokenizerPerformance.m | 6 +----- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/HTMLKit/HTMLTokenizer.m b/HTMLKit/HTMLTokenizer.m index d64cba9..2dc8fd8 100644 --- a/HTMLKit/HTMLTokenizer.m +++ b/HTMLKit/HTMLTokenizer.m @@ -33,6 +33,7 @@ /* Pending Tokens & Attributes*/ HTMLTagToken *_currentTagToken; + HTMLCharacterToken *_currentCharacterToken; HTMLCommentToken *_currentCommentToken; HTMLDOCTYPEToken *_currentDoctypeToken; NSMutableString *_currentAttributeName; @@ -131,6 +132,10 @@ - (void)emitToken:(HTMLToken *)token { + if (_currentCharacterToken != nil) { + [_tokens addObject:_currentCharacterToken]; + _currentCharacterToken = nil; + } [_tokens addObject:token]; } @@ -174,16 +179,15 @@ - (void)emitCharacterTokenWithString:(NSString *)string { - if (string == nil) { + if (string.length == 0) { return; } - HTMLToken *previousToken = [_tokens lastObject]; - if ([previousToken isCharacterToken]) { - [(HTMLCharacterToken *)previousToken appendString:string]; - } else { - [self emitToken:[[HTMLCharacterToken alloc] initWithString:string]]; + if (_currentCharacterToken == nil) { + _currentCharacterToken = [HTMLCharacterToken new]; } + + [_currentCharacterToken appendString:string]; } - (void)emitParseError:(NSString *)format, ... NS_FORMAT_FUNCTION(1, 2) diff --git a/HTMLKitTests/HTMLKitTokenizerPerformance.m b/HTMLKitTests/HTMLKitTokenizerPerformance.m index 9aa8cfa..83c3c09 100644 --- a/HTMLKitTests/HTMLKitTokenizerPerformance.m +++ b/HTMLKitTests/HTMLKitTokenizerPerformance.m @@ -26,11 +26,7 @@ [self measureBlock:^{ HTMLTokenizer *tokenizer = [[HTMLTokenizer alloc] initWithString:string]; - - id token = nil; - do { - token = [tokenizer nextObject]; - } while (token != nil); + [tokenizer allObjects]; }]; }