Background: An NSDocument text editing based app. Each page of the document has its own textView for the main document and a textView for the header and the footer for a total of three textViews (and associated objects such as textContainers) for each page.
Currently I have it optimized to the point of being able to load a 336 page, 102K word test document in 2.83 seconds and being ready to edit text. This means that with the test document, over 1000 textViews are being allocated, initialized, and added to a view in that time span. Not bad but I can never leave well enough alone, and, as a personal challenge, I would like to get this down to less than two seconds. The ultimate goal would be to have everything loaded and ready to edit before the dock icon finishes its first bounce.
I mostly know enough about objective C to be dangerous. I am thinking one possible avenue of tinkering could be the way I allocated and use other objects such as attributed strings, arrays, dictionaries, etc.
Here is where I would like some insight.
Possible scenarios:
// scenario 1
// in a parent class
NSMutableAttributedString *aMainStringOfText;
-(void)setThingsUpForString:(NSMutableAttributedString *)theString {
// assume aMainStringOfText has been allocated/initialized and
// is being passed in
[helperClass fiddleWithAttributes:theString];
}
// in a helperClass
-(void)fiddleWithAttributes:(NSMutableAttributedString *)stringToFiddleWith {
// do some stuff with the attString
}
// scenario 2
// in a parent class
NSMutableAttributedString *aMainStringOfText;
-(void)setThingsUpForString:(NSMutableAttributedString *)theString {
// assume aMainStringOfText has been allocated/initialized and
// is being passed in
theString = [helperClass fiddleWithAttributes:theString];
}
// in a helperClass
-(NSMutableAttributedString *)fiddleWithAttributes:(NSMutableAttributedString *)stringToFiddleWith {
// do some stuff with the stringToFiddleWith
return stringToFiddleWith;
}
// scenario 3
// in a parent class
NSMutableAttributedString *aMainStringOfText;
-(void)setThingsUpForString:(NSAttributedString *)theString {
aMainStringOfText = [[NSMutableAttributedString alloc] initWithAttributedString:[helperClass fiddleWithAttributes:theString]];
}
// in a helperClass
-(NSAttributedString *)fiddleWithAttributes:(NSAttributedString *)stringToFiddleWith {
NSMutableAttributedString *tempString = [[NSMutableAttributedString alloc] initWithAttributedString:stringToFiddleWith];
// do some stuff with the tempString
return tempString;
}
Scenario 1 seems like the best use of resources and the most efficient. If I understand Objective C well enough, I get from this that one string object is created (aMainStringOfText) and all further fiddling happens on that string.
Scenario 2 is where my understanding gets a bit murky. What is being returned from the helper class? Is it a pointer to the original string that was passed in? And since the string that was passed in was not the actual string, but a pointer to it (is this assumption correct?), then is the helper returning a pointer to the pointer?
Scenario 3 seems like a big waste of resources. An attributed string is passed in, a mutable string is created so it can be changed, and then it is returned as an attributedString which is then allocated again as a mutable string. Wasteful? What happens to all these strings? Which one really winds up as the mainStringOfText?
So what about this:
// Scenario A
// in the main class I need to unarchive some data
aMainStringOfText = [[NSMutableAttributedString alloc] initWithAttributedString:[helperClass stringFromData:someData]];
// in helper class
-(NSAttibutedString *)stringFromData:(NSData *)someData {
NSAttributedString *aStringFromData = [unarchiveSomeData:someData];
return aStringFromData;
}
// Scenario B
aMainStringOfText = [helperClass stringFromData:someData]];
// in helper class
-(NSMutableAttributedString *)stringFromData:(NSData *)someData {
NSMutableAttributedString *aStringFromData = [[NSMutableAttributedString alloc] initWithAttributedString:[unarchiveSomeData:someData]];
return aStringFromData;
}
In Scenario B, who owns the mutableAttributedString? Since it is created in the helper class, isn't the helper the owner? And if the helper gets deallocated, does the string go away? From a coding clarity standpoint, Scenario B is cleaner since the helper does all the allocating and the classes that call this helper class (which can be a lot of them) don't have to do all the allocating in their code. But is this really a good choice.
These same examples could also apply to arrays and dictionaries, right? Or really any object that uses a *pointer?
1. Which of these is a better choice from a resource management standpoint?
2. Which is better from an speed standpoint?
3. Which is better from a "best practices" standpoint?
4. Other?