unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* Performance impact of top level definitions
@ 2018-05-09  5:19 Brian
  2018-05-15  2:55 ` Mark H Weaver
  0 siblings, 1 reply; 3+ messages in thread
From: Brian @ 2018-05-09  5:19 UTC (permalink / raw)
  To: guile-devel

Hello,

Today I found that top level defines have a significant performance
impact on Guile (2.2.3). The following program takes about 108 seconds
to complete on my ThinkPad (an i5-5200U with Arch Linux):

(define node cons)
(define node-left car)
(define node-right cdr)

(define (make d)
  (if (= d 0)
      (node #f #f)
      (let ([d2 (1- d)])
        (node (make d2) (make d2)))))

(define (check t)
  (if (node-left t)
      (+ 1 (check (node-left t)) (check (node-right t)))
      1))

(define (main n)
  (define min-depth 4)
  (define max-depth (max (+ min-depth 2) n))
  (define stretch-depth (1+ max-depth))
  (format #t "stretch tree of depth ~a\t check: ~a\n" stretch-depth
(check (make stretch-depth)))
  (let ([long-lived-tree (make max-depth)])
    (do ([d 4 (+ d 2)]) ([not (< d (1+ max-depth))])
      (let ([iterations (ash 1 (+ (- max-depth d) min-depth))])
        (format #t "~a\t trees of depth ~a\t check: ~a\n"
                iterations
                d
                (let sum ([i iterations] [n 0])
                  (if (zero? i)
                      n
                      (sum (1- i) (+ n (check (make d)))))))))
    (format #t "long lived tree of depth ~a\t check: ~a\n"
            max-depth
            (check long-lived-tree))))

(main 21)


By simply wrapping that code in a lambda the program finished in about
47 seconds. Using lets instead of defines is equally effective.

I was quite surprised because I initially thought some optimization
would just substitute those useless nodes symbols away, but it seems
like that's not the case...

Cheers!






^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Performance impact of top level definitions
  2018-05-09  5:19 Performance impact of top level definitions Brian
@ 2018-05-15  2:55 ` Mark H Weaver
  2018-05-15 16:55   ` Brian
  0 siblings, 1 reply; 3+ messages in thread
From: Mark H Weaver @ 2018-05-15  2:55 UTC (permalink / raw)
  To: Brian; +Cc: guile-devel

Hi Brian,

Brian <gomesbascoy@gmail.com> writes:

> Today I found that top level defines have a significant performance
> impact on Guile (2.2.3). The following program takes about 108 seconds
> to complete on my ThinkPad (an i5-5200U with Arch Linux):
[...]
> By simply wrapping that code in a lambda the program finished in about
> 47 seconds. Using lets instead of defines is equally effective.
>
> I was quite surprised because I initially thought some optimization
> would just substitute those useless nodes symbols away, but it seems
> like that's not the case...

Right.  The problem is that toplevel variables can be mutated by
arbitrary code from other modules, e.g. by 'module-set!', so the
compiler cannot make any assumptions about what values those variables
will contain at runtime.

For non-toplevel variables, the situation is quite different.  In
Scheme, non-toplevel variables can be accessed only from within their
lexical scope, so if such a variable is not 'set!' from within its
scope, the compiler knows that it can never be mutated.  In that case,
it can assume that the variable will always contain its initial value,
which enables a great many optimizations including partial evaluation.

       Mark



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Performance impact of top level definitions
  2018-05-15  2:55 ` Mark H Weaver
@ 2018-05-15 16:55   ` Brian
  0 siblings, 0 replies; 3+ messages in thread
From: Brian @ 2018-05-15 16:55 UTC (permalink / raw)
  To: Mark H Weaver; +Cc: guile-devel

Hi Mark,

Thanks for that explanation, it makes sense now to me.

Cheers!


On Mon, 2018-05-14 at 22:55 -0400, Mark H Weaver wrote:
> Hi Brian,
> 
> Brian <gomesbascoy@gmail.com> writes:
> 
> > Today I found that top level defines have a significant performance
> > impact on Guile (2.2.3). The following program takes about 108
> > seconds
> > to complete on my ThinkPad (an i5-5200U with Arch Linux):
> 
> [...]
> > By simply wrapping that code in a lambda the program finished in
> > about
> > 47 seconds. Using lets instead of defines is equally effective.
> > 
> > I was quite surprised because I initially thought some optimization
> > would just substitute those useless nodes symbols away, but it
> > seems
> > like that's not the case...
> 
> Right.  The problem is that toplevel variables can be mutated by
> arbitrary code from other modules, e.g. by 'module-set!', so the
> compiler cannot make any assumptions about what values those
> variables
> will contain at runtime.
> 
> For non-toplevel variables, the situation is quite different.  In
> Scheme, non-toplevel variables can be accessed only from within their
> lexical scope, so if such a variable is not 'set!' from within its
> scope, the compiler knows that it can never be mutated.  In that
> case,
> it can assume that the variable will always contain its initial
> value,
> which enables a great many optimizations including partial
> evaluation.
> 
>        Mark



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-05-15 16:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-05-09  5:19 Performance impact of top level definitions Brian
2018-05-15  2:55 ` Mark H Weaver
2018-05-15 16:55   ` Brian

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).