What do people on here think of the string class that David Eberly uses in his '3D Game Engine Architecture' book..?
string class....?
This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.
-
-
I don't remember the string class he uses. Can you post the interface for it?
-Rez -
Hi Rez,
Thanks for the reply..:)
Here is the string class..:
#ifndef WM3STRING_H
#define WM3STRING_H
#include "Wm3System.h"
namespace Wm3
{
class WM3_ITEM String
{
public:
// construction and destruction
String (const char* acText = 0);
String (int iLength, const char* acText);
String (const String& rkString);
~String ();
// member access
int GetLength () const;
operator const char* () const;
// assignment, comparisons, implicit conversion (support for hash tables)
String& operator= (const String& rkString);
String& operator+= (const String& rkString);
String operator+ (const String& rkString);
bool operator== (const String& rkString) const;
bool operator!= (const String& rkString) const;
operator unsigned int () const;
// memory and disk footprint (support for streaming)
int GetMemoryUsed () const;
int GetDiskUsed () const;
// Case-related
String ToUpper () const;
String ToLower () const;
private:
// Text is stored as null-terminated character string in memory. The
// length counts all but the non-null character. When written to disk,
// the length is stored first and all but the non-null character are
// stored second.
int m_iLength;
char* m_acText;
};
#include "Wm3String.inl"
}
#endif -
The interface seems nice and lightweight, but I don't really see an advantage over the stl std::string class. Why not just use that since it's standard C++?
-Rez -
The interface seems nice and lightweight, but I don't really see an advantage over the stl std::string class. Why not just use that since it's standard C++?
That is what I was thinking....most other books I have read(all in fact...I think) all use standard std::string.
Oh...bassically he says that its more efficient for streaming strings than the std::string..
..efficiency for real-time rendering and all that.. -
Every game I've worked on has used std::string except for Brain Quest. We used a very specialized implementation for performance reasons (this was a DS game that needed to deal with tons of strings). Then again, the first professional game I worked on as a programmer Barbie Diaries a few years ago. I'm curious what someone like Mike or Kain has to say about since they've been in the industry a lot longer than I have.
-Rez -
If your string usage is moderate, more importantly not changing too much, std::string is probably fine. Most recent implementations also have nice optimizations built in to reduce their activity in the heap.
If you end up passing strings by value a lot, or mutating them a lot, or just have ridiculous amounts of text to manage, then you probably want something more specialized in your title.
I've seen std::string ABUSE go HORRIBLY BAD though and kill title performance, cause memory leaks and severe fragmentation, and other ills. On a windows-platform game I worked on in '02 we saw the heap manager using up to 30% of frame time (over 10,000 heap actions per game frame) and while we did not get 100% proof it was the belief of the programming staff that it was the std::string instances being wontonly created all over the place, locally, that was contributing primarily.
Before you go rolling a special string though, I'd say roll a special string allocation pool. Look up how to induce std::string (e.g. std::basic_string<>) to use a specific allocator instead of the default, and keep you string data in one memory pool separate for the rest of the heap.
-
Share
- Facebook 0
- Twitter 0
- Google Plus 0
- Reddit 0
-
Users Online 1
1 Guest