Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Run Perl 5 in the Browser!

by haukex (Archbishop)
on Aug 19, 2018 at 11:06 UTC ( [id://1220615]=note: print w/replies, xml ) Need Help??


in reply to Re: Run Perl 5 in the Browser!
in thread Run Perl 5 in the Browser!

not sure how you solve this, does asm.js also cover the regex engine?

I'm compiling a whole perl binary to WebAssembly, including the regex engine and everything. I haven't gotten very far on porting Perl's test suite yet, because it makes use of some of the stuff that isn't supported (e.g. qx and signals), but the tests I was able to run have looked good so far.

patching B::Deparse to emit native JS should be possible

That's an interesting idea, and perhaps something I could think about is how to optimize code like the example I showed on the website:

js('document')->getElementById('my_button') ->addEventListener('click', sub { js('window')->alert("You clicked the button!"); } );

Running that invokes the JS/Perl glue three times (five if the callback is called), which isn't super efficient at the moment because it does a lot with strings and eval. One possible way to optimize that might be if there was a way to convert at least the first few method calls to

js('document.getElementById("my_button").addEventListener("click",func +tion(){...})');

Replies are listed 'Best First'.
Re^3: Run Perl 5 in the Browser!
by LanX (Saint) on Aug 19, 2018 at 11:42 UTC
    I'm open for discussions, you might also be interested in the work of Flavio Glock and his perlito.

    update

    A general approach for performance enhancements could be to run Perl your way and to replace time consuming inner functions with direct JS translations.(kind of JIT if you want).

    From the Perl perspective these functions would have been monkey patched with XS code.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      A general approach for performance enhancements could be to run Perl your way and to replace time consuming inner functions with direct JS translations.(kind of JIT if you want).

      Yep, although I think it would only speed things up where the Perl<->JavaScript interface is involved, I suspect everything that's pure Perl may even be as fast or even faster than regular JavaScript, since WebAssembly should be faster than JavaScript, and it's a WebAssembly perl binary. OTOH, at the moment I just want to use this for UI's, so as long as button clicks etc. don't cause 100% CPU or noticeable delays, I'm not going to worry too much about performance just yet.

      From the Perl perspective these functions would have been monkey patched with XS code.

      Yes, the interesting thing there is that "XS" code can include JavaScript code ;-) See WebPerl.xs, a mishmash of C, JS, and XS... (and my first real use of XS too!)

        > I suspect everything that's pure Perl is may even be as fast or even faster than regular JavaScript, since WebAssembly should be faster than JavaScript, and it's a WebAssembly perl binary.

        Really? Is WASM JIT-optimized?

        I mean if a Perl Op-Code deals with Scalars will it automatically be optimized to integer if some tracker finds that all calls to the surrounding function always result to paths using Int?

        That would surprise me...

        > OTOH, at the moment I just want to use this for UI's, so as long as button clicks etc. don't cause 100% CPU or noticeable delays,

        Don't you think that for little UI callbacks handling some AJAX compiling the whole Perl core to 4 MB WASM is kind of overkill?

        Gimme some example Perl code for a start and I'll try to "B::Deparse" it into working JS.

        > monkey patched with XS code.

        Actually I'm wondering why this is not generally used for JITing Perl. (Probably some weird side effects or performance brakes of calling subs.)

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      time consuming inner functions

      BTW, just another thought: As I said, my original intention with this was to be able to build user interfaces, so there's always the approach of moving the heavy work into a different process, in this case that would be the web server - even if that's running on the same machine, it'd still give a speedup for the heavy lifting.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1220615]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-26 06:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found