Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could accomplish something like this -- but it's not likely you'd gain all that much.</p> <p>First, there's the performance aspect. You are trading time for memory consumption. Whatever memory you save is going to be offset by having to do the addition and a multiply on every call to <code>end</code> (okay, if it's a vector where <code>sizeof(vector&lt;t&gt;::value_type) == 1</code> the multiply can be optimized out). Note that most handwritten looping code over vectors calls <code>end</code> on every loop iteration. On modern CPUs that's actually going to be a major win, because it allows the processor to keep more things in cache; unless those couple of extra instructions in an inner loop force the processor to swap things in the instruction cache too often)</p> <p>Moreover, the memory savings is likely to be small in terms of the overall memory use in the vector, for the following reasons:</p> <ul> <li>Memory manager overhead. Each allocation from the memory manager, (which vector of course needs) is going to add 16-24 bytes of overhead on its own in most memory manager implementations. (Assuming something like <code>dlmalloc</code> (UNIX/Linux/etc.) or <code>RtlHeap</code> (Windows))</li> <li>Overprovisioning load. In order to achieve amortized constant insertion and removal at the end, when vector resizes, it resizes to some multiple of the size of the data in the vector. This means that the typical memory capacity vector allocates is enough for 1.6 (MSVC++) or 2 (STLPort, libstdc++) times the number of elements actually stored in the vector.</li> <li>Alignment restrictions. If you are putting those many vectors into an array (or another vector), then keep in mind the first member of that vector is still a pointer to the allocated memory block. This pointer generally needs to be 8 byte aligned anyway -- so the 4 bytes you save are lost to structure padding in arrays.</li> </ul> <p>I'd use the plain implementation of vector for now. If you run your code through a memory profiler and find that a significant savings would be made by getting rid of these couple of pointers, then you're probably off implementing your own optimized class which meets your performance characteristics rather than relying on the built in vector implementation. (An example of one such optimized class is <code>std::string</code> on those platforms that implement the small string optimization)</p> <p>(Note: the only compiler of which I am aware that optimizes out the <code>Alloc</code> pointer is VC11, which isn't yet released. Though Nim says that the current prerelease version of libstdc++ does it as well...)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload