Taoufik Dachraoui
1/16/2016 9:35:00 AM
On Saturday, January 16, 2016 at 12:19:05 AM UTC+1, Barry Margolin wrote:
> In article <3f9c12e0-566c-4a2c-afaf-3956ea45f568@googlegroups.com>,
> Taoufik Dachraoui <dachraoui.taoufik@gmail.com> wrote:
>
> > For the example 3, just by declaring b to special the conses increases; I
> > would
> > like to understand why the compiler is behaving differently when the the
> > array is
> > bound to a special variable?
>
> I explained that in my earlier response.
>
> When the variable is lexical, and the value isn't returned or assigned
> to a global variable, the compiler determines that the array doesn't
> need to be allocated on the heap. It can be allocated locally on the
> stack.
>
> When you declare the variable special, other threads can access that
> variable while your code is running, and they could assign the array to
> another variable. So it can't assume that the array can be destroyed
> when the LET returns.
>
> You may get the same behavior with (declare (dynamic-extent b)).
> Basically, it seems that in the earlier example, the compiler figured
> out automatically that it could do this optimization.
>
> --
> Barry Margolin, barmar@alum.mit.edu
> Arlington, MA
> *** PLEASE post questions in newsgroups, not directly to me ***
note in the following that the optimise declaration is necessary to avoid conses:
? (let ((arr (make-array 100)))
(defun test ()
(declare (optimize (speed 3) (safety 0) (debug 0)))
(incf (aref arr 1)))
(defmacro with-test (&rest body)
`(let ((arr ,arr))
(declare (optimize (speed 3) (safety 0) (debug 0)))
,@body)))
WITH-TEST
? (time (with-test (incf (aref arr 1))))
(WITH-TEST (INCF (AREF ARR 1)))
took 4 microseconds (0.000004 seconds) to run.
During that period, and with 2 available CPU cores,
6 microseconds (0.000006 seconds) were spent in user mode
6 microseconds (0.000006 seconds) were spent in system mode
3
? (time (progn (test)))
(PROGN (TEST))
took 5 microseconds (0.000005 seconds) to run.
During that period, and with 2 available CPU cores,
6 microseconds (0.000006 seconds) were spent in user mode
5 microseconds (0.000005 seconds) were spent in system mode
4
?
As you can see the array cannot be destroyed when the (test) returns, and still there is no consing
when the optimise declaration is used.
Kind regards
Taoufik