Topic: Efficient storage management in std::stringstream
Author: Jim Cobban <thesnaguy@hotmail.com>
Date: 2000/04/15 Raw View
This is a multi-part message in MIME format.
--------------9CABDBC9C743906474E813FB
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
As std::stringstream is currently defined there is no mechanism for the
programmer to influence the algorithm for storage allocation. This will
not matter most of the time because most uses of std::stringstream are
for short data formatting exercises and I trust the manufacturers to
have set the initial allocation in such a way that these common uses
will not demand reallocation. However because it was the easiest way to
write it, the other day I wrote a program which output almost 10,000
characters into an ostringstream. I must assume that this caused
frequent reallocations and associated data copies.
By contrast in the old strstream there was a constructor which specified
the initial allocation, and moreover the C++ standard indicates that
when reallocating the amount of this initial allocation is to be
considered.
As it stands it would appear that the only way to influence the
allocation algorithm used by stringstream is to either define your own
stringbuf, or to define your own string type with a private allocator.
This seems to me to be a deficiency of the stringstream by comparison
with the strstream.
I therefore suggest that there should either be a stringstream
constructor which permits supplying the initial allocation amount, as
with strstream, or else that there should be a reserve method, as with
the std::vector and std::string classes.
--
Jim Cobban jcobban@magma.ca
34 Palomino Dr.
Kanata, ON, CANADA
K2M 1M1
+1-613-592-9438
--------------9CABDBC9C743906474E813FB
Content-Type: text/x-vcard; charset=us-ascii;
name="thesnaguy.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Jim Cobban
Content-Disposition: attachment;
filename="thesnaguy.vcf"
begin:vcard
n:Cobban;James
tel;fax:+1-613-592-9438
tel;home:+1-613-592-9438
x-mozilla-html:FALSE
url:http://www.magma.ca/~jcobban
version:2.1
email;internet:thesnaguy@hotmail.com
title:Consultant
adr;quoted-printable:;;34 Palomino Dr.=0D=0A;Kanata;ON;K2M 1M1;Canada
fn:Jim Cobban
end:vcard
--------------9CABDBC9C743906474E813FB--
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 2000/04/15 Raw View
Jim Cobban wrote:
>
> As std::stringstream is currently defined there is no mechanism for the
> programmer to influence the algorithm for storage allocation. This will
> not matter most of the time because most uses of std::stringstream are
> for short data formatting exercises and I trust the manufacturers to
> have set the initial allocation in such a way that these common uses
> will not demand reallocation. However because it was the easiest way to
> write it, the other day I wrote a program which output almost 10,000
> characters into an ostringstream. I must assume that this caused
> frequent reallocations and associated data copies.
>
> By contrast in the old strstream there was a constructor which specified
> the initial allocation, and moreover the C++ standard indicates that
> when reallocating the amount of this initial allocation is to be
> considered.
>
> As it stands it would appear that the only way to influence the
> allocation algorithm used by stringstream is to either define your own
> stringbuf, or to define your own string type with a private allocator.
> This seems to me to be a deficiency of the stringstream by comparison
> with the strstream.
>
> I therefore suggest that there should either be a stringstream
> constructor which permits supplying the initial allocation amount, as
> with strstream, or else that there should be a reserve method, as with
> the std::vector and std::string classes.
Can you get the desired effect using str()->reserve()?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Jim Cobban <thesnaguy@hotmail.com>
Date: 2000/04/16 Raw View
This is a multi-part message in MIME format.
--------------FBE8F08EEA61989E32600551
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
James Kuyper wrote:
> > I therefore suggest that there should either be a stringstream
> > constructor which permits supplying the initial allocation amount, as
> > with strstream, or else that there should be a reserve method, as with
> > the std::vector and std::string classes.
>
> Can you get the desired effect using str()->reserve()?
The value returned by the str() method is a const std::string, and therefore
you cannot use the reserve method with it.
Dietmar Kuehl has also suggested that if the form of the constructor which
takes an existing string paid attention to a reserve done on that string the
same effect would be achieved. But firstly this would require allocating, at
least temporarily, TWO large data areas, and secondly the standard does not
require the stringstream constructor to pay any attention to anything other
than the value of the string parameter.
--
Jim Cobban jcobban@magma.ca
34 Palomino Dr.
Kanata, ON, CANADA
K2M 1M1
+1-613-592-9438
--------------FBE8F08EEA61989E32600551
Content-Type: text/x-vcard; charset=us-ascii;
name="thesnaguy.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Jim Cobban
Content-Disposition: attachment;
filename="thesnaguy.vcf"
begin:vcard
n:Cobban;James
tel;fax:+1-613-592-9438
tel;home:+1-613-592-9438
x-mozilla-html:FALSE
url:http://www.magma.ca/~jcobban
version:2.1
email;internet:thesnaguy@hotmail.com
title:Consultant
adr;quoted-printable:;;34 Palomino Dr.=0D=0A;Kanata;ON;K2M 1M1;Canada
fn:Jim Cobban
end:vcard
--------------FBE8F08EEA61989E32600551--
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 2000/04/18 Raw View
Jim Cobban wrote:
>
> James Kuyper wrote:
>
> > > I therefore suggest that there should either be a stringstream
> > > constructor which permits supplying the initial allocation amount, as
> > > with strstream, or else that there should be a reserve method, as with
> > > the std::vector and std::string classes.
> >
> > Can you get the desired effect using str()->reserve()?
>
> The value returned by the str() method is a const std::string, and therefore
> you cannot use the reserve method with it.
According to the standard, it isn't const. However, it is a temporary
copy, rather than a pointer or reference to the actual underlying
string. That makes it equally useless for this purpose. I apologize for
making a bad suggestion.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]