in reply to Re^5: Run Perl 5 in the Browser! (JIT Compilation)
in thread Run Perl 5 in the Browser!

Is WASM JIT-optimized?

I'm not an expert, but WebAssembly really is meant for running stuff much faster than JavaScript. Copy-and-pasting some quotes from https://webassembly.org/ and https://en.wikipedia.org/wiki/WebAssembly (see also https://developer.mozilla.org/en-US/docs/WebAssembly):

WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable target for compilation of high-level languages like C/C++/Rust, enabling deployment on the web for client and server applications.

It is meant to enable executing code nearly as fast as running native machine code.

The Wasm stack machine is designed to be encoded in a size- and load-time-efficient binary format. WebAssembly aims to execute at native speed by taking advantage of common hardware capabilities available on a wide range of platforms.

And from https://bellard.org/jslinux/ (the original author of QEMU):

... I converted the JSLinux asm.js code to C and converted it back to Javascript with emscripten ! With a careful tuning, the new version is now faster than the hand-coded asm.js version.

Here are some more virtual machines in the browser (although I'm not sure if they're WebAssembly, I just think it's cool*): https://copy.sh/v86/

I haven't tested yet whether the 2-3x slowdown I measured compared to native code might be due to the debug assertions that I currently still have enabled.

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?

It sure is! But it's Perl! :-D

Seriously though, I think the overhead of loading WebPerl is fine for my use case: a single-page, long-running application that does a lot of data exchange with the server. Writing all the data-handling code in JavaScript was getting tiresome, something that Perl can really help with.

If a web application consists of a lot of single pages, loading WebPerl into each of them is probably not going to be particularly performant.

(* Update: In fact, I figured that if I can't get perl compiled with Emscripten, in the worst case I can boot a VM in the browser, run a perl in there, and communicate with that, and yes I'm aware of how insane that is ;-D )

Replies are listed 'Best First'.
Re^7: Run Perl 5 in the Browser!
by LanX (Saint) on Aug 22, 2018 at 22:01 UTC
    > It is meant to enable executing code nearly as fast as running native machine code.

    This means Perl's op-codes are executed at "native speed".

    But JS code is normally far better optimized by using JIT.

    The same algorithm in JS and Perl will normally run (much) faster in JS.

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

      This means Perl's op-codes are executed at "native speed". But JS code is normally far better optimized by using JIT. The same algorithm in JS and Perl will normally run (much) faster in JS.

      True, with Perl there's the additional layer of the execution of opcodes. I don't know much about JavaScript JIT, but if it's compiling to native machine code (and not something internal like WebAssembly), then yes, the same algorithm in JS should be faster than WebPerl.

        It's not that much about native code but reducing dynamic typing to the most "probable" static types.

        A scalar in Perl is a container which can be internally an integer or a float, a string, a ref, an object ... etc.

        Very similar in JS.

        Each time you have an operation on two dynamic data types, the VM needs to find out how to combine the cases for the two types.

        But if in the dynamic process you found out that the variables involved "always" (or mostly) have the same type, you can JIT compile to optimized code.

        In other words: The JIT compiler can treat the code as if the variables where statically typed.

        see https://hacks.mozilla.org/2017/02/a-crash-course-in-just-in-time-jit-compilers/ for a nice explanation.

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