Memory and allocation difference

11 views
Skip to first unread message

Tobias Klonk

unread,
May 18, 2009, 4:45:05 AM5/18/09
to kis...@googlegroups.com
Hi All,

I love KissXML. It's just the libxml Wrapper I was thinking about to write for my own Projects, though sharing the public interface of NSXML is a great idea I haven't thought about.

Stepping trough the Code I stubled upon the memory management section and asked myself why you would go trough the hassle of always allocating new DDXMLNode on every request, which in turn brings the need of a linked retain list via struct _xmlRetain ?

NSXML is doing otherwise. When you request a given NSXMLNode for the second time you get a pointer to the already instanciated NSXMLNode, which saves allocation time and memory. Please try out this code:

NSMutableString *xmlStr = [NSMutableString stringWithCapacity:100];
[xmlStr appendString:@"<?xml version=\"1.0\"?>"];
[xmlStr appendString:@"<request>"];
[xmlStr appendString:@"  <category>"];
[xmlStr appendString:@"  </category>"];
[xmlStr appendString:@"</request>"];



NSXMLDocument *nsDoc = [[NSXMLDocument alloc] initWithXMLString:xmlStr options:0 error:nil];


NSXMLNode *nsChild1 = [[nsDoc rootElement] childAtIndex:0];
NSXMLNode *nsChild2 = [[nsDoc rootElement] childAtIndex:0];


if (nsChild1 == nsChild2) {
NSLog(@"Same request gives same Pointer, the Node is not allocated twice");
} else {
NSLog(@"Same request gives different Pointer, the Node is allocated twice wasting time and memory");
}

[nsDoc release];


DDXMLDocument *ddDoc = [[DDXMLDocument alloc] initWithXMLString:xmlStr options:0 error:nil];


DDXMLNode *ddChild1 = [[ddDoc rootElement] childAtIndex:0];
DDXMLNode *ddChild2 = [[ddDoc rootElement] childAtIndex:0];


if (ddChild1 == ddChild2) {
NSLog(@"Same request gives same Pointer, the Node is not allocated twice");
} else {
NSLog(@"Same request gives different Pointer, the Node is allocated twice wasting time and memory");
}

[ddDoc release];

I would suggest to store only one pointer to an DDXMLNode in _private. When a Node is requested, look up _private, if NULL instanciate a new one and store the pointer in _private, if non-NULL call autorelease on the stored instance and hand it back. On deallocation you would set _private back to NULL and go on with checking the parent pointer releasing underlying libxml structures if appropriate.

This way you could easily trash the hole _xmlRetain linked List.

Please tell me if I overlooked some deeper implications that could prevent using that kind of easier, lazier memory management.


Tobias Klonk






Robbie Hanson

unread,
May 18, 2009, 1:37:25 PM5/18/09
to kis...@googlegroups.com
You know... I was sure there was a reason I did it this way.  But I can't, for the life of me, remember why.  I've been sitting here trying to figure it out, and I'm starting to come to the conclusion that you're right.  I'm going to start working on this.  I'll keep you posted.

-Robbie Hanson
-Deusty Designs



Robbie Hanson

unread,
May 18, 2009, 7:01:16 PM5/18/09
to kis...@googlegroups.com
This way you could easily trash the hole _xmlRetain linked List.

Consider it trashed.  I just committed these changes.  Let me know what you think.

-Robbie Hanson
-Deusty Designs




On May 18, 2009, at 3:45 AM, Tobias Klonk wrote:

Reply all
Reply to author
Forward
0 new messages