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.
c ++ performance memory-management parameter-passing memory
Nick
source share