Skip to content

Commit 3e4547a

Browse files
fcheAbegail Jakop
authored andcommitted
interned_string: drop magic c_str() implementation
It turns out that while it was right for boost::string_ref not to provide it, we can, because our interned_string's backing stores are full (read-only) std::strings, which always carry a \0. This shrinks down our interned_string to 2*sizeof(void*) bytes.
1 parent 419117a commit 3e4547a

File tree

3 files changed

+10
-28
lines changed

3 files changed

+10
-28
lines changed

INTERNALS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,6 @@ Peculiarities
144144

145145
OTOH, it costs CPU (for management of the interned string set, or if
146146
copied between std::string and interned_string unnecessarily), and
147-
RAM (3 pointers when empty, vs. 1 for std::string), and its
147+
RAM (2 pointers when empty, vs. 1 for std::string), and its
148148
instances are not modifiable, so tradeoffs must be confirmed with
149149
tools like memusage, massif, perf-stat, etc.

stringtable.cxx

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ interned_string interned_string::intern(const string& value)
103103
}
104104

105105

106-
interned_string::interned_string(const char* value): string_ref(intern(value)), _c_str(0)
106+
interned_string::interned_string(const char* value): string_ref(intern(value))
107107
{
108108
}
109109

110-
interned_string::interned_string(const string& value): string_ref(intern(value)), _c_str(0)
110+
interned_string::interned_string(const string& value): string_ref(intern(value))
111111
{
112112
}
113113

@@ -155,17 +155,5 @@ size_t interned_string::find (const char *f) const
155155
}
156156
#endif
157157

158-
// The result of c_str() is basically strdup()'d, in anticipation
159-
// that interning may result in string_refs that are not followed by \0,
160-
// so we can't just pass back ->data(). The strdup()'d memory is
161-
// saved in a member variable and freed in the destructor.
162-
const char* interned_string::c_str() const
163-
{
164-
free (_c_str);
165-
_c_str = (char*) malloc(this->length()+1);
166-
strncpy(_c_str, this->data(), this->length());
167-
_c_str[this->length()]='\0';
168-
return _c_str;
169-
}
170158

171159
/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */

stringtable.h

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,20 @@
2121
struct interned_string: public boost::string_ref
2222
{
2323
// all these construction operations intern the incoming string
24-
interned_string(): boost::string_ref(), _c_str(0) {}
24+
interned_string(): boost::string_ref() {}
2525
interned_string(const char* value);
2626
interned_string(const std::string& value);
27-
interned_string(const boost::string_ref& value): boost::string_ref(value), _c_str(0) {}
28-
interned_string(const interned_string& value): boost::string_ref(value), _c_str(0) {}
29-
interned_string& operator = (const interned_string& value) {
30-
if (&value==this) return *this;
31-
boost::string_ref::operator = (value);
32-
// NB: don't propagate _c_str!
33-
return *this;
34-
}
27+
interned_string(const boost::string_ref& value): boost::string_ref(value) {}
28+
interned_string(const interned_string& value): boost::string_ref(value) {}
3529
interned_string& operator = (const std::string& value);
3630
interned_string& operator = (const char* value);
3731

38-
~interned_string() { if (_c_str) free (_c_str); }
39-
4032
// easy out-conversion operators
4133
operator std::string () const { return this->to_string(); }
42-
const char* c_str() const;
34+
35+
// NB: this is OK because we always use a std::string as a backing
36+
// store, and as of c++0x, those always store a \0 in their data().
37+
const char* c_str() const { return this->size() ? (const char*)this->data() : ""; }
4338

4439
// boost oversights
4540
template <typename F>
@@ -65,7 +60,6 @@ struct interned_string: public boost::string_ref
6560
#endif
6661

6762
private:
68-
mutable char *_c_str; // last value copied out
6963
interned_string intern(const std::string& value);
7064
};
7165

0 commit comments

Comments
 (0)