Discussion:
Is this a good use for "compile"
Mark Carter
2018-02-18 21:56:08 UTC
Permalink
New scheme user here.

Suppose I'm writing a spreadsheet. The user inputs a formula for a cell.

The plan is to use guile's peg parser to convert the formula into a
lambda expression, which I then compile in order to speed-up subsequent
processing.

So, suppose I convert the user's formula to a list, which turns out to
be, for example: '(lambda (x) (+ x 13)) and compile it and save it in a
formula table:

(hash-set! my-cell-formulae some-cell-ref (compile '(lambda (x) (+ x 13))))

So I can I expect a speed-up by having done the compile, as opposed to
an eval?

I assume the answer is "yes", but I wanted to check.
Matt Wette
2018-02-19 17:30:57 UTC
Permalink
Post by Mark Carter
New scheme user here.
Suppose I'm writing a spreadsheet. The user inputs a formula for a cell.
The plan is to use guile's peg parser to convert the formula into a
lambda expression, which I then compile in order to speed-up
subsequent processing.
So, suppose I convert the user's formula to a list, which turns out to
be, for example: '(lambda (x) (+ x 13)) and compile it and save it in
(hash-set! my-cell-formulae some-cell-ref (compile '(lambda (x) (+ x 13))))
So I can I expect a speed-up by having done the compile, as opposed to
an eval?
I assume the answer is "yes", but I wanted to check
It is not clear to me how this will work.  First of all, compile may
generate machine-dependent code.  Second, how do you propose to evaluate
my-cell-formulae ?
Vítor De Araújo
2018-02-20 00:08:25 UTC
Permalink
Post by Mark Carter
New scheme user here.
Suppose I'm writing a spreadsheet. The user inputs a formula for a cell.
The plan is to use guile's peg parser to convert the formula into a
lambda expression, which I then compile in order to speed-up subsequent
processing.
So, suppose I convert the user's formula to a list, which turns out to
be, for example: '(lambda (x) (+ x 13)) and compile it and save it in a
(hash-set! my-cell-formulae some-cell-ref (compile '(lambda (x) (+ x 13))))
So I can I expect a speed-up by having done the compile, as opposed to
an eval?
I assume the answer is "yes", but I wanted to check.
We can try this out:

scheme@(guile-user)> (use-modules (system base compile))
scheme@(guile-user)> (define exp '(lambda (n)
(let loop ([i n] [total 0])
(if (= i 0)
total
(loop (1- i) (+ i total))))))
scheme@(guile-user)> (define f1 (eval exp (interaction-environment)))
scheme@(guile-user)> (define f2 (compile exp #:env
(interaction-environment)))
scheme@(guile-user)> ,time (f1 1000000)
$2 = 500000500000
;; 0.845240s real time, 0.895351s run time. 0.071494s spent in GC.
scheme@(guile-user)> ,time (f2 1000000)
$3 = 500000500000
;; 0.067317s real time, 0.067278s run time. 0.000000s spent in GC.

So the answer does seem to be "yes": the compiled procedure is much faster.
--
Vítor De Araújo
https://elmord.org/
Mark Carter
2018-02-20 09:28:07 UTC
Permalink
                                    (let loop ([i n] [total 0])
                                      (if (= i 0)
                                          total
                                        (loop (1- i) (+ i total))))))
(interaction-environment)))
$2 = 500000500000
;; 0.845240s real time, 0.895351s run time.  0.071494s spent in GC.
$3 = 500000500000
;; 0.067317s real time, 0.067278s run time.  0.000000s spent in GC.
So the answer does seem to be "yes": the compiled procedure is much faster.
Thanks. A 10X speedup is just what the doctor ordered.

As it happened, I was looking for a way to time functions, and didn't
realise that guile has a convenient way of doing this.
Mark Carter
2018-02-20 11:03:13 UTC
Permalink
: It is not clear to me how this will work.  First of all, compile may
generate machine-dependent code.

That's OK. Generated code doesn't need to be saved. Formulae are
compiled as needed.

: Second, how do you propose to evaluate my-cell-formulae ?

To evaluate a cell, I would call ((hash-ref my-cell-formula some-cell-ref))

Formulae are compiled to parameterless lambdas, which are stored in
my-cell-formulae, So all I need to do is retrieve the relevant one, and
execute it.

Loading...