Changeset 39748 in project


Ignore:
Timestamp:
03/21/21 17:52:11 (7 weeks ago)
Author:
Mario Domenech Goulart
Message:

release/5/srfi-69/trunk: cache current min and max length based on load and *actually resize*

This is a patch by Peter Bex submitted to chicken-hackers. See
https://lists.nongnu.org/archive/html/chicken-hackers/2021-03/msg00010.html

File:
1 edited

Legend:

Unmodified
Added
Removed
  • release/5/srfi-69/trunk/srfi-69.scm

    r37833 r39748  
    497497    (lambda (test hash len min-load max-load weak-keys weak-values initial
    498498                  #!optional (vec (make-vector len '())))
    499       (let ((ht (##sys#make-structure 'hash-table
    500                  vec 0 test hash min-load max-load #f #f initial #f)))
     499      (let* ((min-load-len (inexact->exact (floor (* len min-load)))) ;; Cached values to speed up hash-table-check-resize!
     500             (max-load-len (inexact->exact (floor (* len max-load))))
     501             (ht (##sys#make-structure 'hash-table
     502                                       vec 0 test hash min-load max-load #f #f initial #f min-load-len max-load-len)))
    501503        (##sys#setslot ht 10 (*make-hash-function hash))
    502504        ht) ) ) )
     
    696698
    697699(define (hash-table-resize! ht vec len)
    698   (let* ([deslen (fxmin hash-table-max-length (fx* len hash-table-new-length-factor))]
    699          [newlen (hash-table-canonical-length hash-table-prime-lengths deslen)]
    700          [vec2 (make-vector newlen '())] )
     700  (let* ((deslen (fxmin hash-table-max-length (fx* len hash-table-new-length-factor)))
     701         (newlen (hash-table-canonical-length hash-table-prime-lengths deslen))
     702         (min-load (##sys#slot ht 5))
     703         (new-min-load-len (inexact->exact (floor (* len min-load))))
     704         (max-load (##sys#slot ht 6))
     705         (new-max-load-len (inexact->exact (floor (* len max-load))))
     706         (vec2 (make-vector newlen '())) )
    701707    (hash-table-rehash! vec vec2 (##sys#slot ht 10))
    702     (##sys#setslot ht 1 vec2) ) )
     708    (##sys#setslot ht 1 vec2)
     709    (##sys#setslot ht 11 new-min-load-len)
     710    (##sys#setslot ht 12 new-max-load-len)) )
    703711
    704712;; hash-table-check-resize!:
    705713
    706714(define-inline (hash-table-check-resize! ht newsiz)
    707   (let ([vec (##sys#slot ht 1)]
    708         [min-load (##sys#slot ht 5)]
    709         [max-load (##sys#slot ht 6)] )
    710     (let ([len (##sys#size vec)] )
    711       (let ([min-load-len (inexact->exact (floor (* len min-load)))]
    712             [max-load-len (inexact->exact (floor (* len max-load)))] )
    713         (if (and (fx< len hash-table-max-length)
    714                  (fx<= min-load-len newsiz) (fx<= newsiz max-load-len))
    715           (hash-table-resize! ht vec len) ) ) ) ) )
     715  (let* ((vec (##sys#slot ht 1))
     716         (min-load-len (##sys#slot ht 11))
     717         (max-load-len (##sys#slot ht 12))
     718         (len (##sys#size vec)))
     719    (if (and (fx< len hash-table-max-length)
     720             (fx<= min-load-len newsiz) (fx<= newsiz max-load-len))
     721        (hash-table-resize! ht vec len) )   ) )
    716722
    717723;; hash-table-copy:
Note: See TracChangeset for help on using the changeset viewer.