Re: types and generic function performance

[email protected]
Newsgroups gmane.lisp.scheme.bigloo
Organization Inria
Message-ID <[email protected]>
Hi Bob,

> Hello and thanks for writing Bigloo!
> 
> I have a question about performance ... I wrote this program:
> 
> ;------------------------
> (module test
>         (main scm-main)
>         (export
>         (class TestClass)
>         ))
> 
> (define-generic (test-func obj) (print "GENERIC METHOD"))
> (define-method (test-func obj::TestClass) (print "TEST METHOD"))
> 
> (define (scm-main argv)
>   (let ((tc::TestClass (instantiate::TestClass)))
>     (test-func tc)))
> 
> ;------------------------
> 
> Which works as expected, producing the output "TEST METHOD", but if I look at the generated C code it looks like it is calling test-func dynamically even though I have specified the class type with tc::TestClass.
> 
>  
> 
> Is there some way to make such generic functions faster by calling the correct define-method function directly if the type is known?
> 
> Thanks,
You are correct in your observation. The compiler does not implement
this optimization. I have never conducted any experiment to check if
it would frequently apply in practice.

As it is, the calling a method instead of a regular function incurs a
couple of memory read + a indirect call. Your function TEST-FUNC is compiled
as follows:

(define-generic (test-func obj)
  (let ((test1086 (object? obj)))
    (if test1086
      (let ((method1053
              (unsafe (find-method obj (closure test-func)))))
        (funcall method1053 method1053 obj))
      (let ((fun1087 (generic-default (closure test-func))))
        (funcall fun1087 fun1087 obj)))))

and FIND-METHOD is defined by:

(define-inline (find-method obj generic)
   (let ((obj-class-num (object-class-num obj)))
      (method-array-ref generic (generic-method-array generic) obj-class-num)))


You can check this with bigloo -unsafe -O2 -fno-user-inlining -ast

Cheers,

-- 
Manuel
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.