diff options
author | Ludovic Courtès <ludo@gnu.org> | 2011-05-08 18:15:10 +0200 |
---|---|---|
committer | Ludovic Courtès <ludo@gnu.org> | 2011-05-08 18:20:42 +0200 |
commit | bc00e06c7ec5575f405bee4e12a062d4269f4eab (patch) | |
tree | fddbc297f93d3a3f37cd659210f18218e0d1f089 | |
parent | 0a6506781ac1082d370b8359199a9d20eda74057 (diff) | |
download | guile-bc00e06c7ec5575f405bee4e12a062d4269f4eab.tar.gz |
Optimize `vlist-fold-right'.
* module/ice-9/vlist.scm (vlist-fold-right): Avoid `vlist-reverse' and
instead `vlist-ref' individual elements. The result is about twice
faster. Thanks Andy for suggesting it indirectly. :-)
-rw-r--r-- | module/ice-9/vlist.scm | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/module/ice-9/vlist.scm b/module/ice-9/vlist.scm index 34c7c00c1..b554e1105 100644 --- a/module/ice-9/vlist.scm +++ b/module/ice-9/vlist.scm @@ -245,7 +245,14 @@ tail." (define (vlist-fold-right proc init vlist) "Fold over @var{vlist}, calling @var{proc} for each element, starting from the last element." - (vlist-fold proc init (vlist-reverse vlist))) + (define len (vlist-length vlist)) + + (let loop ((index (1- len)) + (result init)) + (if (< index 0) + result + (loop (1- index) + (proc (vlist-ref vlist index) result))))) (define (vlist-reverse vlist) "Return a new @var{vlist} whose content are those of @var{vlist} in reverse |