Why is this performance degradation?

I need to reduce the memory used by my native Windows C ++ application, without sacrificing its performance.

My main data structure consists of several thousand instances, dynamically distributed, from the following Line class:

 struct Properties { // sizeof(Properties) == 28 }; // Version 1 class Line { virtual void parse(xml_node* node, const Data& data) { parse_internal(node, data); create(); } virtual void parse_internal(xml_node*, const Data&); void create(); Properties p; }; 

But since I notice that I could get rid of a member of class p because I only need it in the parsing method, I changed the implementation of Line :

 // Version 2 class Line { virtual void parse(xml_node* node, const Data& data) { Properties p; parse_internal(node, data, &p); create(&p); } virtual void parse_internal(xml_node*, const Data&, Properties*); void create(Properties*); }; 

This reduced the memory allocated from a few megabytes, but increased the time elapsed after more than 50 milliseconds.

I wonder how this is possible, given that the application was compiled for the release version with full speed optimization. Is this related to passing an argument? Is this related to the stack allocation of my struct Properties ?

Update:

The Line::parse method is called only once for each instance. The data structure consists of std::vector of Line s. Several threads control another subset of this vector.

+7
c ++ performance memory-management parameter-passing memory
source share
1 answer

You write that parse_internal is recursive. This means that it receives 3 arguments in the modified version instead of 2 in the original - and is called recursively several times.

You also need to access the elements using the pointer syntax instead of deleting the elements (and perhaps make sure the Properties pointer is not null). To fix pointer issues, you can use the parse_internal reference argument.

Is there a reason to have parse_internal as a virtual member function, or can you change it as static (in a modified version)?

0
source share

All Articles