From 68af3d922102d2e9ff6ba2e5820544dfa3469394 Mon Sep 17 00:00:00 2001 From: brain Date: Tue, 19 Sep 2006 12:50:40 +0000 Subject: ULTRA FAST HASH FUNCTION :p I went and looked how hash_fun.h in STL did it, to save a string copy :p (it's deceptively simple too) git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@5298 e03df62e-2008-0410-955e-edbf42e46eb7 --- src/hashcomp.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src/hashcomp.cpp') diff --git a/src/hashcomp.cpp b/src/hashcomp.cpp index 33dcea307..168bcbe0f 100644 --- a/src/hashcomp.cpp +++ b/src/hashcomp.cpp @@ -80,16 +80,16 @@ size_t nspace::hash::operator()(const insp_inaddr &a) const size_t nspace::hash::operator()(const string &s) const { - char a[s.length()]; - size_t t = 0; - static struct hash strhash; - - for (const char* x = s.c_str(); *x; x++) /* Faster to do it this way than */ - a[t++] = lowermap[(unsigned char)*x]; /* Seperate strlcpy and strlower */ - - a[t] = 0; - - return strhash(a); + /* XXX: NO DATA COPIES! :) + * The hash function here is practically + * a copy of the one in STL's hash_fun.h, + * only with *x replaced with lowermap[*x]. + * This avoids a copy to use hash + */ + unsigned long t = 0; + for (const char* x = s.c_str(); *x; x++) + t = 5 * t + lowermap[(unsigned char)*x]; + return size_t(t); } bool irc::StrHashComp::operator()(const std::string& s1, const std::string& s2) const -- cgit v1.2.3