Browse code

few comments on hash distribution added

Jiri Kuthan authored on 29/08/2002 18:22:49
Showing 1 changed files
... ...
@@ -111,15 +111,24 @@ int new_hash( str call_id, str cseq_nr )
111 111
 		//hash_code+=crc_32_tab[(cseq_nr.s[i]+hash_code)%243];
112 112
 		hash_code+=ccitt_tab[*(cs+i)+123];
113 113
 
114
+	/* hash_code conditioning */
114 115
 #ifdef _BUG
116
+	/* not flat ... % 111b has shorter period than
117
+       & 111b by one and results in different distribution;
118
+	   ( 7 % 7 == 0, 7 %7 == 1 )
119
+ 	   % is used as a part of the hash function too, not only
120
+	   for rounding; & is not flat; whoever comes up with
121
+	   a nicer flat hash function which does not take
122
+	   costly division is welcome; feel free to verify
123
+	   distribution using hashtest()
124
+    */
115 125
 	hash_code &= (TABLE_ENTRIES-1); /* TABLE_ENTRIES = 2^k */
116 126
 #endif
117
-	/* hash_code conditioning */
118 127
 	hash_code=hash_code%(TABLE_ENTRIES-1)+1;
119 128
    	return hash_code;
120 129
 }
121 130
 
122
-void hashtest_cycle( int hits[TABLE_ENTRIES], char *ip )
131
+void hashtest_cycle( int hits[TABLE_ENTRIES+5], char *ip )
123 132
 {
124 133
 	long int i,j,k, l;
125 134
 	int  hashv;
... ...
@@ -151,20 +160,23 @@ int init_hash()
151 151
 			1<<10);
152 152
 		LOG(L_WARN, "WARNING: use of %d entries may lead "
153 153
 			"to unflat distribution\n", TABLE_ENTRIES );
154
+	} else {
155
+		DBG("DEBUG: hash function initialized with optimum table size\n");
154 156
 	}
155 157
 	return 1;
156 158
 }
157 159
 
158 160
 void hashtest()
159 161
 {
160
-	int hits[TABLE_ENTRIES];
162
+	int hits[TABLE_ENTRIES+5];
161 163
 	int i;
162
-	
164
+
165
+	init_hash();	
163 166
 	memset( hits, 0, sizeof hits );
164 167
 	hashtest_cycle( hits, "192.168.99.100" );
165 168
 	hashtest_cycle( hits, "172.168.99.100" );
166 169
 	hashtest_cycle( hits, "142.168.99.100" );
167
-	for (i=0; i<TABLE_ENTRIES; i++)
170
+	for (i=0; i<TABLE_ENTRIES+5; i++)
168 171
 		printf("[%d. %d]\n", i, hits[i] );
169 172
 	exit(0);
170 173
 }