
I can implement an FFI interface to a C function and I am doing so for a small hot function (called millions of times) and calling the function from Racket into C is not much faster than calling the Racket implementation. Are there are contracts added in a call created with ffi-obj
(or using an ffi definer)? What’s my best bet in understanding exactly how this call happens?

There are no contracts but there is marshalling code

Your best bet might be to read the libffi documentation or the paper about the ffi implementation by Barzilay, mentioned in the docs

@mflatt What is the (new?) right way to add a primitive to Racket? It seems that cstartup.inc is no longer checked in, so the schminc.h comments are a little out of date. I’ve updated EXPECTED_PRIM_COUNT
and set USE_COMPILED_STARTUP
to 0. I also increased the version number in schvers.h. Then make
succeeds but make cs
fails with the following error: [….]/racket/bin/racketcs -G [….]/racketcs/racket/src/build -N raco -l- raco setup -D attempt to reference undefined variable hash->linklet-bundle

@samth thanks.

The function takes an int and return an int. in racket land they are fixnums. What kind of marshling is done?

Just masking I think

@samth Interesting, I will have to dig deeper… because it feels like something else is going on.

Most likely it’s the code generated to make the C to Racket call

Is that described in the paper you mentioned above? By Eli?

Not really

Need to read the source implementation then… :slightly_smiling_face:

To add a primitive to RacketCS, to you need to add to a suitable file in “src/cs/primitive”. But hash->linklet-bundle
isn’t the primitive you’re trying to add… Something else has gone wrong, probably related to the fact that I just moved that function to the expander.

Hi! do you guys know why the next
here can be accumulated? I thought every time the count
is being called, the let struct will rebind next
to 0
, but it don’t seems like this.

@oldsin This is a “let over lambda”, observe that it’s not (define (count) ...
but rather (define count ...)

Yes I see that. What I don’t understand is that why this form will keep the value of next
using let
. It’s a bit hard to describe…

@oldsin the value is associated with the lambda and it gets to mutate it each time… maybe it’s easier to explain in another language

Do you happen to know C++?

Sorry, no. =(

What lang are you familiar with?

Java; I just checked googled ‘let over lambda’, is that because racket will keep the ‘closures’, so that when evaluating a lambda expression it will reference the previous environment?

Right. I’m not sure if the new Java has lexical closures so I can’t give an example.

But yes, whatever is used inside the lambda
, if it’s not a parameter, it will be “copied” into the lambda’s variable space, so to speak.

Thanks! You are really giving me generous support! :smiley:

java does have lexical closures, yes

@pocmatos really I recommend reading the libffi docs and the paper together

but Java’s lexical closures are degenerate in the sense that they’re essentially flat closures, and variables captured inside have to be final
so that they can be compiled into classes. (As many of you all know)