xdiff/xutils.c(xdl_hash_record): factor out whitespace handling
Since in at least one use case, xdl_hash_record() takes over 15% of the CPU time, it makes sense to even micro-optimize it. For many cases, no whitespace special handling is needed, and in these cases we should not even bother to check for whitespace in _every_ iteration of the loop. Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>
Johannes Schindelin committed
Mar 20, 2007 at 04:05 UTC
824f782c3f8f2db8e884b0c555d21ed4cfd6fff2
1 file changed
+20
-2
xdiff/xutils.c
+20
-2
@@ -236,12 +236,13 @@ int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags)
236
return 0;
237
}
238
239
-unsigned long xdl_hash_record(char const **data, char const *top, long flags) {
239
+static unsigned long xdl_hash_record_with_whitespace(char const **data,
240
+ char const *top, long flags) {
241
unsigned long ha = 5381;
242
char const *ptr = *data;
243
244
for (; ptr < top && *ptr != '\n'; ptr++) {
244
- if (isspace(*ptr) && (flags & XDF_WHITESPACE_FLAGS)) {
245
+ if (isspace(*ptr)) {
246
const char *ptr2 = ptr;
247
while (ptr + 1 < top && isspace(ptr[1])
248
&& ptr[1] != '\n')
@@ -270,6 +271,23 @@ unsigned long xdl_hash_record(char const **data, char const *top, long flags) {
271
}
272
273
274
+unsigned long xdl_hash_record(char const **data, char const *top, long flags) {
275
+ unsigned long ha = 5381;
276
+ char const *ptr = *data;
277
+
278
+ if (flags & XDF_WHITESPACE_FLAGS)
279
+ return xdl_hash_record_with_whitespace(data, top, flags);
280
+
281
+ for (; ptr < top && *ptr != '\n'; ptr++) {
282
+ ha += (ha << 5);
283
+ ha ^= (unsigned long) *ptr;
284
+ }
285
+ *data = ptr < top ? ptr + 1: ptr;
286
+
287
+ return ha;
288
+}
289
+
290
+
291
unsigned int xdl_hashbits(unsigned int size) {
292
unsigned int val = 1, bits = 0;
293