chicken-janitors
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-janitors] #1620: Some let bindings are not replaced resulti


From: Chicken Trac
Subject: Re: [Chicken-janitors] #1620: Some let bindings are not replaced resulting in unnecessary CPS calls
Date: Wed, 29 May 2019 14:45:54 -0000

#1620: Some let bindings are not replaced resulting in unnecessary CPS calls
---------------------------------+-------------------
            Reporter:  sjamaan   |      Owner:
                Type:  defect    |     Status:  new
            Priority:  major     |  Milestone:  5.2
           Component:  compiler  |    Version:  5.0.0
          Resolution:            |   Keywords:
Estimated difficulty:  hard      |
---------------------------------+-------------------
Description changed by sjamaan:

Old description:

> We've seen in #1604 that this is a performance killer:
>
> This code is fast when compiled with `-O5 -strict-types -fixnum-
> arithmetic`:
>
> {{{
> (define (fib n)
>   (if (or (eq? n 0) (eq? n 1))
>       n
>       (+ (fib (- n 1)) (fib (- n 2)))))
>
> (let loop ((n 0))
>   (when (< n 35)
>     (print "n=" n " => " (fib n))
>     (loop (+ n 1))))
> }}}
>
> This code is slow when compiled with the same options, even though the
> generated intermediate Scheme code is equivalent:
>
> {{{
> (define (fib n)
>   (if (or (= n 0) (= n 1))
>       n
>       (+ (fib (- n 1)) (fib (- n 2)))))
>
> (let loop ((n 0))
>   (when (< n 35)
>     (print "n=" n " => " (fib n))
>     (loop (+ n 1))))
> }}}
>
> The reason is that in the first case, the `eq?` calls get replaced by
> `(##core#inline "eqp" a b)` while in the second case, the `=` calls get
> replaced by `(let ((x a) (y b)) (##core_inline "C_eqp" x y))` and the
> `let` is not considered `replacable` even though (I think?) it should be.
>
> That's because `fib`'s arguments are marked by `analyze-expression` in
> `core.scm` as `captured`.
>
> Fixing this could potentially make a lot of code a lot faster! We
> definitely should run the benchmarks with and without the fix to find out
> how dramatic the improvement is.

New description:

 We've seen in #1604 that this is a performance killer:

 This code is fast when compiled with `-O5 -strict-types -fixnum-
 arithmetic`:

 {{{
 (define (fib n)
   (if (or (eq? n 0) (eq? n 1))
       n
       (+ (fib (- n 1)) (fib (- n 2)))))

 (let loop ((n 0))
   (when (< n 35)
     (print "n=" n " => " (fib n))
     (loop (+ n 1))))
 }}}

 This code is slow when compiled with the same options, even though the
 generated intermediate Scheme code is equivalent:

 {{{
 (define (fib n)
   (if (or (= n 0) (= n 1))
       n
       (+ (fib (- n 1)) (fib (- n 2)))))

 (let loop ((n 0))
   (when (< n 35)
     (print "n=" n " => " (fib n))
     (loop (+ n 1))))
 }}}

 The reason is that in the first case, the `eq?` calls get replaced by
 `(##core#inline "C_eqp" a b)` while in the second case, the `=` calls get
 replaced by `(let ((x a) (y b)) (##core_inline "C_eqp" x y))` and the
 `let` is not considered `replacable` even though (I think?) it should be.

 That's because `fib`'s arguments are marked by `analyze-expression` in
 `core.scm` as `captured`.

 Fixing this could potentially make a lot of code a lot faster! We
 definitely should run the benchmarks with and without the fix to find out
 how dramatic the improvement is.

--

--
Ticket URL: <https://bugs.call-cc.org/ticket/1620#comment:1>
CHICKEN Scheme <https://www.call-cc.org/>
CHICKEN Scheme is a compiler for the Scheme programming language.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]