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 22:32:15 -0700
On 13 May '08, at 9:39 PM, Michael Vannorsdel wrote:
If there's some pages of your data you haven't touched in a while
the system will swap those out and use the physical pages for
something that needs them.
Yes, but it's less efficient than a mapped file, which doesn't have to
be swapped out at all.
The OS may have virtual memory, but swapping when the system is under
memory pressure is the chief performance problem in OS X; when I
worked at Apple, the performance people drilled it into us that the
most important optimization is saving memory. (For example, that's why
Release builds use -Os by default instead of -O2.)
When making substrings from the data you can save making redundant
copies using initWithBytesNoCopy:length:encoding:freeWhenDone: for
string objects that only reference the characters from the data.
They depend on the NSData object so it needs to be valid for these
strings to work.
This is tricky and dangerous. It's very difficult to predict object
lifespans in a ref-counted or GC'd environment, and if any of those
little strings are still being retained by something when you free the
big string, they all turn into land mines that will crash the app the
next time they're referenced.
It's possible to make this work, but I would only try it as a last-
ditch optimization if the sheer volume of copied strings was choking
performance.
(Or -initWithBytesNoCopy: might just copy the bytes anyway. It's only
a hint, not a guarantee. I believe it can only use the raw bytes
without copying if they're in UTF-16 or ascii or MacRoman encoding.)
—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