On Thu, 9 Feb 2017 18:03:15 -0600
"Christopher J. Pisz" <
cp...@austin.rr.com> wrote:
> During one interview I was asked about things I would do to optimize
> some code snippet.
>
> I mentioned that
>
> std::vector<Employee> was probably not as good as
> std::vector<Employee *> or std::vector<std::shared_ptr<Employee>>
>
> Because of the copy construction on insert.
> The fellow did not seem to like that answer at all.
>
> I mentioned that depending on the size of the object a collection of
> pointers might be better.
>
> He asked about cache misses.
>
> I really don't know much about how this would interact with the
> cache. I explained that, as I understand it, the employee would have
> to be a certain size to fit them on the cache anyway and if it was a
> really large object, you probably aren't going to get the whole
> collection in there anyway.
>
> Now that I think about it, we have emplace and move, so maybe my
> point was silly.
If the Employee type is mainly composed of fundamental types (integers,
floating points and so forth, say comprising an employee id number, the
rate of pay, a location code and the like) or moderately sized arrays of
these, then holding Employee objects in the vector by value is going to
be considerably faster because of the excellent locality. You will have
both data in cache and pre-fetchability, and also copying will be fast.
If however the Employee type has member data which is allocated on the
heap (which will degrade locality as between both different members of
an Employee object and different Employee objects in the vector) and is
expensive to copy, and it does not have a move constructor and move
assignment operator, then you will probably find yourself better off
holding objects of that type in the vector by pointer, unique_ptr or
shared_ptr. If the Employee type does have a move constructor and move
assignment operator then much of its implementation is probably
allocated on the heap anyway to support movability, and if the use case
mainly involves moving Employee objects into the vector rather than
copying them there, it would normally be better to hold them by value.
In the second case (the Employee type is not mainly composed of
fundamental types or moderately sized arrays of these) you are in a
grey area. You need to know more about the details of the
implementation of the type and how objects of the type are used. Mainly
though, you would need to measure.
Chris