(values (multiple-value-form)) whats the point? 
Author Message
 (values (multiple-value-form)) whats the point?

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



Fri, 15 Apr 2005 21:36:34 GMT  
 (values (multiple-value-form)) whats the point?

Quote:

> 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



Fri, 15 Apr 2005 22:11:05 GMT  
 (values (multiple-value-form)) whats the point?

    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.



Fri, 15 Apr 2005 22:41:33 GMT  
 (values (multiple-value-form)) whats the point?

Quote:

> 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.

Quote:
> 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:

Quote:
> 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.


Sat, 16 Apr 2005 07:44:03 GMT  
 (values (multiple-value-form)) whats the point?

Quote:

> 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.

Quote:
> 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?

Quote:

> 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



Sat, 16 Apr 2005 14:21:52 GMT  
 (values (multiple-value-form)) whats the point?
Hi Vijay L,

Quote:
> 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



Sat, 16 Apr 2005 14:56:09 GMT  
 (values (multiple-value-form)) whats the point?

    [...]
    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.



Sat, 16 Apr 2005 16:09:28 GMT  
 (values (multiple-value-form)) whats the point?

Quote:


>     [...]
>     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.

Quote:
> 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.

Quote:
> 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



Tue, 19 Apr 2005 00:24:38 GMT  
 (values (multiple-value-form)) whats the point?

Quote:

> 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



Tue, 19 Apr 2005 00:29:44 GMT  
 (values (multiple-value-form)) whats the point?
Quote:

> 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



Tue, 19 Apr 2005 00:42:04 GMT  
 (values (multiple-value-form)) whats the point?
Quote:


> > 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



Tue, 19 Apr 2005 14:08:27 GMT  
 (values (multiple-value-form)) whats the point?
Quote:

> 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



Tue, 19 Apr 2005 17:04:16 GMT  
 (values (multiple-value-form)) whats the point?
Quote:

> 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



Tue, 19 Apr 2005 17:04:35 GMT  
 (values (multiple-value-form)) whats the point?
Quote:

> 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



Tue, 19 Apr 2005 17:07:26 GMT  
 
 [ 19 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Multiple-value-list with no values.

2. Defining multiple-values binding forms

3. multipart form data & other form values

4. Generating a value from a VALUE ERROR

5. Comparing value in an input field to any value from another file

6. C4 - Autoincrementing default values for multi-valued template symbols

7. Copying one array value into subsequent array values

8. value dependent boolean to retain its sense when values would change boolean

9. how to set knobs to the dicreet values indicated and no values in between

10. sampling waveform values and writing corresponding time values

11. waveform value different from any other value

12. Scheme values and call-with-values

 

 
Powered by phpBB® Forum Software