 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Alan McKenney Guest
|
Posted: Thu Sep 15, 2005 9:13 am Post subject: Lightweight string class? |
|
|
We are finding that the standard C++ string class, at
least as implemented by Sun, is too expensive for what
we do with it. We have switched from having std::string
data members to using char [] members and using strncpy(),
etc., and gotten a noticable (and needed) speed-up.
Before I go out and reinvent the wheel, has anyone encapsulated
C-style string handling in a "lightweight" string class?
One thing is that we don't need a string object to be able
to hold an arbitrarily large string; in most cases, when
writing the code, we can deduce an upper bound on the size.
This means you don't have to use "new" and "delete" for the
data array.
We also mainly just copy the strings around and compare them
with other strings and with literal strings, so it doesn't
need to be optimized for inserts and replaces.
I checked Boost, but I didn't see anything like it there.
Is this a frequently-enough encountered problem that there's
a widely-used solution
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maciej Sobczak Guest
|
Posted: Thu Sep 15, 2005 10:18 am Post subject: Re: Lightweight string class? |
|
|
Alan McKenney wrote:
| Quote: | One thing is that we don't need a string object to be able
to hold an arbitrarily large string; in most cases, when
writing the code, we can deduce an upper bound on the size.
|
I think then that what you are really looking for is not the
"lightweight string class" (since general approaches to this use dynamic
memory or at best the short string optimization), but rather "statically
bounded string class". This could be a good idea.
| Quote: | I checked Boost, but I didn't see anything like it there.
|
I think that you could use boost::array<char, N> for this, with some
helper functions for setting the value.
Typedef is your friend, as well:
typedef boost::array<char, 100> MyString;
MyString s;
set_string(s, "Some string literal");
What about book-keeping the current length of the string? Is this needed
and would you take advantage of having it?
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alan McKenney Guest
|
Posted: Thu Sep 15, 2005 11:00 pm Post subject: Re: Lightweight string class? |
|
|
Maciej Sobczak wrote:
| Quote: | Alan McKenney wrote:
snip
I think that you could use boost::array<char, N> for this, with some
helper functions for setting the value.
Typedef is your friend, as well:
typedef boost::array<char, 100> MyString;
MyString s;
set_string(s, "Some string literal");
What about book-keeping the current length of the string? Is this needed
and would you take advantage of having it?
--
Maciej Sobczak : http://www.msobczak.com/
|
The small-string optimization would help us some, since many of
our strings have max size 3 or 4. I'm not sure we want to require
them to all have the same max size, since we also have strings with
32-char max.
What I was hoping for was something like a template:
LWString<3> mem1;
LWString<32> mem2;
etc., which could be used the way std::string is, at least
for assignment, comparison, and conversion to and from const char *.
I think I can "roll my own" in a few days. However, I thought that
maybe someone has dealt with this in a more imaginative or just plain
better way than I would.
I thought that, before I reinvent the wheel (and a square wheel at
that),
I might consult the combined (amassed?) wisdom of the C++ community.
-- Alan McKenne
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Old Wolf Guest
|
Posted: Fri Sep 16, 2005 8:17 am Post subject: Re: Lightweight string class? |
|
|
Alan McKenney wrote:
| Quote: | We are finding that the standard C++ string class, at
least as implemented by Sun, is too expensive for what
we do with it. We have switched from having std::string
data members to using char [] members and using strncpy(),
etc., and gotten a noticable (and needed) speed-up.
|
strncpy() is a poor choice for a string copying function,
because it does not 0-terminate its output in some circumstances,
and wastes time in most circumstances.
For the amount of code required to use strncpy() safely, it turns
out that just using memcpy() or some other such function is simpler.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Fri Sep 16, 2005 8:19 am Post subject: Re: Lightweight string class? |
|
|
Alan McKenney wrote:
| Quote: | The small-string optimization would help us some, since many of
our strings have max size 3 or 4. I'm not sure we want to require
them to all have the same max size, since we also have strings with
32-char max.
|
Maciej Sobczak's idea of a "statically bounded string class" would
be like the "small-string optimization" carried to the max.
| Quote: | What I was hoping for was something like a template:
LWString<3> mem1;
LWString<32> mem2;
etc., which could be used the way std::string is, at least
for assignment, comparison, and conversion to and from const char *.
|
Exactly. Whatever your string size is, that's the size of the internal
char array -- as you said, you wouldn't have to use new[] or delete
EVER.
As an implementation detail, you could derive LWString<int N> from
LWStringBase, which would implement all of the operations that don't
depend on N -- this would drastically reduce code bloat on some
compilers with poor optimization. For instance, on some systems
LWString<2>::operator char* would be distinct from
LWString<3>::operator char*, even though they do exactly the same
thing.
The only disadvantage I see is that users always have to count the size
that is needed.
LWString<6> hello = "hello"; // Wastes 1 or 2 characters
LWString<6> hello = "goodbye"; // Generates a run-time error
The string length wouldn't be automatic, so it's a source of potential
waste and/or errors... and I doubt it could be detected at compile
time.
Any method that involves counting the size of the string on assignment
is going to end up calling operator new[], at least sometimes.
| Quote: | I think I can "roll my own" in a few days. However, I thought that
maybe someone has dealt with this in a more imaginative or just plain
better way than I would.
I thought that, before I reinvent the wheel (and a square wheel
at that), I might consult the combined (amassed?) wisdom of the
C++ community.
|
When you've finished writing it, consider submitting it to Boost?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
indranil Guest
|
Posted: Fri Sep 16, 2005 12:28 pm Post subject: Re: Lightweight string class? |
|
|
You can check out Andrei Alexandrescu's flex_string template. It has the
same interface as std::string but he uses all his policy based design tricks
to use different storage policies as template parameters. He supplies
policies for small string optimisation, copy on string, vector of chars,
amongst others.
The site is down at the moment :- http://www.moderncppdesign.com
"Alan McKenney" <alan_mckenney1 (AT) yahoo (DOT) com> wrote
| Quote: | We are finding that the standard C++ string class, at
least as implemented by Sun, is too expensive for what
we do with it. We have switched from having std::string
data members to using char [] members and using strncpy(),
etc., and gotten a noticable (and needed) speed-up.
Before I go out and reinvent the wheel, has anyone encapsulated
C-style string handling in a "lightweight" string class?
One thing is that we don't need a string object to be able
to hold an arbitrarily large string; in most cases, when
writing the code, we can deduce an upper bound on the size.
This means you don't have to use "new" and "delete" for the
data array.
We also mainly just copy the strings around and compare them
with other strings and with literal strings, so it doesn't
need to be optimized for inserts and replaces.
I checked Boost, but I didn't see anything like it there.
Is this a frequently-enough encountered problem that there's
a widely-used solution
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Tony Delroy Guest
|
Posted: Fri Sep 16, 2005 12:29 pm Post subject: Re: Lightweight string class? |
|
|
Old Wolf wrote:
| Quote: | For the amount of code required to use strncpy() safely, it turns
out that just using memcpy() or some other such function is simpler.
|
Not so. For example,
char buffer[1024];
std::strncpy(buffer, source, sizeof buffer - 1);
buffer[sizeof buffer - 1] = ' '; // note: just cater for
unterminated case...
vs
size_t copy_len = std::max(std::strlen(source), sizeof buffer - 1);
std::memcpy(buffer, source, copy_len);
buffer[copy_len] = ' ';
The memcpy approach can be expected to be slower for having necessarily
used strlen. Particularly bad is that strlen counts all the way to the
end of the source string even though only the first (sizeof buffer - 1)
characters are potentially copied.
Cheers,
Tony
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Sat Sep 17, 2005 1:57 pm Post subject: Re: Lightweight string class? |
|
|
Tony Delroy wrote:
| Quote: | Old Wolf wrote:
For the amount of code required to use strncpy() safely, it turns
out that just using memcpy() or some other such function is simpler.
Not so. For example,
char buffer[1024];
std::strncpy(buffer, source, sizeof buffer - 1);
buffer[sizeof buffer - 1] = ' '; // note: just cater for
// unterminated case...
vs
size_t copy_len = std::max(std::strlen(source), sizeof buffer - 1);
std::memcpy(buffer, source, copy_len);
buffer[copy_len] = ' ';
|
I think that Wolf was suggesting:
std::memcpy(buffer, source, sizeof buffer);
Depending on the definition of source, this could be perfectly safe.
It might copy more bytes than the minimum required, but if it's
quick enough, who cares?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kwikius Guest
|
Posted: Sun Sep 18, 2005 2:45 pm Post subject: Re: Lightweight string class? |
|
|
Allan W wrote:
| Quote: | The only disadvantage I see is that users always have to count the size
that is needed.
LWString<6> hello = "hello"; // Wastes 1 or 2 characters
LWString<6> hello = "goodbye"; // Generates a run-time error
|
you could make use of non-type template parameters:
#include <string>
#include <iostream>
template<char p[]>
struct sstring{
size_t length()const{ return strlen(p);}
operator char const*()const {return p;}
};
#define STATIC_STRING(name,string_constant)
namespace private_{
char name [] = string_constant;
}
sstring<private_::name > name;
STATIC_STRING(s,"hello");
int main()
{
std::cout << s << " " << s.length();
}
the only drawback is that it must always defned be in namespace scope
regards
Andy little
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Darko Miletic Guest
|
Posted: Mon Sep 19, 2005 5:10 pm Post subject: Re: Lightweight string class? |
|
|
indranil wrote:
| Quote: | You can check out Andrei Alexandrescu's flex_string template. It has the
same interface as std::string but he uses all his policy based design tricks
to use different storage policies as template parameters. He supplies
policies for small string optimisation, copy on string, vector of chars,
amongst others.
The site is down at the moment :- http://www.moderncppdesign.com
|
It appears that site is moved to this location:
http://erdani.org/
Darko
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|