2022年2月5日土曜日

Output js and WebAssembly(wasm) in V language. English Version.

Japanese original Version.

https://blog.kyubuns.dev/entry/2020/07/12/150546

vlang

V, the language I love for its fast compilation, no nulls, immutable variables by default, and pure functions by default.


The V language now supports outputting js and wasm!

However, the output method is not on the documentation yet.

But the output method is not documented yet, but will be soon.


I'll show you how to build it based on information I gathered from the vlang discord.


Sample code

We'll compile the official code to enumerate the Fibonacci sequence.


fibonacci.v


fn fib(a int, b int) {

    val := a + b

    println(val)

    if val < 1000 {

        fib(b, val)

    }

}


fn main() {

    areas := ['game', 'web', 'tools', 'science', 'systems', 'embedded', 'drivers', 'GUI', 'mobile']

    for area in areas {

        println('Hello, $area developers!')

    }

    fib(0, 1)

}

Try to run it normally

v fibonacci.v


Compilation is as fast as this. (All the following are tested on MBP2018.)

0.25s user 0.04s system 97% cpu 0.301 total


. /fibonacci


Execution is about this fast.

0.00s user 0.00s system 56% cpu 0.004 total


js

v -b js -w -o fibonacci fibonacci.v


This will generate fibonacci.js.

It's ridiculously fast: 0.00s user 0.00s system 73% cpu 0.008 total


> node fibonacci.js

Hello, game developers!

Hello, web developers!

Hello, tools developers!

Hello, science developers!

Hello, systems developers!

Hello, embedded developers!

Hello, drivers developers!

Hello, GUI developers!

Hello, mobile developers!

1

2

3

5

Eight.

13

21

34

55

89

144

233

377

987

987

1597

0.04s user 0.01s system 91% cpu 0.060 total

The number of trials is about two orders of magnitude too short to test the execution speed, so this is just for reference.

But isn't this faster than building and executing with V as is?


The converted code looks like this.


(There are about 100 lines of builtin definition in front of it.)


/** @namespace main */

const main = (function () {

    /**

     * @function

     * @param {number} a

     * @param {number} b

     * @returns {void}

    /** * @function * @param {number}

    function fib(a, b) {

        /** @type {number} */

        const val = a + b;

        builtin.println(val);

        if (val < 1000) {

            fib(b, val);

        }


    }


    /* program entry point */

    (function() {

        /** @type {string[]} */

        const areas = ["game", "web", "tools", "science", "systems", "embedded", "drivers", "GUI", "mobile"];

        for (let _tmp1 = 0; _tmp1 < areas.length; ++_tmp1) {

            const area = areas[_tmp1];

            builtin.println(`Hello, ${area} developers!`);

        }


        fib(0, 1);

    })();


    /* module exports */

    return {};

})();

You can see that the conversion is very straightforward.


wasm

v -o fibonacci.c fibonacci.v

emcc fibonacci.c -s WASM=1 -o hello.html

No, v is just converting to c!

Well, that's the thing, v->c is as fast as js, but

c->wasm takes about 2.15s user 0.72s system 224% cpu 1.277 total.


The fibonacci.c file is a whopping 9559 lines long.

I guess converting to C is not for compile speed, but for execution speed.

0 コメント:

コメントを投稿