[plt-scheme] About extensions, inlining, fixnums and performance

From: Fabien Delorme (fabien.delorme at gmail.com)
Date: Tue Mar 25 08:55:32 EDT 2008

Hello everyone.

I'm currently having a little problem with inlining and mzscheme
extensions. I'm using version 360.

I wrote an extension offering numerical operations for fixnums : fix+,
fix-, ... The goal is to have the fastest possible code when dealing
with numerical computations. In particular, no type or arity checking
is done when the function is called, as these functions are only ran
in safe places where checks have *already* been performed
(automatically generated code actually). In these contexts, there is
no good reason for using +, - et al. : they perform time-consuming
checkings.

For example, here is the code for fix= :

static Scheme_Object * sa_eq_fixnum(int argc, Scheme_Object ** argv)
{
        int i, cur, first;
        first = (int) (argv[0]);

        for (i = 1 ; i < argc ; i++){
                cur = (int) (argv[i]);
                if (cur != first)
                        return scheme_false;
        }

  return scheme_true;
}

Now for the micro-benchmarks. Here are the results I get :

(time (do ((i 0 (+ i 1))) ((= i 100000000)) (+ i 2)))
cpu time: 16189 real time: 16254 gc time: 0

(time (do ((i 0 (fix+ i 1))) ((fix= i 100000000)) (fix+ i 2)))
cpu time: 10700 real time: 10707 gc time: 0

My fixn operations are faster, as expected. Well, there's a way to
speed things up, I just have to use :

(require mzscheme)
(time (do ((i 0 (+ i 1))) ((= i 100000000)) (+ i 2)))
cpu time: 660 real time: 658 gc time: 0

Wow. If I understood, requiring mzscheme informs the compiler +, - et
al. won't be redefined, so he can inline them. But what I don't get is
: why doesn't it work with *my* fixn operations ? They are in a module
too, and can't be redefined either :

(set! fix+ +)
stdin::297: set!: cannot mutate module-required variable in: fix+

What didn't I understand there ? What did I forget ? How can I get my
fix operations taken into consideration as regular operations are ?

Thanks in advance !

Fabien


Posted on the users mailing list.