From: S. Robert James
Subject: Inline functions in Lisp
Date: 
Message-ID: <1172715419.533199.59310@v33g2000cwv.googlegroups.com>
Wow! Just learned how to inline some functions in Lisp and got a 10x
speed improvement.  (Prob' my Ruby experience encourages me to write
lots of short functions for clarity...)

Where can I read more about this?
Does it make sense to inline builtins that I use repeatedly in loops
(mapcar some remove-if)?
What about lambda and curried functions? (I use them a lot too).

From: S. Robert James
Subject: Re: Inline functions in Lisp
Date: 
Message-ID: <1172717516.234465.280620@h3g2000cwc.googlegroups.com>
On Feb 28, 9:16 pm, "S. Robert James" <············@gmail.com> wrote:
> Wow! Just learned how to inline some functions in Lisp and got a 10x
> speed improvement.  (Prob' my Ruby experience encourages me to write
> lots of short functions for clarity...)
>
> Where can I read more about this?
> Does it make sense to inline builtins that I use repeatedly in loops
> (mapcar some remove-if)?
> What about lambda and curried functions? (I use them a lot too).

similar point - I know Lisp supports static type declarations for
additional speed, but I can't find docs, or an introduction to this,
anywhere.  Any starters?
From: Barry Margolin
Subject: Re: Inline functions in Lisp
Date: 
Message-ID: <barmar-101FC8.21551828022007@comcast.dca.giganews.com>
In article <························@h3g2000cwc.googlegroups.com>,
 "S. Robert James" <············@gmail.com> wrote:

> On Feb 28, 9:16 pm, "S. Robert James" <············@gmail.com> wrote:
> > Wow! Just learned how to inline some functions in Lisp and got a 10x
> > speed improvement.  (Prob' my Ruby experience encourages me to write
> > lots of short functions for clarity...)
> >
> > Where can I read more about this?
> > Does it make sense to inline builtins that I use repeatedly in loops
> > (mapcar some remove-if)?
> > What about lambda and curried functions? (I use them a lot too).
> 
> similar point - I know Lisp supports static type declarations for
> additional speed, but I can't find docs, or an introduction to this,
> anywhere.  Any starters?

CLtL2?

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: S. Robert James
Subject: Re: Inline functions in Lisp
Date: 
Message-ID: <1172720153.009706.312370@t69g2000cwt.googlegroups.com>
On Feb 28, 9:55 pm, Barry Margolin <······@alum.mit.edu> wrote:
> CLtL2?

Not having had the priv. of going to school in Boston :-),
I'm a newbie trying to teach myself Lisp in some of my spare time.
I've found the help on this newsgroup to be tremendous, and am
especially honored to be able to discuss things which true experts in
the field (like Dr. Russ, for example).  But getting curt replies,
usually with prefixed with some punctation intended to be the
electronic equivalent of "Duh!", can get kind of discouraging.  No, I
cannot understand the Hyperspec - I don't know what half of those
terms mean.  No, I do not know all of the acronyms and keywords
commonly used by Lispers - I'm still a neophyte.  A one acronym
response only discourages - as if to say "if you're not a Lisper
enough to know what this means, don't bother".  If you don't have the
time to provide help or insight, that's certainly understandable.  But
in that case, I'd prefer you keep the one word acronyms and
exclamations to yourself.
From: Adam
Subject: Re: Inline functions in Lisp
Date: 
Message-ID: <1172721600.002639.175270@k78g2000cwa.googlegroups.com>
Mr James,

As a new lisp user I can sympathize with the confusion involved in
learning lots of these issues. However, it is worth considering that
what you are asking of them is a lot more involved than asking a
similar question on, say, a php board. There, some expert would hand
you a piece of code or tutorial to do exactly what you wanted, but you
wouldn't understand it. With the degree of orthogonal abstraction in
lisp, this sort of one-off solution is worthless. Answering such a
question requires a lot more effort on their part.

