C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Lightweight std::string replacement?
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Stefan Arentz
Guest





PostPosted: Tue Dec 30, 2003 5:10 am    Post subject: Lightweight std::string replacement? Reply with quote




For an embedded project I am looking for a lightweight std::string
replacement. There is too much in the std one that I don't need.

What I basically need is a simple container for ASCII strings with
no bells and whistles.

Before I write such a thing, does anyone know of a decent one that
is available?

Stefan


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Thorsten Ottosen
Guest





PostPosted: Tue Dec 30, 2003 12:48 pm    Post subject: Re: Lightweight std::string replacement? Reply with quote



"Stefan Arentz" <stefan.arentz (AT) soze (DOT) com> wrote

Quote:

For an embedded project I am looking for a lightweight std::string
replacement. There is too much in the std one that I don't need.

What I basically need is a simple container for ASCII strings with
no bells and whistles.

Then only use the interface that you need. Why can't you do that?

br

Thorsten



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Martijn Lievaart
Guest





PostPosted: Tue Dec 30, 2003 6:50 pm    Post subject: Re: Lightweight std::string replacement? Reply with quote



On Tue, 30 Dec 2003 00:10:13 -0500, Stefan Arentz wrote:

Quote:

For an embedded project I am looking for a lightweight std::string
replacement. There is too much in the std one that I don't need.

What I basically need is a simple container for ASCII strings with
no bells and whistles.

Before I write such a thing, does anyone know of a decent one that
is available?

How about...... drumroll ...... std::string? :-)

What bells and whistles are you refering to? Unless you have a bad
compiler or STL implementation, the overhead should not be dramatic, you
only get what you use. Most of it is (should be) templated code that only
gets instantiated if you actually use it.

Is this based on measurements, or are you just assuming that it is to
heavy.

HTH,
M4


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Scott McCaskill
Guest





PostPosted: Tue Dec 30, 2003 8:33 pm    Post subject: Re: Lightweight std::string replacement? Reply with quote

Stefan Arentz <stefan.arentz (AT) soze (DOT) com> wrote

Quote:
For an embedded project I am looking for a lightweight std::string
replacement. There is too much in the std one that I don't need.

What I basically need is a simple container for ASCII strings with
no bells and whistles.

Before I write such a thing, does anyone know of a decent one that
is available?


I haven't used it, but you might have a look at http://bstring.sourceforge.net/

Scott McCaskill

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Fred Jackson
Guest





PostPosted: Tue Dec 30, 2003 8:37 pm    Post subject: Re: Lightweight std::string replacement? Reply with quote

Stefan Arentz <stefan.arentz (AT) soze (DOT) com> wrote

Quote:
For an embedded project I am looking for a lightweight std::string
replacement.

See if this meets your needs.
http://jbschumacher.50megs.com/cpp/projects/apxstring/

Looks like very minimal changes would be required for embedded use.

