>From b7d414a376e85c55e0187abaa8d228c2c75e574f Mon Sep 17 00:00:00 2001 From: Peter Bex Date: Sun, 4 Mar 2012 18:32:08 +0100 Subject: [PATCH] Don't allow random numbers for hash tables to have all zeroes as lower bits; especially for small hashes this produces values that are more predictable than neccessary. Also hide hash-default-randomization in srfi-69 --- eval.scm | 4 +++- runtime.c | 2 +- srfi-69.scm | 9 +++++---- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/eval.scm b/eval.scm index 779c230..e049895 100644 --- a/eval.scm +++ b/eval.scm @@ -45,6 +45,8 @@ #ifndef C_BINARY_VERSION # define C_BINARY_VERSION 0 #endif + +#define C_rnd_fix() (C_fix(rand())) <# (include "common-declarations.scm") @@ -111,7 +113,7 @@ (let ([cache-s #f] [cache-h #f] ;; NOTE: All low-level hash tables share the same randomization factor - [rand (##core#inline "C_random_fixnum" most-positive-fixnum)] ) + [rand (##core#inline "C_rnd_fix")] ) (lambda (s n) (if (eq? s cache-s) (##core#inline "C_fixnum_modulo" cache-h n) diff --git a/runtime.c b/runtime.c index 41a2f9d..5f10812 100644 --- a/runtime.c +++ b/runtime.c @@ -886,7 +886,7 @@ C_regparm C_SYMBOL_TABLE *C_new_symbol_table(char *name, unsigned int size) stp->name = name; stp->size = size; stp->next = symbol_table_list; - stp->rand = C_unfix(C_random_fixnum(C_fix(size))); + stp->rand = rand(); if((stp->table = (C_word *)C_malloc(size * sizeof(C_word))) == NULL) return NULL; diff --git a/srfi-69.scm b/srfi-69.scm index 4b22a3c..2ea384f 100644 --- a/srfi-69.scm +++ b/srfi-69.scm @@ -30,11 +30,13 @@ *eq?-hash *eqv?-hash *equal?-hash *make-hash-table *hash-table-copy *hash-table-merge! *hash-table-update!/default - *hash-table-for-each *hash-table-fold + *hash-table-for-each *hash-table-fold hash-default-randomization hash-table-canonical-length hash-table-rehash! hash-table-check-resize! ) ) (include "common-declarations.scm") +(foreign-declare "#define C_rnd_fix() (C_fix(rand()))") + (register-feature! 'srfi-69) @@ -105,8 +107,7 @@ (define-constant unknown-immediate-hash-value 262) (define-constant hash-default-bound 536870912) -(define hash-default-randomization - (##core#inline "C_random_fixnum" hash-default-bound)) +(define hash-default-randomization (##core#inline "C_rnd_fix")) ;; Force Hash to Bounded Fixnum: @@ -414,7 +415,7 @@ string-hash string-hash-ci number-hash)) ;; Don't add unneccessary bounds checks for procedures known to be ;; well-behaved (these are not user-*created* functions) - (let ((randomization (##core#inline "C_random_fixnum" most-positive-fixnum))) + (let ((randomization (##core#inline "C_rnd_fix"))) (if (memq user-function (list string-hash string-hash-ci)) ;; String functions have differing signatures; treat them specially (lambda (object bound) -- 1.7.9.1