I need to reduce the memory used by my native Windows C++ application, without compromising its performances.
My main data structure is composed by several thousands of instances, dynamically allocated, of 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 the class member p
, because I only need it within the parse method, I changed the Line
implementation:
// 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 of several megabytes, but it increased the elapsed time by more than 50 milliseconds.
I wonder how is this possible considering that the application has been compiled for release version with speed optimization fully on. Is it due to the argument passing? Is it due to the stack allocation of my struct Properties
?
Update:
The method Line::parse
is called just once for each instance. The data structure is composed by a std::vector
of Line
s. Multiple threads manage a different subset of this vector.
unique_ptr
in yourparse
method? Is yourparse
method called multiple times for the sameLine
? – StomatitisProperties
is allocaded just once in both cases. The first version because of thenew Line()
, the second version on the stack within theparse
method. – Pameliapamelinaparse
is only invoked once perLine
instance. If that is not the case, that information is even more relevant. – Anoleparse_internal
, because the base class method is invoked too. But that leads to more argument passing and not to stack allocation. – Pameliapamelinaparse
. – AnoleProperties
byconst&
rather than pointer but that would not make it run faster. Can you provide a minimal program that reproduces this behavior(so we can really see how this gets called/instantiated)? – Brutusverison 2
doesparse()
need to bevirtual
? Probably won't affect speed though. – BrutusProperties
is still in the i-cache while you construct the lines and atparse
it's not anymore so you have to fetch that code again) – PhotinaLine
contain any other data members? What issizeof(Line)
? What objects doesparse
modify? It is possible that, since you mention multi-threading, the removal of 28 bytes from theLine
structure is causing false-sharing in the access of differentLine
members from multiple threads. – Kershner