pocmatos
2018-11-22 14:10:52

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?


samth
2018-11-22 14:54:14

There are no contracts but there is marshalling code


samth
2018-11-22 14:54:58

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


ryanc
2018-11-22 15:15:29

@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


pocmatos
2018-11-22 15:25:21

@samth thanks.


pocmatos
2018-11-22 15:26:00

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


samth
2018-11-22 15:34:23

Just masking I think


pocmatos
2018-11-22 19:17:15

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


samth
2018-11-22 19:18:09

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


pocmatos
2018-11-22 19:18:43

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


samth
2018-11-22 19:20:50

Not really


pocmatos
2018-11-22 21:46:03

Need to read the source implementation then… :slightly_smiling_face:


mflatt
2018-11-22 22:24:28

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.


oldsin
2018-11-22 22:39:50

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.


macocio
2018-11-22 22:40:45

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


oldsin
2018-11-22 22:47:43

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…


macocio
2018-11-22 22:50:28

@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


macocio
2018-11-22 22:52:25

Do you happen to know C++?


oldsin
2018-11-22 22:53:45

Sorry, no. =(


macocio
2018-11-22 22:54:01

What lang are you familiar with?


oldsin
2018-11-22 22:55:50

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?


macocio
2018-11-22 22:56:24

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


macocio
2018-11-22 22:56:51

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.


oldsin
2018-11-22 22:57:36

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


notjack
2018-11-22 23:45:28

java does have lexical closures, yes


samth
2018-11-23 00:54:27

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


krismicinski
2018-11-23 04:31:08

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)