Skip to content

Commit 6e628c4

Browse files
committed
py: Replace naive and teribble hash function with djb2.
1 parent ffb5cfc commit 6e628c4

2 files changed

Lines changed: 7 additions & 5 deletions

File tree

py/makeqstrdata.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818

1919
# this must match the equivalent function in qstr.c
2020
def compute_hash(qstr):
21-
hash = 0
21+
hash = 5381
2222
for char in qstr:
23-
hash += ord(char)
23+
hash = (hash * 33) ^ ord(char)
2424
return hash & 0xffff
2525

2626
def do_work(infiles):

py/qstr.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// A qstr is an index into the qstr pool.
1919
// The data for a qstr contains (hash, length, data).
2020
// For now we use very simple encoding, just to get the framework correct:
21-
// - hash is 2 bytes (simply the sum of data bytes)
21+
// - hash is 2 bytes (see function below)
2222
// - length is 2 bytes
2323
// - data follows
2424
// - \0 terminated (for now, so they can be printed using printf)
@@ -28,10 +28,12 @@
2828
#define Q_GET_LENGTH(q) ((q)[2] | ((q)[3] << 8))
2929
#define Q_GET_DATA(q) ((q) + 4)
3030

31+
// this must match the equivalent function in makeqstrdata.py
3132
machine_uint_t qstr_compute_hash(const byte *data, uint len) {
32-
machine_uint_t hash = 0;
33+
// djb2 algorithm; see http://www.cse.yorku.ca/~oz/hash.html
34+
machine_uint_t hash = 5381;
3335
for (const byte *top = data + len; data < top; data++) {
34-
hash += *data;
36+
hash = ((hash << 5) + hash) ^ (*data); // hash * 33 ^ data
3537
}
3638
return hash & 0xffff;
3739
}

0 commit comments

Comments
 (0)