This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: Patch 2: speeding up the basic_string modifications
- To: Ryszard Kabatek <Ryszard dot Kabatek at softax dot pl>
- Subject: Re: Patch 2: speeding up the basic_string modifications
- From: Carlo Wood <carlo at alinoe dot com>
- Date: Thu, 12 Jul 2001 16:01:44 +0200
- Cc: libstdc++ at gcc dot gnu dot org
- References: <3B4D60F6.BDD3D62D@softax.pl>
On Thu, Jul 12, 2001 at 10:33:58AM +0200, Ryszard Kabatek wrote:
> ! _Rep* __r = _Rep::_S_create(__new_size, __a);
...
> + size_type __new_capacity = _M_rep()->_M_is_shared() ?
> + __new_size : // _M_leak_hard
> + (__new_size < 17 ? 32 : __new_size*2);
> + // We get an exception. OK
> + if (__new_size > _Rep::_S_max_size)
> + __new_capacity = __new_size;
> + else if (__new_capacity > _Rep::_S_max_size) // Avoid an exception. OK
> + __new_capacity = _Rep::_S_max_size;
> +
> allocator_type __a = get_allocator();
> ! _Rep* __r = _Rep::_S_create(__new_capacity, __a);
A few questions,
1) will string::capacity() return the exact same value as passed to _Rep::_S_create?
If not I propose a different name, instead of __new_capacity.
2) wasn't the consensus that the growth should be such that it would be a
a power of two *minus* the malloc administration overhead for sizes under
the page size, and a multiple of the page size above that? It seems to me
that this patch might -say- try to allocate a capacity of 512 bytes, resulting
in a memory allocation of 1024 bytes (512 bytes + 4 bytes of malloc administration),
which is a waste of memory.
Having dealt with this _exact_ type of problem before, I feel the need to warn everyone
that it is VERY easy to make mistakes here (allocate the wrong number of bytes) and
therefore urge to create a new, seperate function, with well-defined parameters that
will do the calculation from 'needed space' --> { 'size-to-pass-to-malloc/new' }.
I feel this function will become complex, or at least sensitive to bugs, so it is good
to have it in one place.
--
Carlo Wood <carlo@alinoe.com>