guile-devel
[Top][All Lists]
Advanced

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

Re: Smart variables, dumb variables


From: Marius Vollmer
Subject: Re: Smart variables, dumb variables
Date: 15 Aug 2002 18:34:28 +0200
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2

address@hidden writes:

> oops, i forgot to mention goops. I was thinking of GOOPS generic 
> metods:
> |  
> |  (define-generic +)
> |  (define-method (+ (a <string>) (b <string>))
> |    (string-append a b))
> |  
> |  (+ 41 1)
> |  => 42
> |  
> |  (+ "Foo" "bar")
> |  => "Foobar"
> |  

In this example you are creating a new variable with the name "+" and
store a new generic in it.  That variable would not carry the
declarations that the variable named "+" in the guile-core module
carries.  So the compiler would treat your variable "+" like any other
and wont perform wrong optimizations on it.

Actually, the builtin '+' function is already a generic; you can add
methods to it.  The compiler can not go ahead and inline all of '+',
but it might inline the first type dispatch for fixnums, which should
be by far the most frequent type to occur.

Guile-lightning can do a bit in this direction:

    (define-asm-macro (scm-add res a b)
      (let ((l0 (gensym "ll"))
            (l1 (gensym "ll")))
        `(  (bmc ,l0 ,a (scm 0))
            (bmc ,l0 ,b (scm 0))
            (sub ,res ,a (scm 0))
            (boadd ,l0 ,res ,b)
            (b ,l1)
          ,l0
            (prepare 2)
            (pusharg ,b)
            (pusharg ,a)
            (finish (subr "scm_sum"))
            (retval ,res)
          ,l1)))

This code will check whether the arguments are fixnums , and when both
are, perform the addition inline, checking for overflow.  When one is
not a fixnum, or the addition overflows, we call scm_sum to do it
right.  scm_sum does also include the Goops method dispatch.

This is way faster than having to fetch a value from a variable and
invoke the machinery to call arbitrary functions.

> In the presence of a generic method system (i.e. function dispatch
> on the type signature of the arguments) this seems to be rather
> non-trivial, or do you want to propose to make guile a [statically]
> typed language ?

No, no, no. :-) As you can see above, types are still checked at
run-time, but without having to call out-of-line functions all the
time.

> > So my next proposal is to add declarations to variables... :-)
> 
> And a type system ;-)

No, that would be fun, sure, but not in this life...

-- 
GPG: D5D4E405 - 2F9B BCCC 8527 692A 04E3  331E FAF8 226A D5D4 E405




reply via email to

[Prev in Thread] Current Thread [Next in Thread]