• 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.

About