Seungbeom Kim <
musi...@bawi.org> wrote in news:kdkuj2$dio$1
@
usenet.stanford.edu:
> On 2013-01-21 09:37, Paavo Helde wrote:
>>
>> Note that a program needs to convert static strings into std::strings
>> only once. Static strings are part of the binary image on disk.
Reading
>> the data in from the disk is orders of magnitude slower than
performing
>> the in-memory copy for initializing std::string, so there is hardly
any
>> point in trying to optimize the latter. If loading static data is too
>> slow you most probably need to get a faster hard drive instead.
>
> But there's a difference in the memory footprint. With another layer
> of dynamic allocation, the process consumes twice the address space
> for each such string that could have remained only in the read-only
> segment. In low-memory situations, this could cause other pages to be
> swapped out to disk.
Yes, the memory footprint may be a problem in case of some programs.
However, this depends on the amount of static data and its usage. If a
program uses gigabytes of static data which goes mostly unmodified, then
yes, it might be appropriate to avoid copying it into std::strings.
Somehow I suspect though that OP was not talking about gigabytes.
Even if he was (talking about gigabytes), the key point would be not to
access the data at all until needed. This can be done with std::strings
as well.
Note that once the data has been copied, the read-only pages can be
swapped out from the working set again (i.e. discarded). This is done
automatically by the OS in case of low memory AFAIK. So the memory
consumption is not doubled in principle, it is only just the read-write
pages are more expensive to deal with in case of memory exhaustion. IOW,
if the memory is exhausted and all programs start trashing, a program
using dynamic memory will trash worse than the one using static memory.
Not sure if this scenario is worth optimizing.
> Of course, it is another story how likely is a program with such a
> large amount of static data to affect the overall system performance.
If most of the static data is never accessed, it should be OK with
current OS-es, the unused parts of the executable file ought to be never
read into the physical RAM.
Cheers
Paavo