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.9 by root, Mon Sep 4 17:16:19 2006 UTC

6#include <cstdlib> 6#include <cstdlib>
7 7
8#include <tr1/unordered_set> 8#include <tr1/unordered_set>
9 9
10#include "shstr.h" 10#include "shstr.h"
11#include "util.h"
11 12
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; 13typedef std::tr1::unordered_set<const char *, str_hash, str_equal> HT;
51 14
52static HT ht; 15static HT ht;
16
17static const char *makevec (const char *s)
18{
19 int len = strlen (s);
20
21 const char *v = (const char *)(2 + (int *)malloc (sizeof (int) * 2 + len + 1));
22
23 shstr::length (v) = len;
24 shstr::refcnt (v) = 1;
25
26 memcpy ((char *)v, s, len + 1);
27
28 return v;
29}
30
31const char *shstr::null = makevec ("<nil>");
32
33// what weird misoptimisation is this again?
34const shstr undead_name ("undead");
53 35
54const char * 36const char *
55shstr::find (const char *s) 37shstr::find (const char *s)
56{ 38{
57 if (!s) 39 if (!s)
66 48
67const char * 49const char *
68shstr::intern (const char *s) 50shstr::intern (const char *s)
69{ 51{
70 if (!s) 52 if (!s)
71 return s; 53 return null;
72 54
73 if (const char *found = find (s)) 55 if (const char *found = find (s))
74 { 56 {
75 ++refcnt (found); 57 ++refcnt (found);
76 return found; 58 return found;
77 } 59 }
78 60
79 int len = strlen (s); 61 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); 62 ht.insert (s);
89
90 return v; 63 return s;
91} 64}
92 65
93// periodically test refcounts == 0 for a few strings 66// periodically test refcounts == 0 for a few strings
94// this is the ONLY thing that erases stuff from ht. keep it that way. 67// this is the ONLY thing that erases stuff from ht. keep it that way.
95void 68void
101 74
102 if (i == ht.end ()) 75 if (i == ht.end ())
103 i = ht.begin (); 76 i = ht.begin ();
104 77
105 // go through all strings roughly once every 4 minutes 78 // go through all strings roughly once every 4 minutes
106 for (int n = ht.size () / 256 + 16; --n; ) 79 int n = ht.size () / 256 + 16;
80
81 for (;;)
107 { 82 {
108 if (i == ht.end ()) 83 if (i == ht.end ())
109 { 84 {
110 curpos = 0; 85 curpos = 0;
111 return; 86 return;
112 } 87 }
113 88 else if (!--n)
89 break;
114 if (!refcnt (*i)) 90 else if (!refcnt (*i))
115 { 91 {
116 HT::iterator o = i++; 92 HT::iterator o = i++;
117 const char *s = *o; 93 const char *s = *o;
118 ht.erase (o); 94 ht.erase (o);
119 95

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines