From: Vijay L
Subject: (values (multiple-value-form)) whats the point?
Date: 
Message-ID: <1eaf81aa.0210280536.715dddda@posting.google.com>
In CLtL2 (sorry I dont have the page numbers right now), GLS says to
use
(values (multiple-values-form)) to return explicitly only one return
value.
he also mentions that this is good when the other values returned by
the form are computationally extensive. However, running:
1> (defun xyz () (values 100 (sleep 2)))
XYZ
2> (defun abc () (values (xyz)))
ABC
3> (time (abc))
2.0 seconds.
;and other details

the SLEEP is still called, so my question is of what practical use is
the above expression. Or is it because this has not been implemented
in LispWorks for Windows?

Thanks,

Vijay

From: Vassil Nikolov
Subject: Re: (values (multiple-value-form)) whats the point?
Date: 
Message-ID: <ud6puy6aq.fsf@poboxes.com>
    On 28 Oct 2002 05:36:34 -0800, ······@lycos.com (Vijay L) said:

    VL> In CLtL2 (sorry I dont have the page numbers right now), GLS says to
    VL> use
    VL> (values (multiple-values-form)) to return explicitly only one return
    VL> value.
    VL> he also mentions that this is good when the other values returned by
    VL> the form are computationally extensive.

I don't remember if looking at the original text would make his
point clearer, but as far as I can see without looking it up, he
refers to cases where the compiler open-codes the form producing
multiple values, so the compiler may omit the code it would have
generated to compute the second and subsequent values.  For
example, (FLOOR I J) may well be open-coded, especially with
declarations for I and J, and computing the second value may be
especially expensive if they are bignums.

---Vassil.

-- 
For an M-person job assigned to an N-person team, only rarely M=N.
From: Nils Goesche
Subject: Re: (values (multiple-value-form)) whats the point?
Date: 
Message-ID: <lkpttur6va.fsf@cartan.de>
······@lycos.com (Vijay L) writes:

> In CLtL2 (sorry I dont have the page numbers right now), GLS says to
> use (values (multiple-values-form)) to return explicitly only one
> return value.

> he also mentions that this is good when the other values returned by
> the form are computationally extensive.
> However, running:
> 1> (defun xyz () (values 100 (sleep 2)))
> XYZ
> 2> (defun abc () (values (xyz)))
> ABC
> 3> (time (abc))
> 2.0 seconds.
> ;and other details
> 
> the SLEEP is still called, so my question is of what practical use
> is the above expression. Or is it because this has not been
> implemented in LispWorks for Windows?

The example in CLtL2 is

(defun foo (x y)
  (values (floor (+ x y) y)))

In this case the compiler might recognize that it is not necessary to
compute the second return value of FLOOR (the remainder) and decide
to compute only the first one.  LispWorks, for instance, does indeed
do this: If you omit the VALUES, it will jump to FLOOR, however if you
return only the first value, it will call SYSTEM::SV-FLOOR instead,
which probably is faster.

This optimization doesn't change the behavior of FOO.  In your example
however, omitting the call to SLEEP would clearly change the behavior
of the function and such an optimization would be rather questionable.
I mean, if that was a valid optimization, then /every/ call to SLEEP
could be omitted as that will in most cases speed up the program,
right? :-)

Regards,
-- 
Nils Goesche
"Don't ask for whom the <CTRL-G> tolls."

PGP key ID 0x0655CFA0
From: Kaz Kylheku
Subject: Re: (values (multiple-value-form)) whats the point?
Date: 
Message-ID: <cf333042.0210281544.68b141a9@posting.google.com>
······@lycos.com (Vijay L) wrote in message news:<····························@posting.google.com>...
> In CLtL2 (sorry I dont have the page numbers right now), GLS says to
> use
> (values (multiple-values-form)) to return explicitly only one return
> value.

Doing this is usually a poor idea. If the subordinate form returns
multiple values, there is no reason to kill all but the first one.
Your function then becomes badly behaved ``middleware'' that will
chagrin someone who tries to pass multiple values through it one day.
It's best to leave the extra values alone: a caller may want them.

> he also mentions that this is good when the other values returned by
> the form are computationally extensive.
                               ^^^^^^^^^^
Expensive?

But if they have side effects, they have to be evaluated. So this is
not an optimization at all. In some trivial cases the compiler may
elide the computation of the second and subsequent values, seeing that
the multiple value is immediately passed to VALUES. But that is an
optimization, not part of the semantics of VALUES.

 However, running:
> 1> (defun xyz () (values 100 (sleep 2)))
> XYZ
> 2> (defun abc () (values (xyz)))
> ABC
> 3> (time (abc))
> 2.0 seconds.

Yes, of course. The function call (sleep 2) cannot be elided.  VALUES
has no such semantics.
From: Vijay L
Subject: Re: (values (multiple-value-form)) whats the point?
Date: 
Message-ID: <1eaf81aa.0210282221.3996e8fc@posting.google.com>
Kaz Kylheku wrote:
> Doing this is usually a poor idea. If the subordinate form returns
> multiple values, there is no reason to kill all but the first one.
> Your function then becomes badly behaved ``middleware'' that will
> chagrin someone who tries to pass multiple values through it one day.
> It's best to leave the extra values alone: a caller may want them.
 
Which was, in a way, my point when I posted my question.
 
> But if they have side effects, they have to be evaluated. So this is
> not an optimization at all. In some trivial cases the compiler may
> elide the computation of the second and subsequent values, seeing that
> the multiple value is immediately passed to VALUES. But that is an
> optimization, not part of the semantics of VALUES.

Understood. Thanks. I was misunderstood what was written. Using SLEEP
in the example was a blunder.

By what yourself, Nils, and Vassil say this will be optimized:

(defun foo () (values (floor (+ most-positive-fixnum 0.1) 0.001)))

because FLOOR is open-coded (whatever that is), and because the
subsequent values do not have any side-effects, and does not change
the behaviour of FOO.

Will

(defun foo () (floor (+ most-positive-fixnum 0.1) 0.0001)))

(defun bar () (values (foo)))

be optimized similarly?

Vassil Nikolov wrote:
> I can see without looking it up, he
> refers to cases where the compiler open-codes the form producing
> multiple values, so the compiler may omit the code it would have
> generated to compute the second and subsequent values.  For
> example, (FLOOR I J) may well be open-coded, especially with
> declarations for I and J, and computing the second value may be
> especially expensive if they are bignums.

What does open-coding mean?

Thanks,
Vijay L
From: Vassil Nikolov
Subject: Re: (values (multiple-value-form)) whats the point?
Date: 
Message-ID: <uptttekef.fsf@poboxes.com>
    On 28 Oct 2002 22:21:52 -0800, ······@lycos.com (Vijay L) said:

    [...]
    VL> By what yourself, Nils, and Vassil say this will be optimized:

In fact, _may_ be optimized.  (Since the language itself does not
require the optimization, we could say `will' only for some
specific implementation and then very likely with regards to a
specific combination of compiler settings.)

    VL> (defun foo () (values (floor (+ most-positive-fixnum 0.1) 0.001)))

Strictly speaking, I had integer arguments in mind, particularly as
computing something like (FLOOR (EXPT 34 56) (EXPT 12 34)) is going
to cost considerably more than that.

    VL> because FLOOR is open-coded (whatever that is), and because the

If the call to FLOOR is open-coded, that means the generated code
will be something different from code for these two steps:
accessing the function cell of the symbol FLOOR, and transferring
control to whatever function is stored there.  If it is open-coded
(or inlined), the generated code may, for example, contain just a
transfer of control to an implementation-specific function that
performs the operation (bypassing the access to FLOOR's function
cell), or the generated code may consist directly of the native
machine instructions for performing the operation (especially
likely if the arguments are declared to be fixnums).

    VL> subsequent values do not have any side-effects, and does not change
    VL> the behaviour of FOO.

    VL> Will

    VL> (defun foo () (floor (+ most-positive-fixnum 0.1) 0.0001)))

    VL> (defun bar () (values (foo)))

    VL> be optimized similarly?

That may be more likely if FOO's definition is preceded by a
(DECLAIM (INLINE FOO)).

(Again, it _may_; if you want to ask if it _will_, take an
implementation and examine it.  (Make sure you know about
DISASSEMBLE.))

---Vassil.

-- 
For an M-person job assigned to an N-person team, only rarely M=N.
From: Vijay L
Subject: Re: (values (multiple-value-form)) whats the point?
Date: 
Message-ID: <1eaf81aa.0210310824.6f2c4d6d@posting.google.com>
Vassil Nikolov <········@poboxes.com> wrote in message news:<·············@poboxes.com>...
> On 28 Oct 2002 22:21:52 -0800, ······@lycos.com (Vijay L) said:
> 
>     [...]
>     VL> By what yourself, Nils, and Vassil say this will be optimized:

> If the call to FLOOR is open-coded, that means the generated code
> will be something different from code for these two steps:
> accessing the function cell of the symbol FLOOR, and transferring
> control to whatever function is stored there.  If it is open-coded
> (or inlined), the generated code may, for example, contain just a
> transfer of control to an implementation-specific function that
> performs the operation (bypassing the access to FLOOR's function
> cell), or the generated code may consist directly of the native
> machine instructions for performing the operation (especially
> likely if the arguments are declared to be fixnums).
[snip]
> That may be more likely if FOO's definition is preceded by a
> (DECLAIM (INLINE FOO)).
Thanks. Understood.

> Again, it _may_; if you want to ask if it _will_,
My mistake, I should have said _may_, I know that when it comes to
code optimizations it is (almost) always _may_.
Moral: Premature posting is the root of (almost) all evil.

Another question when concerning optimizations. Will a (DECLAIM
(INLINE FOO)) statement always inline FOO ? I know that in C++,
declaring a function inline may or may not actually be inlined, which
is why I ask the same here.

> Make sure you know about DISASSEMBLE.
Not yet and I don't think I'll go into it just yet. Thanks for the
advice anyhow.

Thanks,
Vijay L
From: Tim Bradshaw
Subject: Re: (values (multiple-value-form)) whats the point?
Date: 
Message-ID: <ey3lm4e8tc7.fsf@cley.com>
* Vijay L wrote:
> Another question when concerning optimizations. Will a (DECLAIM
> (INLINE FOO)) statement always inline FOO ? I know that in C++,
> declaring a function inline may or may not actually be inlined, which
> is why I ask the same here.

No, it means that it *may* be inlined.  You (almost certainly) need to
have the inline declaration before the definition of FOO (so the
system stores the information needed to inline it), and some
implementations don't do inlining anyway.  Also inlining may be
overridden by local declarations.

--tim
From: Will Deakin
Subject: Re: (values (multiple-value-form)) whats the point?
Date: 
Message-ID: <aprmdp$jqb$1@newsreaderg1.core.theplanet.net>
Vijay L wrote:
> Another question when concerning optimizations. Will a (DECLAIM
> (INLINE FOO)) statement always inline FOO ?
No. For the hyperspec sayeth:

`Inline specifies that it is desirable for the compiler to produce 
inline calls to the functions named by function-names;... A compiler 
is free to ignore this declaration.'

:)w
From: Vijay L
Subject: Re: (values (multiple-value-form)) whats the point?
Date: 
Message-ID: <1eaf81aa.0210312208.6daad392@posting.google.com>
Will Deakin <···········@hotmail.com> wrote in message news:<············@newsreaderg1.core.theplanet.net>...
> Vijay L wrote:
> > Another question when concerning optimizations. Will a (DECLAIM
> > (INLINE FOO)) statement always inline FOO ?
> No. For the hyperspec sayeth:
> 
> `Inline specifies that it is desirable for the compiler to produce 
> inline calls to the functions named by function-names;... A compiler 
> is free to ignore this declaration.'
> 
> :)w
Thanks for that. I apologize if I seem to ask questions before
searching for the answers myself. I have both CLtL2 and Hyperspec in
my room, but no internet connection. So I don't have access to Lisp
when reading your posts _and_ the bandwidth here is so bad that I
cannot view more than 3 webpages at a time. And this is worst when
going thru c.l.l. (I don't know if the other groups are this bad but I
assume they are). Henceforth, nevertheless, I shall ask more
purposeful questions.

Thanks,

Vijay L
From: Will Deakin
Subject: Re: (values (multiple-value-form)) whats the point?
Date: 
Message-ID: <3DC24390.6000200@hotmail.com>
Vijay L wrote:
> Thanks for that. I apologize if I seem to ask questions before
> searching for the answers myself.
Don't be daft. I knew this to be true but say I *think* is some what 
less definitive than quoting the Hyperspec.

:)w
From: Will Deakin
Subject: Re: (values (multiple-value-form)) whats the point?
Date: 
Message-ID: <3DC243A3.9050501@hotmail.com>
Vijay L wrote:
> Thanks for that. I apologize if I seem to ask questions before
> searching for the answers myself.
Don't be daft. I knew this to be true but to say I *think* x is true 
is some what less definitive than quoting the Hyperspec.

:)w
From: Will Deakin
Subject: Re: (values (multiple-value-form)) whats the point?
Date: 
Message-ID: <aptg58$co5$1@newsreaderg1.core.theplanet.net>
Vijay L wrote:
> Thanks for that. I apologize if I seem to ask questions before
> searching for the answers myself.
Don't be daft. I knew this to be true but to say I *think* x is true 
is some what less definitive than quoting the Hyperspec.

:)w
From: Vijay L
Subject: Re: (values (multiple-value-form)) whats the point?
Date: 
Message-ID: <1eaf81aa.0211010852.4cf78934@posting.google.com>
Will Deakin <···········@hotmail.com> wrote in message news:<············@newsreaderg1.core.theplanet.net>...
> Vijay L wrote:
> > Thanks for that. I apologize if I seem to ask questions before
> > searching for the answers myself.
> Don't be daft. I knew this to be true but to say I *think* x is true 
> is some what less definitive than quoting the Hyperspec.
> 
> :)w

I was being daft because I felt guilty after reading the link that
Brian Palmer pointed out (in "The differenced between c++ and The
Ultimate Language"):
http://www.tuxedo.org/~esr/faqs/smart-questions.html

Thanks,

Vijay L
From: Will Deakin
Subject: OT appologies [Re: (values (multiple-value-form)) whats the point?]
Date: 
Message-ID: <aptg9d$co5$2@newsreaderg1.core.theplanet.net>
Sorry about the multiple posts. Mea culpa. Or was it was a pathetic 
attempt to get higher in the c.l.l. ratings this week...

;)
From: Vijay L
Subject: Re: OT appologies [Re: (values (multiple-value-form)) whats the point?]
Date: 
Message-ID: <1eaf81aa.0211010848.3ec3f9be@posting.google.com>
Will Deakin <···········@hotmail.com> wrote in message news:<············@newsreaderg1.core.theplanet.net>...
> Sorry about the multiple posts. Mea culpa. Or was it was a pathetic 
> attempt to get higher in the c.l.l. ratings this week...
> 
> ;)

If that is the case maybe I should try too :)

Thanks,

Vijay L
From: Adam Warner
Subject: Re: (values (multiple-value-form)) whats the point?
Date: 
Message-ID: <pan.2002.10.29.06.56.09.177332@consulting.net.nz>
Hi Vijay L,

> What does open-coding mean?

http://cvs2.cons.org/ftp-area/cmucl/doc/cmu-user/compiler.html#toc97

   When a function call is open coded, inline code whose effect is
   equivalent to the function call is substituted for that function call.

Regards,
Adam