[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: what is the best language for go: Re: [computer-go] Scoreestimating



Don Dailey <drd@xxxxxxxxxxxxxxxxx> writes:

> Hi Peter,
>
> I'll take a closer look at common lisp.  Of course I am biased towards
> really fast execution, approaching assembler.  But of course I want it
> all, a really  high level language that is super  fast too.  It's hard
> to get both I'm afraid.
>
> Do you  believe that well written common  lisp can come close  to C in
> performance  or is  your pitch  more that  it is  a much  higher level
> language and with native code compilation at least is reasonably fast?

Well written Common Lisp can come often close to C if that's what you
want. The trick is that you need to use declarations to give the
compiler information it can use to generate optimal code. The
advantage of this approach is that you only need to bother putting
declarations into the hotspots in your code.

To take a simple example suppose I write this trivial function that
adds two numbers:

  (defun add (x y) (+ x y))

I can compile that function and then use the function DISASSEMBLE to
see what machine code the compiler generated:

  CL-USER> (disassemble 'add)
  ;; disassembly of #<Function ADD>
  ;; formals: X Y

  ;; code start: #x7179f5ac:
     0: 55          pushl	ebp
     1: 8b ec       movl	ebp,esp
     3: 56          pushl	esi
     4: 83 ec 24    subl	esp,$36
     7: 83 f9 02    cmpl	ecx,$2
    10: 74 02       jz	14
    12: cd 61       int	$97       ; EXCL::TRAP-ARGERR
    14: 80 7f 97 00 cmpb	[edi-105],$0  ; SYS::C_INTERRUPT
    18: 74 02       jz	22
    20: cd 64       int	$100      ; EXCL::TRAP-SIGNAL-HIT
    22: 8b d8       movl	ebx,eax
    24: 0b da       orl	ebx,edx
    26: f6 c3 03    testb	bl,$3
    29: 75 0e       jnz	45
    31: 8b d8       movl	ebx,eax
    33: 03 da       addl	ebx,edx
    35: 70 08       jo	45
    37: 8b c3       movl	eax,ebx
    39: f8          clc
    40: c9          leave
    41: 8b 75 fc    movl	esi,[ebp-4]
    44: c3          ret
    45: 8b 5f 93    movl	ebx,[edi-109] ; EXCL::+_2OP
    48: ff 57 27    call	*[edi+39]      ; SYS::TRAMP-TWO
    51: eb f3       jmp	40
    53: 90          nop
  ; No value

That looks pretty hairy for just adding two numbers together. But
that's because Common Lisp is a high-level language. It supports
arbitrary precision integers and rationals as well as floating point,
and complex numbers. So (+ x y) in Common Lisp is doing a lot more
than x + y in C. And by default the compiler I'm using (Allegro 6.2
from Franz) adds interrupt checking into every function so I can
control-C execution of my program.

However if I happen to know that everywhere I use the function ADD I
am actually passing integers that can be represented in a machine word
and whose sum can also be represented in a machine word without
overflowing and I don't care about checking interrupts in htis
function then I can declare the arguments to be "fixnums" and tell the
compiler to trade speed for safety (where "safety" is stuff like
argument checking that only matters if your program has bugs in it)
when compiling this one function:

  (defun add (x y)
    (declare (optimize (speed 3) (safety 0))
             (fixnum x y))
    (+ x y))

Now DISASSEMBLE reveals nice compact code:

  CL-USER> (disassemble 'add)
  ;; disassembly of #<Function ADD>
  ;; formals: X Y

  ;; code start: #x717a3c24:
     0: 03 c2       addl	eax,edx
     2: f8          clc
     3: 8b 75 fc    movl	esi,[ebp-4]
     6: c3          ret
     7: 90          nop
  ; No value

This is obviously a trivial example. But it shows the basic idea. You
can write your program using general purpose data types and quickly
get a working version that allows you to tune your algorithm (which
is, of course, where the main performance gains are to be had.) Then
you start profiling and can use declarations to tune the bits where it
really matters. But since you get to do most of your work in a nice
high-level language you have more time to spend on nitty-gritty
optimization of the bits that need it.

-Peter

-- 
Peter Seibel                                      peter@xxxxxxxxxxxxxxxxx

         Lisp is the red pill. -- John Fraser, comp.lang.lisp
_______________________________________________
computer-go mailing list
computer-go@xxxxxxxxxxxxxxxxx
http://www.computer-go.org/mailman/listinfo/computer-go/