Re: [Chicken-hackers] [PATCH] Slight promise efficiency and memory improvements

2012-11-05 Thread Moritz Heidkamp
Peter Bex  writes:
> Very clever!  I've tested and pushed this.

Cool, thank you very much!


> Are you actually using promises so much that you're running into
> performance issues with them, or was this just for fun?

I ran into memory leaks when trying to implement the lazy-seq egg based
on promises. And IIRC that was due to references that were kept in the
delay thunk's closure and thus weren't garbage collected. The
record-based implementation I came up with instead is what inspired me
to check whether the same could be applied to core. I'm now evaluating
whether using this promise implementation actually works for lazy-seq
and whether it yields any performance benefits. One drawback is that it
needs to support multi values whereas my hand-rolled version in lazy-seq
doesn't. I can post a follow-up here once I've tried it if you're
interested.

Moritz

___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] [PATCH] Slight promise efficiency and memory improvements

2012-11-05 Thread Peter Bex
On Mon, Nov 05, 2012 at 04:25:18PM +0100, Moritz Heidkamp wrote:
> Dear Chickeneers,
> 
> the attached patch changes the implementation of promises to be slightly
> more efficient when forcing a promise more than once (one procedure call
> less). Also, instead of keeping the promise result in the promise
> thunk's closure it is now kept in a slot of the promise structure. This
> allows for removing the reference to the thunk once the promise has been
> forced and thus saving some memory.

Very clever!  I've tested and pushed this.

Are you actually using promises so much that you're running into
performance issues with them, or was this just for fun?

Cheers,
Peter
-- 
http://sjamaan.ath.cx
--
"The process of preparing programs for a digital computer
 is especially attractive, not only because it can be economically
 and scientifically rewarding, but also because it can be an aesthetic
 experience much like composing poetry or music."
-- Donald Knuth

___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] [PATCH] Slight promise efficiency and memory improvements

2012-11-05 Thread Moritz Heidkamp
Quick follow-up: I posted the benchmark results of a different program,
of course, one with the sequence going up to 50 rather than
100. Sorry for the blunder! :-)

___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


[Chicken-hackers] [PATCH] Slight promise efficiency and memory improvements

2012-11-05 Thread Moritz Heidkamp
Dear Chickeneers,

the attached patch changes the implementation of promises to be slightly
more efficient when forcing a promise more than once (one procedure call
less). Also, instead of keeping the promise result in the promise
thunk's closure it is now kept in a slot of the promise structure. This
allows for removing the reference to the thunk once the promise has been
forced and thus saving some memory. I measured its effect by running the
following program with and without the patch:

  (define seq
(let next ((n 0))
  (cons n (delay (next (+ n 1))

  (define (run)
(print
 (time
  (let loop ((seq seq))
(if (< (car seq) 100)
(loop (force (cdr seq)))
(car seq)
(print (memory-statistics)))


  (run)
  (run)

The results of which are:

without patch:

  0.856s CPU time, 0.352s GC time (major), 50 mutations, 9/1539 GCs 
(major/minor)
  50
  #(268435456 230448536 1048576)
  0.034s CPU time, 0/213 GCs (major/minor)
  50
  #(268435456 230448536 1048576)

with the patch:

  0.506s CPU time, 0.233s GC time (major), 50 mutations, 11/1500 GCs 
(major/minor)
  50
  #(134217728 107357264 1048576)
  0.03s CPU time, 0/198 GCs (major/minor)
  50
  #(134217728 107357264 1048576)

Hope it's of interested and not fatally flawed!

Moritz

>From b42464bccb2852121148c726eb49b90566d4519e Mon Sep 17 00:00:00 2001
From: Moritz Heidkamp 
Date: Mon, 5 Nov 2012 16:14:36 +0100
Subject: [PATCH] Make promises slightly more efficient and less memory
 intensive

Instead of keeping the promise result in the promise thunk's closure
the implementation is changed to keep the result in a slot of the
promise structure. This allows for removing the reference to the thunk
once the promise has been forced and thus saving some memory.
---
 library.scm | 28 ++--
 1 file changed, 10 insertions(+), 18 deletions(-)

diff --git a/library.scm b/library.scm
index 438004a..1711b84 100644
--- a/library.scm
+++ b/library.scm
@@ -341,8 +341,15 @@ EOF
 
 (define (##sys#force promise)
   (if (##sys#structure? promise 'promise)
-  ((##sys#slot promise 1))
-  promise) )
+  (apply ##sys#values
+ (or (##sys#slot promise 2)
+ (let ((results (##sys#call-with-values (##sys#slot promise 1) (lambda xs xs
+   (or (##sys#slot promise 2)
+   (begin
+ (##sys#setslot promise 1 #f)
+ (##sys#setslot promise 2 results)
+ results)
+  promise))
 
 (define force ##sys#force)
 
@@ -4708,22 +4715,7 @@ EOF
 ;;; Promises:
 
 (define (##sys#make-promise proc)
-  (let ([result-ready #f]
-	[results #f] )
-(##sys#make-structure
- 'promise
- (lambda ()
-   (if result-ready
-	   (apply ##sys#values results)
-	   (##sys#call-with-values 
-	proc
-	(lambda xs
-	  (if result-ready
-		  (apply ##sys#values results)
-		  (begin
-		(set! result-ready #t)
-		(set! results xs)
-		(apply ##sys#values results) ) ) ) ) ) ) ) ) )
+  (##sys#make-structure 'promise proc #f))
 
 (define (promise? x)
   (##sys#structure? x 'promise) )
-- 
1.7.12

___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers