[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] a couple of questions about foreign functions
From: |
Felix Winkelmann |
Subject: |
Re: [Chicken-users] a couple of questions about foreign functions |
Date: |
Thu, 22 Jan 2004 08:08:30 +0100 |
User-agent: |
Opera7.21/Win32 M2 build 3218 |
Am Wed, 21 Jan 2004 20:05:34 -0800 hat Valentyn Kamyshenko
<address@hidden> geschrieben:
Hello:
a couple of questions:
- in the chicken code, the Scheme strings seem to be never created on
the stack. Are there particular reasons for this? I mean, is
smth. wrong with the following piece of code:
(foreign-lambda* scheme-object () #<<EOF
char buf[13+sizeof(C_header)];
((C_SCHEME_BLOCK*)(void*)buf)->header=C_make_header(C_STRING_TYPE,13);
memcpy(((C_SCHEME_BLOCK*)(void*)buf)->data, "Hello, world!", 13);
return(buf);
EOF
)
When this function returns, `buf' will point into a dead stack area,
so the data is likely to be corrupted after the return.
- are there advantages/disadvantages of using foreign-lambda* instead
of ##core#primitive function in terms of performance or they are more
or less equivalent?
Good question. ##core#primitive has a little bit less overhead, here
some timing results on my 900Mzh Athlon:
#>
void foo(C_word c, C_word self, C_word k) { C_kontinue(k,
C_SCHEME_UNDEFINED); }
<#
(define foo (##core#primitive "foo"))
(define bar
(foreign-lambda* void () "") )
(define baz
(foreign-callback-lambda* void () "") )
(define (repeat n thunk)
(do ([i n (sub1 i)])
((zero? i))
(thunk) ) )
(print "foo:")
(time (repeat 10000000 foo))
(print "bar:")
(time (repeat 10000000 bar))
(print "baz:")
(time (repeat 10000000 baz))
Without optimizations:
foo:
4.316 seconds elapsed
0 seconds in (major) GC
0 mutations
9871 minor GCs
0 major GCs
bar:
4.787 seconds elapsed
0 seconds in (major) GC
0 mutations
8538 minor GCs
1 major GCs
baz:
14.611 seconds elapsed
1.882 seconds in (major) GC
10000000 mutations
1920 minor GCs
1814 major GCs
With -O2 -f:
foo:
0.881 seconds elapsed
0 seconds in (major) GC
0 mutations
2401 minor GCs
0 major GCs
bar:
1.051 seconds elapsed
0 seconds in (major) GC
0 mutations
2536 minor GCs
0 major GCs
baz:
10.305 seconds elapsed
1.89 seconds in (major) GC
10000000 mutations
3889 minor GCs
1813 major GCs
- can I call error routines from C code (that is, inside
foreign-lambda*, for example)?
If you mean error-routines that eventually invoke Scheme, then
you should use foreign-callback-lambda*. If you handle the
error in C (for example to display a message and exit), then
I see no problem.
cheers,
felix