Re: Create NSStrings from a mapped NSData object - safe?
Re: Create NSStrings from a mapped NSData object - safe?
- Subject: Re: Create NSStrings from a mapped NSData object - safe?
- From: Jens Alfke <email@hidden>
- Date: Tue, 13 May 2008 08:00:26 -0700
On 12 May '08, at 11:38 PM, Daniel Vollmer wrote:
I'm parsing a rather large text-file (usually >20MB) and in doing so
I'm iterating over its lines with [String getParagraphStart::::].
I've found a rather noticeable speed-up in the parsing operation if
I create the string in question from an NSData object (created via
initWithContentsOfMappedFile) using [String initWithData:encoding:].
It sounds like you're creating a single NSString containing the entire
contents of the file, then?
Now to the questions:
1) Is this safe if the file in question is being moved / deleted /
edited during parsing?
The string initializer you're using copies the data. This might just
involve calling -copy on the NSData instead of copying the bytes into
a new buffer; I'm not sure.
If the NSString made its own copy of the bytes, then you're totally
safe; the data from the mapped file isn't being used at all anymore.
If it's using the bytes in the NSData, you're "mostly" safe. Moving or
deleting the mapped file won't break the mapping (a deleted file isn't
actually deleted until all open file descriptors close.) A typical
"safe-save" won't alter the data either, since it creates a new file
and then deletes the old one. The only problem would be if something
overwrote the file in place, in which case the overwritten data would
suddenly show up in the NSData.
2) Are substrings created from the original string (e.g.
substringWithRange etc.) still backed properly after the original
string and the NSData object are released?
Yes. Even if the NSString is still using the NSData's contents for its
buffer, it retained them, so releasing the NSData won't make it go
away until the string is done with it.
—Jens
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden