From bc00e06c7ec5575f405bee4e12a062d4269f4eab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludovic=20Court=C3=A8s?= Date: Sun, 8 May 2011 18:15:10 +0200 Subject: [PATCH] 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. :-) --- module/ice-9/vlist.scm | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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