--- deliantra/server/common/shstr.C 2006/09/03 00:18:40 1.3 +++ deliantra/server/common/shstr.C 2006/09/03 09:00:05 1.5 @@ -9,6 +9,9 @@ #include "shstr.h" +// NOTE: even with lots of stuff loaded, we do not usually have >>20000 strings. +// maybe refcounting is just overhead? + struct hash { std::size_t operator ()(const char *s) const @@ -51,44 +54,80 @@ const char * shstr::find (const char *s) { + if (!s) + return s; + HT::iterator i = ht.find (s); return i != ht.end () - ? (char *)*i + ? *i : 0; } const char * shstr::intern (const char *s) { - HT::iterator i = ht.find (s); + if (!s) + return s; - if (i != ht.end ()) - return (char *)*i; + if (const char *found = find (s)) + { + ++refcnt (found); + return found; + } int len = strlen (s); - int *v = (int *)malloc (sizeof (int) * 2 + len + 1); - - v [0] = len; - v [1] = 0; + const char *v = (const char *)(2 + (int *)malloc (sizeof (int) * 2 + len + 1)); - v += 2; + length (v) = len; + refcnt (v) = 1; - memcpy (v, s, len + 1); + memcpy ((char *)v, s, len + 1); - ht.insert ((char *)v); + ht.insert (v); - return (char *)v; + return v; } -// TODO: periodically test refcounts == 0 for a few strings (e.g. one hash bucket, -// exploiting the fatc that iterators stay valid for unordered_set). +// periodically test refcounts == 0 for a few strings +// this is the ONLY thing that erases stuff from ht. keep it that way. void shstr::gc () { + static const char *curpos; + + HT::iterator i = curpos ? ht.find (curpos) : ht.begin (); + + if (i == ht.end ()) + i = ht.begin (); + + // go through all strings roughly once every 4 minutes + for (int n = ht.size () / 256 + 16; --n; ) + { + if (i == ht.end ()) + { + curpos = 0; + return; + } + + if (!refcnt (*i)) + { + HT::iterator o = i++; + const char *s = *o; + ht.erase (o); + + //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); + free (-2 + (int *)s); + } + else + ++i; + } + + curpos = *i; } +//TODO: this should of course not be here /* buf_overflow() - we don't want to exceed the buffer size of * buf1 by adding on buf2! Returns true if overflow will occur. */