Re: [NSString UTF8String] performance
Re: [NSString UTF8String] performance
- Subject: Re: [NSString UTF8String] performance
- From: Ali Ozer <email@hidden>
- Date: Sun, 15 Feb 2004 08:49:59 -0800
I'm using the AGRegex kit to match regular expressions and with larger
files it's starting to get very slow. The bottleneck seems to be in
[NSString UTF8String] which it calls often. Each call to UTF8String
seems to be calling both
__CFStringEncodedByteStream and
creates a new NSData
Does this conversion and copy always happen when calling UTF8String?
Or do I have my string in the wrong format? I was hoping that their
would be a way to make UTF8String just return a pointer to some
underlying data structure.
For built-in NSString classes (those you get with [NSString alloc]),
UTF8String does try to return an internal pointer if it can. However,
this depends on a bunch of factors, isn't guaranteed even when it seems
like it might be able to, and the fact remains that UTF8String might
allocate a new buffer each time to package the result. So it is not a
great choice in time-critical loops and such if it might potentially be
called many thousands or millions of times, which might be the case in
sorting or reg-ex type situations.
It would be best to try to rewrite the segments not to call UTF8String
often; if the conversion can be done less times, on larger parts of the
string? Another option for now is to call CFStringGetBytes() which
allows passing in a pre-allocated buffer. (You can just pass the
NSString into this call, thanks to toll-free bridging.)
Ali
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.