I didn't know the answer myself, so I went to look at the spec, which
in this case is very clear:

  A declare form is known as a declaration. Declarations may occur
only at the beginning of the bodies of certain special forms; ..
Declarations may occur in lambda-expressions and in the forms listed
here.

  ...gives a list of what you would expect: functions, loops, etc.

  Each decl-spec is a list whose car is a symbol specifying the kind
of declaration to be made. Declarations may be divided into two
classes: those that concern the bindings of variables, and those that
do not. (The special declaration is the sole exception: it effectively
falls into both classes, as explained below.) Those that concern
variable bindings apply only to the bindings made by the form at the
head of whose body they appear. For example, in

  (defun foo (x)
    (declare (type float x)) ...
    (let ((x 'a)) ...)
    ...)

  the type declaration applies only to the outer binding of x, and not
to the binding made in the let.

  .. And for declaring the results of evaluating code...

  Frequently it is useful to declare that the value produced by the
evaluation of some form will be of a particular type. Using declare
one can declare the type of the value held by a bound variable, but
there is no easy way to declare the type of the value of an unnamed
form. For this purpose the "the" special form is defined;

  (the string (copy-seq x))     ;The result will be a string

  The values type specifier may be used to indicate the types of
multiple values:

  (the (values integer integer) (floor x y))

That is all I needed to know and hopefully what you needed as well.
Now, if there is a particular instance where you aren't sure what to
do or a piece of existing code you don't understand, that may get the
board's excitement and be more helpful for everyone.

Hope that helped,

Adam Solove

On Mar 1, 11:35 am, "S. Robert James" <············@gmail.com> wrote:
> On Feb 28, 9:55 pm, Barry Margolin <······@alum.mit.edu> wrote:
>
> > CLtL2?
>
> Not having had the priv. of going to school in Boston :-),
> I'm a newbie trying to teach myself Lisp in some of my spare time.
> I've found the help on this newsgroup to be tremendous, and am
> especially honored to be able to discuss things which true experts in
> the field (like Dr. Russ, for example).  But getting curt replies,
> usually with prefixed with some punctation intended to be the
> electronic equivalent of "Duh!", can get kind of discouraging.  No, I
> cannot understand the Hyperspec - I don't know what half of those
> terms mean.  No, I do not know all of the acronyms and keywords
> commonly used by Lispers - I'm still a neophyte.  A one acronym
> response only discourages - as if to say "if you're not a Lisper
> enough to know what this means, don't bother".  If you don't have the
> time to provide help or insight, that's certainly understandable.  But
> in that case, I'd prefer you keep the one word acronyms and
> exclamations to yourself.
From: Dan Bensen
Subject: Re: Inline functions in Lisp
Date: 
Message-ID: <es5r0t$8r8$1@wildfire.prairienet.org>
S. Robert James wrote:
> On Feb 28, 9:55 pm, Barry Margolin <······@alum.mit.edu> wrote:
>> CLtL2?
> ... getting curt replies... can get kind of discouraging.

Common Lisp the Language, 2nd Edition.
here:
http://www.cs.cmu.edu/Groups/AI/html/cltl/cltl2.html
The links to the online version are busted, but you can
download it from links at the bottom of the page.
Declarations are in chapter 9.

-- 
Dan
www.prairienet.org/~dsb
From: Tim Bradshaw
Subject: Re: Inline functions in Lisp
Date: 
Message-ID: <1172749379.242343.311000@t69g2000cwt.googlegroups.com>
On Mar 1, 3:35 am, "S. Robert James" A one acronym
> response only discourages - as if to say "if you're not a Lisper
> enough to know what this means, don't bother".

I think what it actually means is: if you're not able to type that
term into google, then don't bother.  I agree with that sentiment: the
very first hit is a link to the whole book online, and the first two
pages of hits were all references to it in some form or other.  I
mean, really, learn to use the interweb before you try to learn CL
(that's an acronym by the way, it stands for "Common Lisp").

--tim
From: Zach Beane
Subject: Re: Inline functions in Lisp
Date: 
Message-ID: <m38xehky29.fsf@unnamed.xach.com>
"S. Robert James" <············@gmail.com> writes:

> On Feb 28, 9:55 pm, Barry Margolin <······@alum.mit.edu> wrote:
> > CLtL2?
> 
[snip]
> But getting curt replies, usually with prefixed with some punctation
> intended to be the electronic equivalent of "Duh!", can get kind of
> discouraging.

When you look at Rorschach inkblots, do they always seem to be
attacking you?

Zach
From: fireblade
Subject: Re: Inline functions in Lisp
Date: 
Message-ID: <1172760806.849810.187630@h3g2000cwc.googlegroups.com>
On Mar 1, 4:35 am, "S. Robert James" <············@gmail.com> wrote:
> On Feb 28, 9:55 pm, Barry Margolin <······@alum.mit.edu> wrote:
>
> > CLtL2?
>
> Not having had the priv. of going to school in Boston :-),
> I'm a newbie trying to teach myself Lisp in some of my spare time.
> I've found the help on this newsgroup to be tremendous, and am
> especially honored to be able to discuss things which true experts in
> the field (like Dr. Russ, for example).  But getting curt replies,
> usually with prefixed with some punctation intended to be the
> electronic equivalent of "Duh!", can get kind of discouraging.  No, I
> cannot understand the Hyperspec - I don't know what half of those
> terms mean.  No, I do not know all of the acronyms and keywords
> commonly used by Lispers - I'm still a neophyte.  A one acronym
> response only discourages - as if to say "if you're not a Lisper
> enough to know what this means, don't bother".  If you don't have the
> time to provide help or insight, that's certainly understandable.  But
> in that case, I'd prefer you keep the one word acronyms and
> exclamations to yourself.

Ok
Sopose you have a function for doubling a number :
(defun double (x)
  (* 2 x))
It works nice for all kinds of numbers,
but it's slow for your application , you disassembled it
and if your on lw (Lispworks www.lispworks.com)
you got something like this
;;The function disassemble is a debugging aid that composes symbolic
instructions or expressions in some implementation-dependent language
which represent the code used to produce the function which is or is
named by the argument fn. ...
http://www.lisp.org/HyperSpec/Body/fun_disassemble.html
(disassemble 'double)
200DD562:
       0:      3B25EC330020     cmp   esp, [200033EC]  ; SYSTEM:*
%STACK-LIMIT
       6:      761B             jbe   L1
       8:      80FD01           cmpb  ch, 1
      11:      7516             jne   L1
      13:      55               push  ebp
      14:      89E5             move  ebp, esp
      16:      A803             testb al, 3
      18:      7514             jne   L2
      20:      89C3             move  ebx, eax
      22:      C1FB02           sar   ebx, 2
      25:      6BFB08           imul  edi, ebx, 8
      28:      700A             jo    L2
      30:      FD               std
      31:      89F8             move  eax, edi
      33:      C9               leave
      34:      C3               ret
L1:   35:      E880150300       call  2010EB0A         ; #<Function
RUNTIME:BAD-ARGS-OR-STACK 2010EB0A>
L2:   40:      FF7500           push  [ebp]
      43:      83ED04           sub   ebp, 4
      46:      8B7508           move  esi, [ebp+8]
      49:      897504           move  [ebp+4], esi
      52:      894508           move  [ebp+8], eax
      55:      B808000000       move  eax, 8
      60:      C9               leave
      61:      E90E000300       jmp   2010D5B2         ; #<Function
SYSTEM::*%*$ANY-STUB 2010D5B2>
NIL

so you decided to make it work only with fuxnums which is all you
need
and make it something like this:
(defun double-optimized (x)
  (declare (type fixnum x y))
  (the fixnum (* 2 x)))
You run disassemble againg and whoops !!!  more than twice
instructions less
(disassemble 'double-optimized)
2171991A:
       0:      3B25EC330020     cmp   esp, [200033EC]  ; SYSTEM:*
%STACK-LIMIT
       6:      760E             jbe   L1
       8:      80FD01           cmpb  ch, 1
      11:      7509             jne   L1
      13:      55               push  ebp
      14:      89E5             move  ebp, esp
      16:      C1E001           shl   eax, 1
      19:      FD               std
      20:      C9               leave
      21:      C3               ret
L1:   22:      E8D5519FFE       call  2010EB0A         ; #<Function
RUNTIME:BAD-ARGS-OR-STACK 2010EB0A>
      27:      90               nop
      28:      90               nop
      29:      90               nop
NIL

Your functions is speedy .

cheers
slobodan
BTW
I'm also a selft-thought , gratuated in financial management in
Skopje,Macedonia.
From: S. Robert James
Subject: Re: Inline functions in Lisp
Date: 
Message-ID: <1172772555.286044.110090@n33g2000cwc.googlegroups.com>
Thanks for all the great material.  This has given me a lot to learn!

To clarify one point:
I realize that premature optimization is bad.  I've already solved
correctly problem I've decided to work on, and am at the point where
CPU time (for large problems) is high.  Inlining my functions and
declaring types has improved it 100 fold (literally), and I've been
pouring over the dissasembly to get that even higher!
From: fireblade
Subject: Re: Inline functions in Lisp
Date: 
Message-ID: <1172822119.912009.5880@8g2000cwh.googlegroups.com>
On Mar 1, 7:09 pm, "S. Robert James" <············@gmail.com> wrote:
> Thanks for all the great material.  This has given me a lot to learn!
>
> To clarify one point:
> I realize that premature optimization is bad.  I've already solved
> correctly problem I've decided to work on, and am at the point where
> CPU time (for large problems) is high.  Inlining my functions and
> declaring types has improved it 100 fold (literally), and I've been
> pouring over the dissasembly to get that even higher!

I'm happy that you're pleased, this is actually a very friendly group.
But most people get quckly tired of answering to same questions  you
could
easily find with 2 min search.

If you are fan of lisp, don't go to the barebones in order to save
some extra
cpu' cycles, shoot for clarity and it will pay off.
When i recoded my  raycar'  from c++ it's it about 25% slower but
code
is by far smaller, cleaner and testing new settings and adding new
feats is
a real joy.

I wish a happy lisping
bobi

'
cpu- central processing unit
'
Type of physical representation or car used in video games
From: Josip Gracin
Subject: Re: Inline functions in Lisp
Date: 
Message-ID: <es5s4r$bcg$1@sunce.iskon.hr>
S. Robert James wrote:
> On Feb 28, 9:55 pm, Barry Margolin <······@alum.mit.edu> wrote:
>> CLtL2?
> 
> electronic equivalent of "Duh!", can get kind of discouraging.  No, I
> cannot understand the Hyperspec - I don't know what half of those
> terms mean.  No, I do not know all of the acronyms and keywords
> commonly used by Lispers - I'm still a neophyte.  A one acronym
> response only discourages

Relax, it seems you misunderstood the answer.  Start with writing cltl2 
in Google and pressing "I'm feeling Lucky".  I find that book very 
informative and interesting to read.
From: Ari Johnson
Subject: Re: Inline functions in Lisp
Date: 
Message-ID: <m2649l7g2c.fsf@hermes.theari.com>
"S. Robert James" <············@gmail.com> writes:

> On Feb 28, 9:16 pm, "S. Robert James" <············@gmail.com> wrote:
>> Wow! Just learned how to inline some functions in Lisp and got a 10x
>> speed improvement.  (Prob' my Ruby experience encourages me to write
>> lots of short functions for clarity...)
>>
>> Where can I read more about this?
>> Does it make sense to inline builtins that I use repeatedly in loops
>> (mapcar some remove-if)?
>> What about lambda and curried functions? (I use them a lot too).
>
> similar point - I know Lisp supports static type declarations for
> additional speed, but I can't find docs, or an introduction to this,
> anywhere.  Any starters?

One way to start is with the HyperSpec.  I see your other reply so
I'll walk you through it a bit to help you find your way in the
future.

http://www.lispworks.com/documentation/HyperSpec/Front/index.htm

I usually start with the symbol index or the table of contents.  Let's
try the table of contents first:

1. Click contents
2. See that "Types and Classes" is chapter 4, click on it
3. Click Introduction
4. See that the fifth paragraph starts out "Declarations can be made
   about types using declare, proclaim, declaim, or the.  For more
   information about declarations, see Section 3.3 (Declarations)."
   Click the section 3.3 link
5. Read the section and see that "[t]he the special form provides a
   shorthand notation for making a local declaration about the type
   of the value of a given form."  Also read that "[l]ocal declarations
   can be embedded in executable code using declare" and that "[g]lobal
   declarations, or proclamations, are established by proclaim or
   declaim."  Try out the links for "the," "declare," "proclaim," and
   "declaim."
6. Notice that the documentation for DECLARE includes a list of permissible
   declaration specifiers (which term is itself linked to its definition,
   in case you want to know what it means).  Notice that one of them
   is "type."  Click on it.
7. See that TYPE can be a declaration, symbol, or glossary entry.  Click
   Declaration since that's what you want to learn about.
8. Notice that the first example for the TYPE declaration uses it to
   declare that two variables are of type FIXNUM.

Now, let's try it with the symbol index.  From the main HyperSpec page:

1. Click Symbol Index
2. Click T for "type" or D for "declaration" in the first list of
   letters.
3. Notice that "type" is listed under T and "declaration" and "declare"
   are both listed under D.
4. Discover that "declaration" is a dead end.
5. Read about "declare" and continue from step 6 above
6. In the alternative, read about "type" and continue from step 7 above
From: fireblade
Subject: Re: Inline functions in Lisp
Date: 
Message-ID: <1172759367.667497.68280@t69g2000cwt.googlegroups.com>
On Mar 1, 3:51 am, "S. Robert James" <············@gmail.com> wrote:
> On Feb 28, 9:16 pm, "S. Robert James" <············@gmail.com> wrote:
>
> > Wow! Just learned how to inline some functions in Lisp and got a 10x
> > speed improvement.  (Prob' my Ruby experience encourages me to write
> > lots of short functions for clarity...)
>
> > Where can I read more about this?
> > Does it make sense to inline builtins that I use repeatedly in loops
> > (mapcar some remove-if)?
> > What about lambda and curried functions? (I use them a lot too).
>
> similar point - I know Lisp supports static type declarations for
> additional speed, but I can't find docs, or an introduction to this,
> anywhere.  Any starters?

Hyperspec :
http://www.lispworks.com/documentation/lcl50/aug/aug-27.html
http://www.lispworks.com/documentation/HyperSpec/Body/d_type.htm

cltl2:
http://www.supelec.fr/docs/cltl/clm/node103.html

OnLisp:
Computation at Compile-Time

cheers
bobi
From: Pascal Costanza
Subject: Re: Inline functions in Lisp
Date: 
Message-ID: <54nehuF20trpoU2@mid.individual.net>
S. Robert James wrote:
> On Feb 28, 9:16 pm, "S. Robert James" <············@gmail.com> wrote:
>> Wow! Just learned how to inline some functions in Lisp and got a 10x
>> speed improvement.  (Prob' my Ruby experience encourages me to write
>> lots of short functions for clarity...)
>>
>> Where can I read more about this?
>> Does it make sense to inline builtins that I use repeatedly in loops
>> (mapcar some remove-if)?
>> What about lambda and curried functions? (I use them a lot too).
> 
> similar point - I know Lisp supports static type declarations for
> additional speed, but I can't find docs, or an introduction to this,
> anywhere.  Any starters?

You can find a list of good tutorials at 
http://www.cliki.net/Online%20Tutorial


Pascal

-- 
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: Pascal Costanza
Subject: Re: Inline functions in Lisp
Date: 
Message-ID: <54negbF20trpoU1@mid.individual.net>
S. Robert James wrote:
> Wow! Just learned how to inline some functions in Lisp and got a 10x
> speed improvement.  (Prob' my Ruby experience encourages me to write
> lots of short functions for clarity...)
> 
> Where can I read more about this?
> Does it make sense to inline builtins that I use repeatedly in loops
> (mapcar some remove-if)?
> What about lambda and curried functions? (I use them a lot too).

Don't get too carried away with efficiency. It's always tempting that 
when you learn a new tool that you want to use it everywhere. In the 
end, it doesn't matter that much, though. (For example, there is the 
80/20 rule of thumb, which says that 80% of the time is typically spent 
in 20% of the code. So if you optimize everything in your source code, 
you have wasted 80% of your own time.)

Anyway: You typically don't have to worry about built-ins. A Lisp 
program is not allowed to redefine them, so a Lisp compiler can assume 
they are fixed. In turn, this means that a Lisp compiler can do all 
kinds of optimizations with such built-ins. Some Lisp implementations 
are better in that regard than others. So sometimes, you can get speed 
improvements just by switching the CL implementation.

Closures (as created by lambda expressions) and curried functions are 
harder to optimize in general - the purpose is to pass this things 
around as first-class values, so you can't do a lot of inlining. But 
again: Don't let your programming style be dictated by low-level 
optimization issues. Common Lisp is not Ruby, but it's not C either.

With more experience, you will learn much more interesting ways to 
squeeze out performance from a program, for example by programming in a 
functional style and using memoization techniques, or by using compiler 
macros, etc.


Pascal

-- 
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: Rainer Joswig
Subject: Re: Inline functions in Lisp
Date: 
Message-ID: <joswig-4886D5.10040301032007@news-europe.giganews.com>
In article <·······················@v33g2000cwv.googlegroups.com>,
 "S. Robert James" <············@gmail.com> wrote:

> Wow! Just learned how to inline some functions in Lisp and got a 10x
> speed improvement.  (Prob' my Ruby experience encourages me to write
> lots of short functions for clarity...)
> 
> Where can I read more about this?
> Does it make sense to inline builtins that I use repeatedly in loops
> (mapcar some remove-if)?
> What about lambda and curried functions? (I use them a lot too).

Peter Norvig's book 'Paradigms of Artificial Intelligence Programming,
Case Studies in Common Lisp' explains the way how you
move from working software to faster working software.
It has a lot of information on optimization.
The book is REALLY good. It is definitely worth to read,
if you are interested in programming in Common Lisp.


It is also worth to mention that ANSI Common Lisp is a specification
of Common Lisp. Common Lisp as described can be implemented
very differently. It is possible to have relatively simple
implementations and also more complex batch compilers.
So a lot of optimizations have to be seen in context of your current
Lisp system. Take as an example inlining:

* the implementation might do no inlining, it can ignore declarations
  and support no inlining of built-ins
* the implementation may choose itself which function calls to inline
* the implementation may take the inline declarations and use them
  when certain optimization settings (SPACE, SPEED, COMPILATION-SPEED)
  are over a threshold.

and so on.

You can see this in the context of an Interpreter, a simple compiler,
the file compiler (COMPILE-FILE), ...

Allegro CL was (is?) known to inline functions, but to ignore the declarations.
Some other compilers do aggressive inlining, turning Lisp into
a more static language (like Lucid's optimizing compiler).

So you need to read the documentation chapter about the
compiler of your Lisp system to find out how it deals
with all this.

Did I mention PAIP? It's a good book. ;-)