From: Michael Kohout
Subject: questions: how to load a file "inlined" inside a let, and how to 	reference count objects
Date: 
Message-ID: <0600d640-a829-4a8d-9e0f-5adcbaa8713d@q21g2000hsa.googlegroups.com>
Hello folks, a relative noob with a couple of two questions for you.

Of late, I've been working my way through the Little Schemer, PG's
ANSI Lisp and On Lisp, and Siebel's book.

I understand lexical closures...but I'm having some trouble making
them suit my needs.  Is there was any(standard) way to "inline" files
into them?

Here's my example.
In the repl, I've got this:
   (let ((x 1)) (load  "test-scope.lisp")
and in the file "test-scope.lisp", I've got a function that simply
returns a var named x:
   (defun return-x () ( list x ))

after running the first line in the repl, I call the return-x method
but it errors, complaining x isn't bound.

Of course if I simply inline the code from the file into the repl it
works just fine:
CL-USER> (let ((x 1))(defun return-x () ( list x )))
RETURN-X
CL-USER> (return-x)
(1)

in summary, my question is if there is any standard macro or whatnot
that does this for me: inlining loaded files so the example closure
will work.

my second query is if there is any way to count references of a
variable, for the purposes of maintaining an object pool.  After a
brief google search I didn't find anything.

thanks for your help

Mike Kohout

From: Espen Vestre
Subject: Re: questions: how to load a file "inlined" inside a let, and how to  reference count objects
Date: 
Message-ID: <m1fxwebf6f.fsf@vestre.net>
Michael Kohout <········@gmail.com> writes:

> in summary, my question is if there is any standard macro or whatnot
> that does this for me: inlining loaded files so the example closure
> will work.

No, but you could easily hack a macro that reads the file and glues
the code into the function. But I really don't think you want to do
this. Are you sure you don't want to use dynamic variables instead?

-- 
  (espen)
From: Alex Mizrahi
Subject: Re: questions: how to load a file "inlined" inside a let, and how to reference count objects
Date: 
Message-ID: <47a18eea$0$90264$14726298@news.sunsite.dk>
 MK> in summary, my question is if there is any standard macro or whatnot
 MK> that does this for me: inlining loaded files so the example closure
 MK> will work.

no, there is no standard macro for this, and that's for reason -- this 
construct is weird and will be hard to maintain.

but if you really want it..

(defmacro form-from-file (filename)
     (with-open-file (s filename) (read s)))

this reads only one form, if you want many, add a loop and wrap stuff into 
progn.

 MK> my second query is if there is any way to count references of a
 MK> variable, for the purposes of maintaining an object pool.
 MK>   After a brief google search I didn't find anything.

most implementations use garbage collector that doesn't use "variable 
reference counting" (whatever it is), and it would be too much hassle to 
maintain this information just for you.
however, there is stuff called finalizers, so you have a chance to do 
something when object is garbage-collected, probably you can do something 
with it..
but it's implementation-dependent, consult documentation of your 
implementation.

and it's not a widespread practice to do so, so probably you don't need 
this. do you have profiling results that say that object creation/garbage 
collection is inefficient and you need to maintain pool yourself? 
From: Pascal J. Bourguignon
Subject: Re: questions: how to load a file "inlined" inside a let, and how to  reference count objects
Date: 
Message-ID: <7cve5a2twx.fsf@pbourguignon.anevia.com>
Michael Kohout <········@gmail.com> writes:

> Hello folks, a relative noob with a couple of two questions for you.
>
> Of late, I've been working my way through the Little Schemer, PG's
> ANSI Lisp and On Lisp, and Siebel's book.
>
> I understand lexical closures...but I'm having some trouble making
> them suit my needs.  Is there was any(standard) way to "inline" files
> into them?
>
> Here's my example.
> In the repl, I've got this:
>    (let ((x 1)) (load  "test-scope.lisp")
> and in the file "test-scope.lisp", I've got a function that simply
> returns a var named x:
>    (defun return-x () ( list x ))
>
> after running the first line in the repl, I call the return-x method
> but it errors, complaining x isn't bound.
>
> Of course if I simply inline the code from the file into the repl it
> works just fine:
> CL-USER> (let ((x 1))(defun return-x () ( list x )))
> RETURN-X
> CL-USER> (return-x)
> (1)
>
> in summary, my question is if there is any standard macro or whatnot
> that does this for me: inlining loaded files so the example closure
> will work.

This is not really a good idea, but if you really want it, have it:

(let ((x 1)) #.(COM.INFORMATIMAGO.COMMON-LISP.UTILITY:INCLUDE "file.lisp"))

;; http://darcs.informatimago.com/lisp/common-lisp/utility.lisp


> my second query is if there is any way to count references of a
> variable, for the purposes of maintaining an object pool.  After a
> brief google search I didn't find anything.

"reference count" is taboo around here ;-)

You can use weak references, in the form of weak hash-tables or other
weak structures or pointers.  That is, you don't do it yourself, you
let the garbage collect do it.  Type: (apropos "WEAK") in your REPL.


-- 
__Pascal Bourguignon__
From: John Thingstad
Subject: Re: questions: how to load a file "inlined" inside a let, and how to  reference count objects
Date: 
Message-ID: <op.t5su8xisut4oq5@pandora.alfanett.no>
P� Thu, 31 Jan 2008 11:03:58 +0100, skrev Pascal J. Bourguignon  
<···@informatimago.com>:

>
> You can use weak references, in the form of weak hash-tables or other
> weak structures or pointers.  That is, you don't do it yourself, you
> let the garbage collect do it.  Type: (apropos "WEAK") in your REPL.
>
>

Perhaps this is a good time to make him aware of the trivial-grabage  
library.
"trivial-garbage is a simple library that provides a portable API to  
finalizers, weak hash-tables and weak pointers."
http://www.cliki.net/trivial-garbage

--------------
John Thingstad
From: Kaz Kylheku
Subject: Re: questions: how to load a file "inlined" inside a let, and how to 	reference count objects
Date: 
Message-ID: <78fc5e7b-7b23-4b0c-8750-19eadce2ffcd@1g2000hsl.googlegroups.com>
On Jan 30, 11:54 pm, Michael Kohout <········@gmail.com> wrote:
> my second query is if there is any way to count references of a
> variable, for the purposes of maintaining an object pool.

Since Lisp objects don't have a reference count, you add a slot called
``reference-count'' to the object, and you manage it.

I suspect I know what you want: you want an object recycling hack,
whereby a ``pool'' aggregate holds references to objects which have a
zero refcount. These objects can be allocated from the pool, which
raises their refcount to be above zero. The refcount drops back to
zero when the object is no longer used, and the pool can then use the
object to satisfy an allocation request for a new object, thereby
recycling it.

This strategy is used, for instance, to recycle the vnodes in BSD's
filesystem layer. It is a useful hack in a situation where you are
already using reference counting for computing object lifetimes. You
then get this for free; it's just a change to the logic that handles a
reference count hitting zero.

If you already have your storage managed, you will might lose
performance, because you have to introduce reference counting to make
it work. Each operation to initialize, increment, decrement and test a
reference count is a cost. (And in the presence of threads, you have
to use atomic operations, which are expensive on a multiprocessor).
If, over the life of the object, these costs add up so that they
exceed the cost of recycling the object through the allocator, you're
losing.

You could try using finalization for this (language extension, not in
standard Lisp). When the finalization hook for an object is called,
you can stick it into a recycling pool. However, by then you have
already paid the cost of discovering that the object is garbage.
You've only prevented that garbage from being added to the known list
of free memory.

By keeping a large pool of objects that are not used, you might only
be slowing down the garbage collector. The more live objects there
are, the larger the data set that a full GC has to scan. And this
approach won't prevent GC, because it depends on GC to identify
objects to go into the pool.

It's an easy enough hack that you could whip it up quickly and profile
it. It's easy to enable and disable since it's nonintrusive.
From: Maciej Katafiasz
Subject: Re: questions: how to load a file "inlined" inside a let, and how to 	reference count objects
Date: 
Message-ID: <fnutk1$6jg$1@news.net.uni-c.dk>
Den Thu, 31 Jan 2008 20:03:07 -0800 skrev Kaz Kylheku:

> By keeping a large pool of objects that are not used, you might only be
> slowing down the garbage collector. The more live objects there are, the
> larger the data set that a full GC has to scan. And this approach won't
> prevent GC, because it depends on GC to identify objects to go into the
> pool.

It's been discussed in a fairly recent thread, and the conclusion was 
that keeping your own pool has almost 0% chance of being faster, and a 
very big chance of being much slower than a modern GC. I believe it was 
backed by experiences of people who actually tried that (and discovered 
they just made their code slower), but unfortunately, I can't find it 
now. 

Cheers,
Maciej