unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Stefan Israelsson Tampe <stefan.itampe@gmail.com>
To: guile-devel <guile-devel@gnu.org>
Subject: hashtable higher order interface
Date: Thu, 7 Apr 2022 22:50:37 +0200	[thread overview]
Message-ID: <CAGua6m0YdCo4AaDzvRsxFzOntKy0AX9gM4wOeDmpEpMXEg9gOQ@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 2109 bytes --]

;; First of some benchmarking to show of some code
(define n 10000000)
(define (test)
  (with-stis-hash ((h ref))
    (let lp ((i 0) (s 0))
      (if (< i n)
          (lp (+ i 1) (+ s (ref h (modulo i 1000))))
           s))))

(define (test2)
  (let lp ((i 0) (s 0))
    (if (< i n)
        (lp (+ i 1) (+ s (hashq-ref H (modulo i 1000))))
         s)))

scheme@(guile-user)> ,time (test)
$11 = 4995000000
;; 0.268354s real time, 0.268264s run time.  0.000000s spent in GC.
scheme@(guile-user)> ,time (test2)
$12 = 4995000000
;; 0.448963s real time, 0.448837s run time.  0.000000s spent in GC.

Nice eh!
------------------------------------------------
API
------------------------------------------------
;; Load the module,
(use-modules (ice-9 stis-hash))

;; Anyhow to create a stis hash table you need to use,
(define m (make-stis-hash-table 'hashq 10000 #:name 'test))

;; pre defined types are 'hashq 'hashv and 'hash

;; we can make a new type by
(define myhash (lambda (x) (logxor (hashq x 10) (hashv x 10))))
(make-stis-hash-type 'my  myhash equal?)

;; and use it by
(define m (make-stis-hash-table 'my 10000 #:name 'test))

;; We have
stis-hash-ref
stis-hash-set!
stis-hash-remove!

;; which is similar to the guile interface. But in order to
;; be fast us with-stis-hash like
(with-stis-hash ((h ref        )) code ...)
(with-stis-hash ((h ref set    )) code ...)
(with-stis-hash ((h ref set rem)) code ...)

;; we can use _ to indicate that this part is not used,
;; the the interface is the same as other hash accessors
;; but the hash-table is implicit and remeved as argument

;; Finally we can loop using functional constructions like
stis-hash-for-each
stis-hash-fold
stis-hash-map

;; which are much much faster than guile vanilla hashes and
;; uses only pure scheme

This uses a modified guile with v, instructions for hash-ref parts and hash
calc functions. In that guile branch I also have the one shot continuation
code as well.

Refereces:
https://gitlab.com/tampe/guile-persist/-/blob/master/ice-9/stis-hasch.scm
https://gitlab.com/tampe/guile-persist/-/blob/master/src/hash/work.c

[-- Attachment #2: Type: text/html, Size: 5304 bytes --]

                 reply	other threads:[~2022-04-07 20:50 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAGua6m0YdCo4AaDzvRsxFzOntKy0AX9gM4wOeDmpEpMXEg9gOQ@mail.gmail.com \
    --to=stefan.itampe@gmail.com \
    --cc=guile-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).