ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/shstr.C
(Generate patch)

Comparing deliantra/server/common/shstr.C (file contents):
Revision 1.5 by root, Sun Sep 3 09:00:05 2006 UTC vs.
Revision 1.12 by root, Thu Sep 7 07:52:58 2006 UTC

3 */ 3 */
4 4
5#include <cstring> 5#include <cstring>
6#include <cstdlib> 6#include <cstdlib>
7 7
8#include <glib.h>
9
8#include <tr1/unordered_set> 10#include <tr1/unordered_set>
9 11
10#include "shstr.h" 12#include "shstr.h"
13#include "util.h"
11 14
12// NOTE: even with lots of stuff loaded, we do not usually have >>20000 strings.
13// maybe refcounting is just overhead?
14
15struct hash
16{
17 std::size_t operator ()(const char *s) const
18 {
19 unsigned long hash = 0;
20 unsigned int i = 0;
21
22 /* use the one-at-a-time hash function, which supposedly is
23 * better than the djb2-like one used by perl5.005, but
24 * certainly is better then the bug used here before.
25 * see http://burtleburtle.net/bob/hash/doobs.html
26 */
27 while (*s)
28 {
29 hash += *s++;
30 hash += hash << 10;
31 hash ^= hash >> 6;
32 }
33
34 hash += hash << 3;
35 hash ^= hash >> 11;
36 hash += hash << 15;
37
38 return hash;
39 }
40};
41
42struct equal
43{
44 bool operator ()(const char *a, const char *b) const
45 {
46 return !strcmp (a, b);
47 }
48};
49
50typedef std::tr1::unordered_set<const char *, hash, equal> HT; 15typedef std::tr1::unordered_set<const char *, str_hash, str_equal> HT;
51 16
52static HT ht; 17static HT ht;
18
19static const char *makevec (const char *s)
20{
21 int len = strlen (s);
22
23 const char *v = (const char *)(2 + (int *)g_slice_alloc (sizeof (int) * 2 + len + 1));
24
25 shstr::length (v) = len;
26 shstr::refcnt (v) = 1;
27
28 memcpy ((char *)v, s, len + 1);
29
30 return v;
31}
32
33const char *shstr::null = makevec ("<nil>");
34
35// what weird misoptimisation is this again?
36const shstr undead_name ("undead");
53 37
54const char * 38const char *
55shstr::find (const char *s) 39shstr::find (const char *s)
56{ 40{
57 if (!s) 41 if (!s)
66 50
67const char * 51const char *
68shstr::intern (const char *s) 52shstr::intern (const char *s)
69{ 53{
70 if (!s) 54 if (!s)
71 return s; 55 return null;
72 56
73 if (const char *found = find (s)) 57 if (const char *found = find (s))
74 { 58 {
75 ++refcnt (found); 59 ++refcnt (found);
76 return found; 60 return found;
77 } 61 }
78 62
79 int len = strlen (s); 63 s = makevec (s);
80
81 const char *v = (const char *)(2 + (int *)malloc (sizeof (int) * 2 + len + 1));
82
83 length (v) = len;
84 refcnt (v) = 1;
85
86 memcpy ((char *)v, s, len + 1);
87
88 ht.insert (v); 64 ht.insert (s);
89
90 return v; 65 return s;
91} 66}
92 67
93// periodically test refcounts == 0 for a few strings 68// periodically test refcounts == 0 for a few strings
94// this is the ONLY thing that erases stuff from ht. keep it that way. 69// this is the ONLY thing that erases stuff from ht. keep it that way.
95void 70void
101 76
102 if (i == ht.end ()) 77 if (i == ht.end ())
103 i = ht.begin (); 78 i = ht.begin ();
104 79
105 // go through all strings roughly once every 4 minutes 80 // go through all strings roughly once every 4 minutes
106 for (int n = ht.size () / 256 + 16; --n; ) 81 int n = ht.size () / 256 + 16;
82
83 for (;;)
107 { 84 {
108 if (i == ht.end ()) 85 if (i == ht.end ())
109 { 86 {
110 curpos = 0; 87 curpos = 0;
111 return; 88 return;
112 } 89 }
113 90 else if (!--n)
91 break;
114 if (!refcnt (*i)) 92 else if (!refcnt (*i))
115 { 93 {
116 HT::iterator o = i++; 94 HT::iterator o = i++;
117 const char *s = *o; 95 const char *s = *o;
118 ht.erase (o); 96 ht.erase (o);
119 97
120 //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); 98 //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s));
121 free (-2 + (int *)s); 99 g_slice_free1 (sizeof (int) * 2 + length (s) + 1, -2 + (int *)s);
122 } 100 }
123 else 101 else
124 ++i; 102 ++i;
125 } 103 }
126 104

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines