From 20379a264c03725329ce7b5788675315ef0bfecd Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Mon, 30 Apr 2018 13:59:22 -0400 Subject: [PATCH] Issue #249 - Improve hash table performance Hardcode bound such that it is not a bignum (reducing bignum allocations). Replace symbol hash with a more efficient algorithm, since symbols are uniquely identified by memory location as well as name. --- srfi/69.sld | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/srfi/69.sld b/srfi/69.sld index 8463001e..77d0d320 100644 --- a/srfi/69.sld +++ b/srfi/69.sld @@ -44,7 +44,9 @@ ;(scheme cyclone util) ) (begin -(define *default-bound* (- (expt 2 29) 3)) + +;; Increased to (2^30) - 1, hardcode to ensure fixnum +(define *default-bound* 1073741823) ;;(- (expt 2 29) 3)) (define (%string-hash s ch-conv bound) (let ((hash 31) @@ -63,15 +65,21 @@ (let ((bound (if (null? maybe-bound) *default-bound* (car maybe-bound)))) (%string-hash s char-downcase bound))) -(define (symbol-hash s . maybe-bound) - (let ((bound (if (null? maybe-bound) *default-bound* (car maybe-bound)))) - (%string-hash (symbol->string s) (lambda (x) x) bound))) +;; Symbols are unique by memory location, so replace old string comparison +(define-c symbol-hash + "(void *data, int argc, closure _, object k, object sym)" + " return_closcall1(data, k, obj_int2obj(((long)sym) & 0x7FFFFFFF)); " + "(void *data, object ptr, object sym)" + " return obj_int2obj(((long)sym) & 0x7FFFFFFF); ") (define (hash obj . maybe-bound) (let ((bound (if (null? maybe-bound) *default-bound* (car maybe-bound)))) (cond ((integer? obj) (modulo obj bound)) ((string? obj) (string-hash obj bound)) - ((symbol? obj) (symbol-hash obj bound)) + ((symbol? obj) + ;(symbol-hash obj bound) + (modulo (symbol-hash obj) bound) + ) ((real? obj) (modulo (+ (numerator obj) (denominator obj)) bound)) ((number? obj) (modulo (+ (hash (real-part obj)) (* 3 (hash (imag-part obj))))