gnustep-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSTextStorage -string inefficient


From: Richard Frith-Macdonald
Subject: Re: NSTextStorage -string inefficient
Date: Wed, 6 Feb 2002 12:09:36 +0000

On Wednesday, February 6, 2002, at 11:22 AM, Nicola Pero wrote:
So - I suggest that - either we fix GSTextStorage so that -string just
returns _textChars, adding prominent comments that it's not copied so
after calling it you should read the chars you need from it and discard it immediately (or else make your own copy). We might need to fix callers to
make a copy in places where a copy is needed.  (for example, I think we
want to change NSTextView -string to return a copy).

Otherwise, if we don't implement it in O(1) time but keep our very lossy
O(N), I suggest we add -characterAtIndex: and other NSStringish methods to
access characters efficiently from an NSTextStorage.  Even that might
normally be not as efficient, because if you access, say, 1000 chars,
calling string + 1000 * characterAtIndex: probably takes less than calling
1000 * (2 * characterAtIndex:) (because NSTextStorage -characterAtIndex:
will return [_textChars charactersAtIndex:] so each call to NSTextStorage
actually implies another call to the actual string).  While it might not
be particularly relevant for speed now, it might be very relevant for the
speed of the layout manager in the future, as the layout manager has to
read huge amount of chars from the text storage.  Currently, we copy the
whole textstorage string each time we need to do a layout, or, for the
glyph code, each time we want to read a char from it ... this last one is
obviously not very efficient.

Well ... if you use the -mutableString method instead of the -string method, we could optimise the private class returned by this method to be close to giving the same speed as having direct access to the underlying NSMutableString
object.  This is more work to code, but would keep the API clean/safe.
I'll do that optimisation if you want.

That being said ... as the MacOS-X documentation says 0(1) time ... they are
clearly not copying the string, so perhaps we should return our actual
internal string.  However, I don't really like that!

Another possible compromise would be to write an NSString subclass to wrap
mutable strings (such that they can't be mutated by it) and use that.
The base library already uses such a class to handle substrings of constant
strings - if we made this public in the base library, NSTextStorage could
use it to return its contents in such a way that other classes couldn't
modify them. It would also be efficient - since it could have direct access
to the internals of the string. I think this is my preferred option.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]