unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Stefan Israelsson Tampe <stefan.itampe@gmail.com>
To: guile-devel@gnu.org
Subject: Better generator and iterator support in guile w.r.t. speed
Date: Tue, 05 Mar 2013 17:57:36 +0100	[thread overview]
Message-ID: <4559439.WT9qmlgVD5@warperdoze> (raw)

Hi guilers!

Inspired by wingo's recent blogage on wingolog in combination with my
portings of racket and cl loop frameworks, I did some thinking about 
generators. Especially inlining.

1. Can we make stubs like this inlinable
(iterate (generator g (iterate (for x in l-i-s-t)
                               (when (even? x) (yield (f x)))))
	 (collect (+ (next g) (next g))))

A generator would be somthing like
(define-syntax-rule (generator _thunk)
  (lambda ()
    (let ((thunk (lambda () (_thunk) (finish))))
      (call-with-prompt tag
	 thunk
	 (lambda (k . l)
	   (set! thunk k)
	   (apply values l))))))

(define-syntax-rules (yield x)
  (abort-to-prompt tag x))

With this kind of generator, as wingo says, we will get what we want but 
slowly. Might matter sometimes and not somtimes. What we would have 
liked 
here is some way of noticing that the liveness of "tag" is somewhat 
lexical
and cannot be hidden in f. With that information a compiler would be 
able to
inline code quite efficiently and make extensive use of gotos. How?

Let's see how finish is working
(define-syntax-parameter finish)
(define-syntax-rule (iterate c ...)
  (syntax-parametrize ((finish (syntax-rules () 
				 ((_ x) (apport-to-prompt tag2 x)))))
    (call-with-prompt
        (lambda () loop  code)
	(lambda _  final code))))

This whole setup would then be identified to something like

(seqence
  loop: 
  (with-frame g (generator thunk)
      code ...)
  finish:
  finish-code)

To make the compilation to glil simple we mean by with-frame that
we can setup a stack area where we can inline the thunk code and 
then a call to g like (next g) == (g) we can identify that
1. if (g) returns it will be from the abort clause in the prompt
because the next part will jump to the finish part in the section
which is below the generator frame and then it's possible to mark that
stack space void. 

2 The usage of the continuation is that we overwrite the 
same stack space with continuos versions of k's e.g. this is a 
characterizatin that we can keep the stack as a storage for this 
continuation. 

3 with-frame is not recursive in g e.g. g is not visible inside thunk


4 So we should be able to make this work with intelligent usage of 
gotos. No, (f x) will be a problem because that we cannot know how
much stack-space that function is needing.

5 What is interesting with the rtl format is that function applications 
are setuped at the end of the stack frame. This means that imersion of
a generator is pretty simple using gotos and for rtl we would be able to
manage quite general iterators and inline them with quite simple means.
 
My conclusion is that for wip-rtl we should really start working on a
prompt ideom that is lexical. But is it general enough. probably, but if 
you If you consider the main use case to be inlinable code, 
then you would need to consider the following tree-il extension

(lexical-rec-prompts 
   (name thunk abort-lam) 
   ...)
	 
where name ... is visible inside the thunk's and abort's sections. 
Of cause not all versions of this code will be possible to manage 
without long-jumps and old pompt like features.
(to note, CL's tagbody seams to be a very similar notion)

Am I out in the blue or do you agree?

Cheers Stefan.



             reply	other threads:[~2013-03-05 16:57 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-05 16:57 Stefan Israelsson Tampe [this message]
2013-03-06 18:44 ` Better generator and iterator support in guile w.r.t. speed Stefan Israelsson Tampe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4559439.WT9qmlgVD5@warperdoze \
    --to=stefan.itampe@gmail.com \
    --cc=guile-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).