Comparing the performance of using NSXMLParser within objective C vs Swift, there's a large performance discrepancy. Only registering didStartElement
, didEndElement
and foundCharacters
has a performance of ~17 MB/s in objective C, but a low ~1.4 MB/s in Swift (when casting to String, see below). The code is run in Release (optimized) mode.
Objective C:
#import <Foundation/Foundation.h>
@interface MyDelegate: NSObject <NSXMLParserDelegate> {
@public
int didStartElement;
int didEndElement;
int foundCharacters;
}
@end
@implementation MyDelegate
-(MyDelegate *)init {
didStartElement = 0;
didEndElement = 0;
foundCharacters = 0;
return self;
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
didStartElement += 1;
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
didEndElement += 1;
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
foundCharacters += 1;
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSURL *input = [NSURL fileURLWithPath: [[NSProcessInfo processInfo] arguments][1]];
NSError *error;
if (![input checkResourceIsReachableAndReturnError:&error]) {
NSLog(error.description);
abort();
}
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:input];
MyDelegate *delegate = [[MyDelegate alloc] init];
parser.delegate = delegate;
NSDate *start = [NSDate new];
if (![parser parse]) {
NSLog(parser.parserError.description);
}
NSDate *end = [NSDate new];
NSLog(@"Done. #didStartElement: %d, #didEndElement: %d, #foundCharacters: %d", delegate->didStartElement, delegate->didEndElement, delegate->foundCharacters);
NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:input.path error:&error];
// Determine MB/s
if (error != nil) {
NSLog(@"%@", error);
abort();
}
double throughput = ((NSNumber *)[attrs valueForKey:NSFileSize]).doubleValue / [end timeIntervalSinceDate:start] / 1e6;
NSLog(@"Throughput %f MB/s", throughput);
}
return 0;
}
Swift:
import Foundation
var input = NSURL(fileURLWithPath: Process.arguments[1])!
var error: NSError?
if !input.checkResourceIsReachableAndReturnError(&error) {
println(error)
abort()
}
class MyDelegate: NSObject, NSXMLParserDelegate {
var didStartElement = 0
var didEndElement = 0
var foundCharacters = 0
func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [NSObject : AnyObject]) {
didStartElement += 1
}
func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
didEndElement += 1
}
func parser(parser: NSXMLParser, foundCharacters string: String) {
foundCharacters += 1
}
}
var parser = NSXMLParser(contentsOfURL: input)!
println(input)
var delegate = MyDelegate()
parser.delegate = delegate
var start = NSDate()
parser.parse()
var end = NSDate()
println("Done. #didStartElement: \(delegate.didStartElement), #didEndElement \(delegate.didEndElement), #foundCharacters \(delegate.foundCharacters)")
// Determine MB/s
var attrs = NSFileManager.defaultManager().attributesOfItemAtPath(input.path!, error: &error)
if error != nil {
println(error!)
abort()
}
var throughput = Double(attrs![NSFileSize]! as Int) / end.timeIntervalSinceDate(start) / 1e6
println("Throughput \(throughput) MB/s")
A lot of performance is lost when casting; see the difference for the type definitions for the attributes
argument in didStartElement
:
NSDictionary
: 19 MB/s
[NSObject: AnyObject]
: 8.5 MB/s
[String: String]
: 1.4 MB/s
Using the Counter Instrument, apparently 36% of the time is being spent on converting the dictionary to Swift (using [NSObject: AnyObject]
):
As the attributes of the nodes are relevant for further processing, casting them to Swift String can't be avoided. How to still get a decent processing performance in Swift?
Update
When using libxml2's sax parser directly in C, the performance is around 110 MB/s. So there really is some performance issue here.