Hello hackers,
here's a small patch that makes `string->list' tail recursive, we gain
a small speed up and
one less word allocated for every iteration.
The patch has been made against the chicken-5 branch and should apply
cleanly to the
master branch too.

Cheers,
LemonBoy
From 1598f73a5c92a8084f434f48d5fdffac4b358718 Mon Sep 17 00:00:00 2001
From: LemonBoy <[email protected]>
Date: Sat, 25 Mar 2017 00:04:28 +0100
Subject: [PATCH] Rewrite string->list to be tail-recursive.

This way we allocate one less word per iteration.
A run of chicken-benchmarks shows a (very) small speed-up.
---
 library.scm | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)

diff --git a/library.scm b/library.scm
index 284b6530..8a654a0d 100644
--- a/library.scm
+++ b/library.scm
@@ -639,15 +639,14 @@ EOF
 	 (##sys#check-char c 'make-string)
 	 c ) ) ) )
 
-(define string->list
-  (lambda (s)
-    (##sys#check-string s 'string->list)
-    (let ((len (##core#inline "C_block_size" s)))
-      (let loop ((i 0))
-	(if (fx>= i len)
-	    '()
-	    (cons (##core#inline "C_subchar" s i)
-		  (loop (fx+ i 1)) ) ) ) ) ) )
+(define (string->list s)
+  (##sys#check-string s 'string->list)
+  (let ((len (##sys#size s)))
+    (let loop ((i (fx- len 1)) (ls '()))
+      (if (fx< i 0)
+	  ls
+	  (loop (fx- i 1)
+		(cons (##core#inline "C_subchar" s i) ls))))))
 
 (define ##sys#string->list string->list)
 
-- 
2.12.1

_______________________________________________
Chicken-hackers mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/chicken-hackers

Reply via email to