[ 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





PostPosted: Wed Dec 31, 2003 12:39 am    Post subject: Re: Lightweight std::string replacement? Reply with quote

Stefan Arentz <stefan.arentz (AT) soze (DOT) com> wrote
Quote:
For an embedded project I am looking for a lightweight std::string
replacement. There is too much in the std one that I don't need.

Too much what?

Other than the traits class, string is already pretty lightweight.
Hopefully with a good library, most of the features you don't use
aren't included in the compiled executable. If all you do is construct,
copy and append strings, you may find that it doesn't take much more
overhead than you would have to write anyway.

(However, I haven't tried this... it may be that on many implementations,
it drags in every string function plus the entire traits tree.)

Quote:
What I basically need is a simple container for ASCII strings with
no bells and whistles.

Before I write such a thing, does anyone know of a decent one that
is available?

Before you re-design the class, you should have a clear idea what
"bells and whistles" it is that you don't need. Are you trying to make
the program more space-efficient? Trying to prevent code bloat? Trying
to make it work faster? Write this down before you begin, and look at
it often.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
apm
Guest





PostPosted: Wed Dec 31, 2003 1:07 am    Post subject: Re: Lightweight std::string replacement? Reply with quote

Stefan Arentz <stefan.arentz (AT) soze (DOT) com> wrote

Quote:
For an embedded project I am looking for a lightweight std::string
replacement. There is too much in the std one that I don't need.

What I basically need is a simple container for ASCII strings with
no bells and whistles.

Before I write such a thing, does anyone know of a decent one that
is available?

John Panzer wrote one for the CUJ a while ago which I had a go with.
It seems to be roughly twice as fast as std::string for most
operations. Presumably you are trying to avoid the overhead of mad COW
disease?

-apm

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Stephen Howe
Guest





PostPosted: Wed Dec 31, 2003 1:40 am    Post subject: Re: Lightweight std::string replacement? Reply with quote

Quote:
For an embedded project I am looking for a lightweight std::string
replacement. There is too much in the std one that I don't need.

You mean std::string member functions are linked in despite the fact that
you don't use them?

Quote:
What I basically need is a simple container for ASCII strings with
no bells and whistles.

Before I write such a thing, does anyone know of a decent one that
is available?

Have you considered the deprecated strstream, ostrstream, istrstream family?
There are a few gotcha's to using these but they wrap ASCII strings and they
can wrap fixed-size char buffers as well as allow dynamic growth. They might
be what you want.

Stephen Howe




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Pete Becker
Guest





PostPosted: Wed Dec 31, 2003 1:07 pm    Post subject: Re: Lightweight std::string replacement? Reply with quote

apm wrote:
Quote:

John Panzer wrote one for the CUJ a while ago which I had a go with.
It seems to be roughly twice as fast as std::string for most
operations. Presumably you are trying to avoid the overhead of mad COW
disease?


If you're going to base decisions on performance then you need to say
which implementations you're talking about. Same thing if you're
concerned about COW: some implementations use it, some don't.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Dhruv
Guest





PostPosted: Wed Dec 31, 2003 1:08 pm    Post subject: Re: Lightweight std::string replacement? Reply with quote

On Tue, 30 Dec 2003 20:07:53 -0500, apm wrote:

[...]

Quote:
John Panzer wrote one for the CUJ a while ago which I had a go with.
It seems to be roughly twice as fast as std::string for most
operations. Presumably you are trying to avoid the overhead of mad COW
disease?

I thought that the mad COW disease had been eliminated some time ago due
to the threads attached with it?


Regards,
-Dhruv.




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Stefan Arentz
Guest





PostPosted: Wed Dec 31, 2003 2:52 pm    Post subject: Re: Lightweight std::string replacement? Reply with quote

"Stephen Howe" <NOSPAMsjhowe (AT) dial (DOT) pipex.com> writes:

Quote:
For an embedded project I am looking for a lightweight std::string
replacement. There is too much in the std one that I don't need.

You mean std::string member functions are linked in despite the fact that
you don't use them?

The difference between using std::string and my own String class:

// stringtest1
int main() {
String s = "fluts";
s += "xxx";
return 0;
}

// stringtest2
int main() {
std::string s = "fluts";
s += "xxx";
return 0;
}

-rwxr-xr-x 1 stefan stefan 40576 Dec 31 06:08 stringtest1*
-rwxr-xr-x 1 stefan stefan 459324 Dec 31 06:08 stringtest2*

Binaries are statically linked and stripped.

This is with:

14:11 sources > gcc -v
Using built-in specs.
Configured with: FreeBSD/i386 system compiler
Thread model: posix
gcc version 3.2.2 [FreeBSD] 20030205 (release)

Now, I understand that GCC's libstdc++ might not be the best, but what I really do
not understand is why it needs to link with so much cruft when I essentially only
only use a constructor and one operator of std::string.

Anyway, thanks for all the hints people gave me. I ended up writing my own that just
implements this:

const String& operator = (char c);
const String& operator = (const char* s);
const String& operator = (const String& s);

const char & operator [] (size_t index) const;
char & operator [] (size_t index);

size_t GetLength() const;
const char* GetString() const;

void Reserve(size_t size);
void Clear();

String& operator += (char c);
String& operator += (const char* s);
String& operator += (const String& s);

bool operator == (const String& s) const;
bool operator == (const char* s) const;

Works fine now ;-)

More to uncode!

S.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
James Kanze
Guest





PostPosted: Thu Jan 01, 2004 4:03 pm    Post subject: Re: Lightweight std::string replacement? Reply with quote

"Dhruv" <dhruvbird (AT) gmx (DOT) net> writes:

Quote:
On Tue, 30 Dec 2003 20:07:53 -0500, apm wrote:

[...]

John Panzer wrote one for the CUJ a while ago which I had a go
with. It seems to be roughly twice as fast as std::string for
most operations. Presumably you are trying to avoid the overhead
of mad COW disease?

I thought that the mad COW disease had been eliminated some time ago
due to the threads attached with it?

It depends on what you are doing (or trying to do). At least two
implementations, to my knowledge, use COW : Rogue Wave and g++.

Depending on the application, COW can be a real win in a single threaded
environment. Depending on the application, it can also be a win in a
multithreaded environment, provided the environment has a rapid way of
making atomic increment and decrement (Intel, Sparc v9, Alpha, and
doubtlessly others do), and the implementation uses it.

