Hi all,
What's the idiom (without forcing read-time evaluation or dumping a new
Lisp image) to perform a lengthy calculation at compile time and return
the externalizable result at load time?
(eval-when (:compile-toplevel :load-toplevel :execute)
(defvar *variable* ...lengthy calculation...))
The above approach computes everything at compile time and every time the
file is loaded in a new image.
Thanks,
Adam
> What's the idiom (without forcing read-time evaluation or dumping a new
> Lisp image) to perform a lengthy calculation at compile time and return
> the externalizable result at load time?
>
> (eval-when (:compile-toplevel :load-toplevel :execute)
> (defvar *variable* ...lengthy calculation...))
>
> The above approach computes everything at compile time and every time the
> file is loaded in a new image.
I've realised I can simply compute the result at macro expansion time:
(defmacro macro-time-value (&body body)
(eval `(progn ,@body)))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defvar *variable* (macro-time-value (print "Evaluated!") (+ 1 2))))
This has the advantage of also working if the file is loaded from source.
Regards,
Adam
From: Marco Baringer
Subject: Re: Perform calculations at compile time and load the result
Date:
Message-ID: <m2wtxcqjk3.fsf@bese.it>
Adam Warner <······@consulting.net.nz> writes:
> I've realised I can simply compute the result at macro expansion time:
>
> (defmacro macro-time-value (&body body)
> (eval `(progn ,@body)))
you could also check out load-time-value
--
-Marco
Ring the bells that still can ring.
Forget your perfect offering.
There is a crack in everything.
That's how the light gets in.
-Leonard Cohen
Hi Marco Baringer,
>> I've realised I can simply compute the result at macro expansion time:
>>
>> (defmacro macro-time-value (&body body)
>> (eval `(progn ,@body)))
>
> you could also check out load-time-value
Thanks. That's what I modelled the name of my macro on.
This demonstrates that load-time-value reevaluates the form at load time
(if it didn't it would only return 3, not print 1 and 3. Which means it
will not save me any load time computation):
(eval-when (:compile-toplevel :load-toplevel :execute)
(defvar *variable* (load-time-value (print (+ (progn (print 1)) 2)))))
My macro time evaluation was unsuccessful for two reasons:
(a) Some implementations (CLISP, ABCL) don't cope well with huge fas
files. CLISP appeared to freeze for minutes but it eventually managed to
write out a 10MB file. ABCL bombed.
CLISP couldn't read a hash table of string literals back in without
setting an appropriate string encoding when loading the file. This appears
to preclude treating strings as arbitrary octet sequences.
(b) I'm creating multiple EQ hash tables of strings. I do not see there is
any portable way to ensure that all references to the same string literal
are coalesced to the same pointer. This means I must postpone creating the
hash tables until load time.
I was most impressed by SBCL's performance. Macro time evaluation wasn't
any slower than run time evaluation and the loading of the fasl file was
acceptably fast. CMUCL would have been using its interpreter (perhaps I
should wrap macro-time-value in a compile form!)
Regards,
Adam
From: David Steuber
Subject: Re: Perform calculations at compile time and load the result
Date:
Message-ID: <871xfivub6.fsf@david-steuber.com>
Adam Warner <······@consulting.net.nz> writes:
> I was most impressed by SBCL's performance. Macro time evaluation wasn't
> any slower than run time evaluation and the loading of the fasl file was
> acceptably fast. CMUCL would have been using its interpreter (perhaps I
> should wrap macro-time-value in a compile form!)
I didn't think CMUCL had an interpreter. I thought it was compile only.
--
An ideal world is left as an excercise to the reader.
--- Paul Graham, On Lisp 8.1
From: Edi Weitz
Subject: Re: Perform calculations at compile time and load the result
Date:
Message-ID: <uoeimot1m.fsf@agharta.de>
On 29 Oct 2004 03:48:29 -0400, David Steuber <·····@david-steuber.com> wrote:
> I didn't think CMUCL had an interpreter. I thought it was compile
> only.
That's true for SBCL, not for CMUCL.
* (defun foo (x) (1+ x))
FOO
* (type-of #'foo)
EVAL:INTERPRETED-FUNCTION
* (compile 'foo)
; Compiling LAMBDA (X):
; Compiling Top-Level Form:
FOO
NIL
NIL
* (type-of #'foo)
FUNCTION
* (lisp-implementation-version)
"CVS release-19a 19a-release-20040728 + minimal debian patches"
Edi.
--
Lisp is not dead, it just smells funny.
Real email: (replace (subseq ·········@agharta.de" 5) "edi")
From: Rahul Jain
Subject: Re: Perform calculations at compile time and load the result
Date:
Message-ID: <87sm7v994w.fsf@nyct.net>
Adam Warner <······@consulting.net.nz> writes:
> CLISP couldn't read a hash table of string literals back in without
> setting an appropriate string encoding when loading the file. This appears
> to preclude treating strings as arbitrary octet sequences.
Strings are character sequences.
(ARRAY (*) (UNSIGNED-BYTE 8)) are octect sequences. Use those if they
are what you want.
--
Rahul Jain
·····@nyct.net
Professional Software Developer, Amateur Quantum Mechanicist