[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Good way to code the equivalent to this?

From: Kon Lovett
Subject: Re: [Chicken-users] Good way to code the equivalent to this?
Date: Wed, 24 Sep 2008 13:30:33 -0700

On Aug 25, 2008, at 12:50 PM, Tobia Conforto wrote:

Matt Welland wrote:
the tactic of loading lots of data into a hierarchy of hash arrays and then extracting the needed pieces in a myriad of ways, sometimes on the fly in a meeting with management nervously looking on :-) has been tremendously useful for me. I for one am hoping that there are faster hash tables in the future of chicken.

Until there are, here's how you can have the best of both worlds, especially considering: 1. the use you seem to make of hash tables (mainly setting new keys and referencing them, rarely or never deleting them)
2. what has been said of alists vs. hash tables for small data sets
3. what has been said of alist->hash-table

Just use your own hash table-like structure, that starts as an alist and only becomes a proper hash table when it grows over a certain size, tuneable with a parameter.

Something like this should go a long way in letting you keep your coding style and have good performance:

(define smart-hash-threshold (make-parameter 20))

(define (make-smart-hash)
 (cons (list)
       (void))) ;cdr currently unused

(define (smart-hash-ref table key)
 (if (list? (car table))
     (alist-ref key (car table))
     (hash-table-ref (car table) key)))

(define (smart-hash-set! table key value)
 (if (list? (car table))
       (set-car! table (alist-update! key value (car table)))
       (if (> (length (car table)) (smart-hash-threshold))
           (set-car! table (alist->hash-table (car table)))))
     (hash-table-set! (car table) key value)))


The "lookup-table" egg operates in a similar fashion.

Chicken-users mailing list

Best Wishes,

reply via email to

[Prev in Thread] Current Thread [Next in Thread]