From 045b70ff20b5428965687e0fb16de5c3fea21a55 Mon Sep 17 00:00:00 2001 From: Robert Spalek Date: Sat, 25 May 2002 13:59:34 +0000 Subject: [PATCH] - added str_hash.[ch] for fast evaluation of str_len() and str_hash() - added a tester/benchmark str-test.c, it is not compiled by default --- lib/Makefile | 3 +- lib/str-test.c | 113 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/str_hash.c | 93 ++++++++++++++++++++++++++++++++++++++++ lib/str_hash.h | 14 ++++++ 4 files changed, 222 insertions(+), 1 deletion(-) create mode 100644 lib/str-test.c create mode 100644 lib/str_hash.c create mode 100644 lib/str_hash.h diff --git a/lib/Makefile b/lib/Makefile index 01f79634..2d96bb1d 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -8,7 +8,7 @@ SHLIB_OBJS=alloc.o alloc_str.o ctmatch.o db.o fastbuf.o fb-file.o fb-mem.o lists prime.o random.o realloc.o regex.o timer.o url.o wildmatch.o \ wordsplit.o str_ctype.o str_upper.o bucket.o conf.o object.o sorter.o \ finger.o proctitle.o ipaccess.o profile.o bitsig.o randomkey.o \ - hash-string.o hash-istring.o custom.o base224.o + hash-string.o hash-istring.o custom.o base224.o str_hash.o obj/lib/libsh.a: $(addprefix obj/lib/,$(SHLIB_OBJS)) @@ -20,3 +20,4 @@ obj/lib/sort-test: obj/lib/sort-test.o obj/lib/libsh.a obj/lib/lfs-test: obj/lib/lfs-test.o obj/lib/libsh.a obj/lib/regex-test: obj/lib/regex-test.o obj/lib/libsh.a obj/lib/hash-test: obj/lib/hash-test.o obj/lib/libsh.a +obj/lib/str-test: obj/lib/str-test.o obj/lib/libsh.a diff --git a/lib/str-test.c b/lib/str-test.c new file mode 100644 index 00000000..adac0fb8 --- /dev/null +++ b/lib/str-test.c @@ -0,0 +1,113 @@ +/* + * Checking the correctness of str_len() and str_hash() and proving, that + * it is faster than the classical version ;-) + */ + +#include "lib/str_hash.h" + +#include +#include +#include +#include + +/* It will be divided by (10 + strlen()). */ +#define TEST_TIME 1000000 + +static void +random_string(char *str, int len) +{ + int i; + for (i=0; i= 0; i++) + { + char str[lengths[i] + 1]; + uns count = TEST_TIME / (lengths[i] + 10); + uns el1 = 0, el2 = 0, elh = 0; + uns tot1 = 0, tot2 = 0, hash = 0; + uns j; + for (j=0; j%d\n", i, i*3-2, i*i, hash_modify(4587, i*3-2, i*i)); + printf("test1: %d\n", hash_modify(lengths[5], 345, i)); +*/ + return 0; +} diff --git a/lib/str_hash.c b/lib/str_hash.c new file mode 100644 index 00000000..44310158 --- /dev/null +++ b/lib/str_hash.c @@ -0,0 +1,93 @@ +/* + * Hyper-super-meta-alt-control-shift extra fast str_len() and str_hash() + * routines + * + * It is always at least as fast as the classical strlen() routine and for + * strings longer than 100 characters, it is substantially faster. + * + * (c) 2002, Robert Spalek + */ + +#include "lib/lib.h" +#include "lib/str_hash.h" + +/* The number of bits the hash (in the function str_hash()) is rotated after + * every pass by. It should be prime with the word size. */ +#define SHIFT_BITS 5 + +/* A bit-mask which clears higher bytes than a given threshold. */ +static uns mask_higher_bits[sizeof(uns)]; + +static void CONSTRUCTOR +str_hash_init(void) +{ + uns i, j; + char *str; + for (i=0; i + */ + +#include "lib/lib.h" + +/* An equivalent of the Intel's rol instruction. */ +#define ROL(x, bits) (((x) << (bits)) | ((x) >> (sizeof(uns)*8 - (bits)))) + +uns str_len(const char *str) __attribute__((const)); +uns str_hash(const char *str) __attribute__((const)); -- 2.39.2