[racket] slow FFI?

From: Denys Rtveliashvili (rtvd at icloud.com)
Date: Fri Nov 30 05:22:08 EST 2012

Hello,

I am new to Racket. Currently I am evaluating it to find out if it works good enough for my work.

One of important aspects for me is efficiency of Foreign Function Interface.
Unfortunately, it seems that FFI is quite slow.

Please can you tell me if this is a bug in my code or FFI itself is slow? And if it is indeed slow, is there a way of improving it?

Here is the code I have:

-- test.c --

void do_test(void)
{}

-- test.rkt --

#lang racket/base

(require
    ffi/unsafe
    ffi/unsafe/define)

(define-ffi-definer define-t (ffi-lib "libtest"))

(define-t do_test (_fun -> _void))

(define (do_benchmark)
    (time (for ([i (in-range 1000000)])
        (do_test)))
)

(for ([i (in-range 10)])
    (do_benchmark))

-- Makefile --

libtest.so: test.o
    gcc -fPIC -shared -pthread -o libtest.so test.o

test.o: test.c
    gcc test.c -fPIC -shared -pthread -c -O2 -o test.o

clean:
    rm -f test.o libtest.so

---------
Running the test suggests that a call to "do_test" costs about 150 nanoseconds. I would expect something not larger than 5 nanoseconds. A test where C program calls this function shows the call costs 3 nanoseconds.

P.S. In real life I would want to pass data to the function, most likely in form of a C-structure.

P.P.S. While 150 nanoseconds may not seem like much, it easily becomes a problem when many of such calls need to be done.

With kind regards,
Denys Rtveliashvili
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20121130/9c053020/attachment.html>

Posted on the users mailing list.