Hello Guilers! Göran Weinholt wrote a pure-Scheme (R6RS) implementation of common cryptographic hash functions:
https://github.com/weinholt/hashing I thought this would make a useful benchmark (and probably favorable to JIT because it’s one hot loop), so here goes (computing the SHA256 hash of ‘guile-2.2.6.tar.xz’ with the code below, compiled with -O2, Guile 2.2.6 vs. 2.9.8): --8<---------------cut here---------------start------------->8--- ludo@ribbon ~/src/guix$ ./pre-inst-env guix environment --pure --ad-hoc guile guile-hashing -- guile ~/tmp/sha256.scm ;;; (hash "b33576331465a60b003573541bf3b1c205936a16c407bc69f8419a527bf5c988") clock utime stime cutime cstime gctime 135.07 164.13 0.52 0.00 0.00 42.14 ludo@ribbon ~/src/guix$ ./pre-inst-env guix environment --pure --ad-hoc guile-next guile3.0-hashing -- guile ~/tmp/sha256.scm ;;; (hash "b33576331465a60b003573541bf3b1c205936a16c407bc69f8419a527bf5c988") clock utime stime cutime cstime gctime 65.17 89.75 0.45 0.00 0.00 35.63 --8<---------------cut here---------------end--------------->8--- Guile 3 is twice as fast! No difference if we force ‘-O3’. Still far from the libgcrypt implementation in C + asm, but hey! --8<---------------cut here---------------start------------->8--- ludo@ribbon ~/src/guix$ time guix hash -f hex "/gnu/store/zfp7d4wr5hbl5lrnzs8c15bsc3ygq74g-guile-2.2.6.tar.xz" b33576331465a60b003573541bf3b1c205936a16c407bc69f8419a527bf5c988 real 0m0.097s user 0m0.093s sys 0m0.024s --8<---------------cut here---------------end--------------->8--- To be continued… Ludo’.
(use-modules (hashing sha-2) (rnrs bytevectors) (ice-9 binary-ports) (ice-9 match) (ice-9 time)) (define (port-sha256 port) ;; Return the SHA256 of the data read from PORT. (define bv (make-bytevector 65536)) (define hash (make-sha-256)) (let loop () (match (get-bytevector-n! port bv 0 (bytevector-length bv)) ((? eof-object?) (sha-256-finish! hash) hash) (n (sha-256-update! hash bv 0 n) (loop))))) (define (file-sha256 file) ;; Return the SHA256 of FILE. (call-with-input-file file port-sha256)) (time (pk 'hash (sha-256->string (file-sha256 "/gnu/store/zfp7d4wr5hbl5lrnzs8c15bsc3ygq74g-guile-2.2.6.tar.xz"))))