Note, however, that in a multithreaded environment, I'm not sure that an
implementation giving the full Posix guarantees is possible with
reasonable performance. Of the two implementations I know, g++ makes a
much weaker guarantee, and Rogue Wave simply mutex locks everything, at
a fairly high cost in performance.

I would consider this above all a defect in the specification of the
class.

--
James Kanze mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France +33 1 41 89 80 93

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Stefan Arentz
Guest





PostPosted: Fri Jan 02, 2004 1:01 pm    Post subject: Re: Lightweight std::string replacement? Reply with quote

James Kanze <kanze (AT) alex (DOT) gabi-soft.fr> writes:

Quote:
"Dhruv" <dhruvbird (AT) gmx (DOT) net> writes:

|> On Tue, 30 Dec 2003 20:07:53 -0500, apm wrote:

|> [...]

|> > John Panzer wrote one for the CUJ a while ago which I had a go
|> > with. It seems to be roughly twice as fast as std::string for
|> > most operations. Presumably you are trying to avoid the overhead
|> > of mad COW disease?

|> I thought that the mad COW disease had been eliminated some time ago
|> due to the threads attached with it?

It depends on what you are doing (or trying to do). At least two
implementations, to my knowledge, use COW : Rogue Wave and g++.

What is COW :-)

{Copy on Write. -mod/fwg}

S.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Joshua Lehrer
Guest





PostPosted: Fri Jan 02, 2004 1:03 pm    Post subject: Re: Lightweight std::string replacement? Reply with quote

Stefan Arentz <stefan (AT) tiSateh (DOT) local> wrote

Quote:
The difference between using std::string and my own String class:

// stringtest1
int main() {
String s = "fluts";
s += "xxx";
return 0;
}

// stringtest2
int main() {
std::string s = "fluts";
s += "xxx";
return 0;
}


Our RW implementation does not have a += that takes a "const char *",
just one that takes an "std::string". Thus, the above += requires a
string construction, at least one copy, and a destruction of the
temporary.

Quote:

Anyway, thanks for all the hints people gave me. I ended up writing my own that just
implements this:

const String& operator = (char c);
const String& operator = (const char* s);
const String& operator = (const String& s);


No need for these three methods. Given that you have constructors
that take char, const char*, and const String &, you need only one
operator=, one that takes a String. And why did you return "const"?

String& operator=(String temp) { temp.swap(*this); return *this; }

Quote:
const char & operator [] (size_t index) const;
char & operator [] (size_t index);

size_t GetLength() const;

why not call this size() and length(), just like the standard? You
might as well get used to using "correct" method names.

Quote:
const char* GetString() const;

again, I'd prefer c_str or data. GetString, shouldn't that return the
"String"?
Quote:

void Reserve(size_t size);
void Clear();


again, reserve() and clear()

Quote:
String& operator += (char c);
String& operator += (const char* s);
String& operator += (const String& s);

bool operator == (const String& s) const;
bool operator == (const char* s) const;

why no op==(char), you have op=(char) and op+=(char)


-joshua lehrer
factset reseach systems
NYSE:FDS

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Howard Hinnant
Guest





PostPosted: Sat Jan 03, 2004 4:10 am    Post subject: Re: Lightweight std::string replacement? Reply with quote

In article <m28yktxh5v.fsf (AT) tiSateh (DOT) local>,
Stefan Arentz <stefan (AT) tiSateh (DOT) local> wrote:

Quote:
The difference between using std::string and my own String class:

// stringtest1
int main() {
String s = "fluts";
s += "xxx";
return 0;
}

// stringtest2
int main() {
std::string s = "fluts";
s += "xxx";
return 0;
}

-rwxr-xr-x 1 stefan stefan 40576 Dec 31 06:08 stringtest1*
-rwxr-xr-x 1 stefan stefan 459324 Dec 31 06:08 stringtest2*

Binaries are statically linked and stripped.

This is with:

14:11 sources > gcc -v
Using built-in specs.
Configured with: FreeBSD/i386 system compiler
Thread model: posix
gcc version 3.2.2 [FreeBSD] 20030205 (release)

Wow, impressive!

I'm testing on a Mac PPC instead of x86, but I am sitting on BSD. With
static linking and the Metrowerks compiler I get:

-rwxr-xr-x 1 hinnant hinnant 40240 2 Jan 16:09 helloworld

but switching over to g++ (opts O4, version 3.3) I get:

-rwxr-xr-x 1 hinnant hinnant 111712 2 Jan 16:13 a.out

These both of course are with your std::string test.

-Howard

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.