Browse code

- usrloc hash fix (performace): switched to a numeric string optimized hash function (which also has very good distribution for strings) - added the new hash to core's hashes.h & changes hashes.h license to openbsd style (so that anybody can use the hash functions without being forced to gpl the code)

Andrei Pelinescu-Onciul authored on 24/11/2006 17:20:08
Showing 2 changed files
... ...
@@ -67,7 +67,7 @@ MAIN_NAME=ser
67 67
 VERSION = 0
68 68
 PATCHLEVEL = 10
69 69
 SUBLEVEL =   99
70
-EXTRAVERSION = -dev59
70
+EXTRAVERSION = -dev60
71 71
 
72 72
 SER_VER = $(shell expr $(VERSION) \* 1000000 + $(PATCHLEVEL) \* 1000 + \
73 73
 			$(SUBLEVEL) )
... ...
@@ -3,31 +3,23 @@
3 3
  *
4 4
  * Copyright (C) 2006 iptelorg GmbH 
5 5
  *
6
- * This file is part of ser, a free SIP server.
6
+ * Permission to use, copy, modify, and distribute this software for any
7
+ * purpose with or without fee is hereby granted, provided that the above
8
+ * copyright notice and this permission notice appear in all copies.
7 9
  *
8
- * ser is free software; you can redistribute it and/or modify
9
- * it under the terms of the GNU General Public License as published by
10
- * the Free Software Foundation; either version 2 of the License, or
11
- * (at your option) any later version
12
- *
13
- * For a license to use the ser software under conditions
14
- * other than those described here, or to purchase support for this
15
- * software, please contact iptel.org by e-mail at the following addresses:
16
- *    info@iptel.org
17
- *
18
- * ser is distributed in the hope that it will be useful,
19
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
- * GNU General Public License for more details.
22
- *
23
- * You should have received a copy of the GNU General Public License 
24
- * along with this program; if not, write to the Free Software 
25
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
10
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 17
  */
27 18
 /*
28 19
  * History:
29 20
  * --------
30 21
  *  2006-02-02  created by andrei
22
+ *  2006-11-24  added numeric string optimized hash function (andrei)
31 23
  */
32 24
 
33 25
 
... ...
@@ -98,6 +90,58 @@ inline static unsigned int get_hash1_raw(char* s, int len)
98 98
 
99 99
 
100 100
 
101
+/* a little slower than hash_* , but better distribution for 
102
+ * numbers and about the same for strings */
103
+#define hash_update_str2(s, end, p, v, h) \
104
+	do{ \
105
+		for ((p)=(s); (p)<=((end)-4); (p)+=4){ \
106
+			(v)=(*(p)*16777213)+((p)[1]*65537)+((p)[2]*257)+(p)[3]; \
107
+			(h)=16777259*(h)+((v)^((v)<<17)); \
108
+		} \
109
+		(v)=0; \
110
+		for (;(p)<(end); (p)++){ (v)*=251; (v)+=*(p);} \
111
+		(h)=16777259*(h)+((v)^((v)<<17)); \
112
+	}while(0)
113
+
114
+/* internal use: call it to adjust the h from hash_update_str */
115
+#define hash_finish2(h) (((h)+((h)>>7))+(((h)>>13)+((h)>>23)))
116
+
117
+
118
+
119
+/* a little slower than get_hash1_raw() , but better distribution for 
120
+ * numbers and about the same for strings */
121
+inline static unsigned int get_hash1_raw2(char* s, int len)
122
+{
123
+	char* p;
124
+	register unsigned v;
125
+	register unsigned h;
126
+	
127
+	h=0;
128
+	
129
+	hash_update_str2(s, s+len, p, v, h);
130
+	return hash_finish2(h);
131
+}
132
+
133
+
134
+
135
+/* "raw" 2 strings hash optimized for numeric strings (see above)
136
+ * returns an unsigned int (which you can use modulo table_size as hash value)
137
+ */
138
+inline static unsigned int get_hash2_raw2(str* key1, str* key2)
139
+{
140
+	char* p;
141
+	register unsigned v;
142
+	register unsigned h;
143
+	
144
+	h=0;
145
+	
146
+	hash_update_str2(key1->s, key1->s+key1->len, p, v, h);
147
+	hash_update_str2(key2->s, key2->s+key2->len, p, v, h);
148
+	return hash_finish(h);
149
+}
150
+
151
+
152
+
101 153
 /* generic, simple str keyed hash */
102 154
 
103 155
 struct str_hash_entry{