Boost logo

Boost Users :

Subject: Re: [Boost-users] String, vector type with stack-allocated internal array (and dynamic array fallback)?
From: Boris Dušek (boris.dusek_at_[hidden])
Date: 2009-07-01 04:40:07


Hello Christian,
thanks for your reply.

On Tue, Jun 30, 2009 at 2:02 PM, Christian Schladetsch <
christian.schladetsch_at_[hidden]> wrote:

> Hi,
>
> I have a library proposal called Boost.Monotonic that does exactly this.
> Documentation is a work in progress, and what there is, is out of date.
> However, you are welcome to dig around
> https://svn.boost.org/svn/boost/sandbox/monotonic/. A good starting point
> is the test suite at http://tinyurl.com/mhwn5b.
>
> Very quickly, it is a storage system that starts on that stack (with a size
> you can specify), then grows to the heap as needed. This is combined with an
> allocator that can use this storage, allowing containers and strings etc to
> use the stack at first, then the heap as needed.
>

This is great - exactly what I have been looking for. I have checked out the
library from trunk and I am now in the process of trying it on my production
code (first I have to finally add some sane repeatable performance testing
to my project, until today I just used to manually ran a profiler and that
was it).

I want to ask a few questions though:

If I write

void some_function() {
   typedef std::basic_string<wchar_t, std::char_traits<wchar_t>,
boost::monotonic::allocator<wchar_t> > bufferwstring;
   bufferwstring key;
   bufferwstring key2;
}

then are key and key2 each having its own buffer (and is the buffer on the
stack, i.e. thread-safe)? Is there a way to specify the size of the buffer?
like boost::monotonic::allocator<wchar_t, 32> (I looked and the other
template parameters are "class"es so not this way), or is the only way the
one below? Or is the probably big default size not an issue (I have not
really experience in these stack-allocated buffers, other than knowing they
are zero-cost).

Is it possible to specify the size roughly like:

void some_func() {
    boost::monotonic::storage<32*sizeof(wchar_t)> storage();
    std::vector<wchar_t, std::char_traits<wchar_t>,
boost::monotonic::allocator<wchar_t> > key(storage);
}

Also in this case (the example you mailed me):

const size_t stack_size = 10*1024;
monotonic::storage<stack_size> storage;
{
     monotonic::string<> str(storage);
     monotonic::vector<Foo> vec(storage);
     // use str and vec; storage will use 10k of stack space, then the heap.
     // resources will be freed when storage goes out of scope.
}

So str and vec are sharing the buffer (storage)? Does that mean that we run
into same performance issues as malloc due to free space management inside
that buffer (at least I suppose that's where the main cost of malloc/free
comes from)?

Also thanks for the pointer to auto_buffer, good to know.

I will try to make some measurements with my code and report back the
results.

Cheers,
Boris

> 2009/6/30 Boris Dušek <boris.dusek_at_[hidden]>
>
>> Hello,
>>
>> for performance reasons, I would like not to have to have a
>> std::wstring (1 allocation/1 deallocation of its character array) in one
>> routine and instead have a wchar_t[MAXSIZE]. But I also would like my code
>> to be correct for sizes bigger than MAXSIZE.
>>
>> Is there some type (in boost or elsewhere, preferable for both
>> basic_string and vector) that defaults to stack-allocated array of some
>> size (preferably configurable by a template parameter), and in case of
>> overflow (i.e. push_back or insert when it's full), would allocate a
>> dynamically-allocated array? This is how it works on MSVC with their STL
>> basic_string implementation, just the array size is unconfigurable (16 bytes
>> including null-terminating byte). I would obviously choose the MAXSIZE so
>> that 99.9% of cases are handled by the stack-allocated array.
>>
>> Thanks,
>> Boris
>>
>> _______________________________________________
>> Boost-users mailing list
>> Boost-users_at_[hidden]
>> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>>
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net