• If I use inline c to compile a function, and then I attach it to to say, the NFCon, NFCoff, and NFCrx callbacks, what would happen?

    Would the callback see that it's a native function and execute it directly, or would it jump into the javascript interpreter just to jump back into the native code?

    In my experience with inlineC, the time required to call it from javascript is more than the time to execute it, so it's faster to just keep everything in javascript, but if inlineC attached to a native callback stays native, well...

    If this isn't an allowed behavior, maybe it wouldn't be a terrible idea to have a way to attach native c functions to callbacks directly that don't jump out to the javascript interpreter for at least some of the more time sensitive ones (like NFC)

    also, is it possible to output to the console from inline c at all?

  • Would the callback see that it's a native function and execute it directly, or would it jump into the javascript interpreter just to jump back into the native code?

    It is not clear what exactly you consider to be javascript interpreter vs native code as whole interpreter is native code but if you specifically mean parsing javascript source and interpreting it then there is no such thing if no source is involved. So if the callback is native method there is no javascript source to parse and interpret.

    In my experience with inlineC, the time required to call it from javascript is more than the time to execute it, so it's faster to just keep everything in javascript

    I fail to see the logic in the conclusion you made. Interpreting is slow so yes, it is slower (when compared to native code) before it gets to the point where it runs your native method and then it is slower again when it leaves it but the sum of total time is still smaller than having it all in javascript. There is no extra slowdown caused by calling native method compared to calling javascript method so you can only gain speed by using it.

    even smallest simplest native method gives speed boost, try e.g. uploading this

    var c = E.compiledC(`
    // int add(int, int)
    int add(int x,int y){
    return x+y;
    }
    `);
    function jsadd(x,y){return x+y;}
    
    function run(f){
    var x,y;
    for (y=0;y<64;y++) {
     for (x=0;x<64;x++) f(x,y);
    }
    }
    

    and then measure the difference of using add vs jsadd method

    >var d=Date();run(jsadd);d=Date().ms-d.ms;pri­nt("jsadd "+d);
    jsadd 2362.36572265625
    =undefined
    >var d=Date();run(c.add);d=Date().ms-d.ms;pri­nt("add "+d);
    add 1586.57836914062
    

    So just replacing method call of doing x+y in javascript loop saved you 800ms which is more than 1/3 of total time!

    also, is it possible to output to the console from inline c at all?

    Not easily. accessing any javascript variable (like console) and calling into it from native inline c code is tricky. In theory everything what can be done from "compiled" javascript code (see https://www.espruino.com/Compilation) should be possible in inline c code (so also calling methods of objects) but it won't be pretty.

  • Hi! In some cases, inline c can be called a bit more 'directly' - eg setWatch(..., {irq:true}).

    However for NFC and Bluetooth stuff it's not really possible - they are designed around a message queue, which pushes data from interrupt-land down to the message loop. By the time you're in the message loop and have seen that you're calling inline c, it's too late.

    Judging by your other posts about NFC/Inline C, I think you're at the point where you really want to be looking at compiling your own firmware to implement what you want - it's really not hard at all.

    is it possible to output to the console from inline c at all?

    In theory, yes. You have access to these functions: https://github.com/gfwilliams/EspruinoCo­mpiler/blob/master/src/utils.js#L74

    So you could do something like:

    var c = E.compiledC(`
    // int test(int)
    int test(int n){
      JsVar *p = jspGetNamedVariable("print");
      JsVar *s = jsvNewFromString("Hello world");
      jsvUnLock(jspeFunctionCall(p,0,0,false,1­,&s));
      jsvUnLock(s);
      jsvUnLock(p);
    }
    `);
    
    c.test(20);
    // prints 'Hello World'
    

    In a way it might be nice to have a bit of a standard library of functions (eg digitaWrite/etc) that can be called from Inline C

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Question about assigning compiled inline c functions to callbacks

Posted by Avatar for DanTheMan827 @DanTheMan827

Actions