<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ivan Enderlin on Medium]]></title>
        <description><![CDATA[Stories by Ivan Enderlin on Medium]]></description>
        <link>https://medium.com/@hywan?source=rss-d1a15a1c8914------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*xJwBHp5mcALYl454-Z96DQ.jpeg</url>
            <title>Stories by Ivan Enderlin on Medium</title>
            <link>https://medium.com/@hywan?source=rss-d1a15a1c8914------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 10:35:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hywan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Announcing the first Java library to run WebAssembly: Wasmer JNI]]></title>
            <link>https://blog.wasmer.io/announcing-the-first-java-library-to-run-webassembly-wasmer-jni-89e319d2ac7c?source=rss-d1a15a1c8914------2</link>
            <guid isPermaLink="false">https://medium.com/p/89e319d2ac7c</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[webassembly]]></category>
            <category><![CDATA[computers]]></category>
            <dc:creator><![CDATA[Ivan Enderlin]]></dc:creator>
            <pubDate>Wed, 13 May 2020 18:15:22 GMT</pubDate>
            <atom:updated>2020-05-18T12:14:35.168Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OmhVSxTipIsddbGi-XM2jQ.jpeg" /></figure><p><a href="https://webassembly.org/">WebAssembly</a> is a portable binary format. That means the same file can run anywhere.</p><blockquote>To uphold this bold statement, each language, platform and system must be able to run WebAssembly — as fast and safely as possible.</blockquote><p>People who are familiar with Wasmer are used to this kind of announcement! Wasmer is written in Rust, and comes with an additional native C API. But you can use it in a lot of other languages. After having announced libraries to use Wasmer, and thus WebAssembly, in:</p><ul><li><a href="https://github.com/wasmerio/php-ext-wasm"><strong>PHP</strong> with the </a><a href="https://github.com/wasmerio/php-ext-wasm">ext/wasm extension</a>,</li><li><a href="https://github.com/wasmerio/python-ext-wasm"><strong>Python</strong> with the </a><a href="https://github.com/wasmerio/python-ext-wasm">wasmer library</a>,</li><li><a href="https://github.com/wasmerio/ruby-ext-wasm"><strong>Ruby</strong> with the </a><a href="https://github.com/wasmerio/ruby-ext-wasm">wasmer library</a>,</li><li><a href="https://github.com/wasmerio/go-ext-wasm"><strong>Go</strong> with the </a><a href="https://github.com/wasmerio/go-ext-wasm">wasmer library</a> (see <a href="https://medium.com/wasmer/announcing-the-fastest-webassembly-runtime-for-go-wasmer-19832d77c050">Announcing the fastest WebAssembly runtime for Go: </a><a href="https://medium.com/wasmer/announcing-the-fastest-webassembly-runtime-for-go-wasmer-19832d77c050">wasmer</a>), and even</li><li><a href="https://github.com/wasmerio/postgres-ext-wasm"><strong>Postgres</strong> with the </a><a href="https://github.com/wasmerio/postgres-ext-wasm">wasmer library</a> (see <a href="https://medium.com/wasmer/announcing-the-first-postgres-extension-to-run-webassembly-561af2cfcb1">Announcing the first Postgres extension to run WebAssembly</a>),</li><li>and many other contributions in <a href="https://github.com/migueldeicaza/WasmerSharp">.NET/C#</a>, <a href="https://github.com/dirkschumacher/wasmr">R</a> and <a href="https://github.com/tessi/wasmex">Elixir</a>…</li></ul><p>…we are jazzed to announce that <strong>Wasmer has now landed in Java</strong>!</p><p><a href="https://github.com/wasmerio/java-ext-wasm">wasmerio/java-ext-wasm</a></p><p>Let’s discover the Wasmer JNI library together.</p><h3>Installation</h3><p>The Wasmer JNI (<em>Java Native Interface</em>) library is based on the <a href="https://github.com/wasmerio/wasmer">Wasmer runtime</a>, which is written in <a href="https://www.rust-lang.org/">Rust</a>, and is compiled to a shared library. For your convenience, we produce one JAR (<em>Java Archive</em>) per architecture and platform. By now, the following are supported, consistently tested, and pre-packaged (available in <a href="https://bintray.com/wasmer/wasmer-jni/wasmer-jni">Bintray</a> and <a href="https://github.com/wasmerio/java-ext-wasm/releases">Github Releases</a>):</p><ul><li>amd64-darwin for macOS, x86 64bits,</li><li>amd64-linux for Linux, x86 64 bits,</li><li>amd64-windows for Windows, x86 64 bits.</li></ul><p>More architectures and more platforms will be added in the near future. If you need a specific one, <a href="https://github.com/wasmerio/java-ext-wasm/issues/new?assignees=&amp;labels=%F0%9F%8E%89+enhancement&amp;template=---feature-request.md&amp;title=">feel free to ask</a>! However, it is possible to <a href="https://github.com/wasmerio/java-ext-wasm#development">produce your own JAR for your own platform and architecture</a>.</p><p>The JAR files are named as follows: wasmer-jni-$(architecture)-$(os)-$(version).jar. Thus, to include Wasmer JNI as a dependency of your project (assuming you use <a href="http://gradle.org/">Gradle</a>), write for instance:</p><pre>dependencies {<br>    implementation &quot;org.wasmer:wasmer-jni-amd64-linux:0.2.0&quot;<br>}</pre><p>JAR are hosted on the Bintray/JCenter repository under the <a href="https://bintray.com/wasmer/wasmer-jni/wasmer-jni">wasmer-jni</a> project. They are also attached to our <a href="https://github.com/wasmerio/java-ext-wasm/releases">Github releases as assets</a>.</p><h3>Calling a WebAssembly function from Java</h3><p>As usual, let’s start with a simple Rust program that we will compile to WebAssembly, and then execute from Java.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1596c119675ed0347ffdf48aca2a300e/href">https://medium.com/media/1596c119675ed0347ffdf48aca2a300e/href</a></iframe><p>After compilation to WebAssembly, we get a file like <a href="https://github.com/wasmerio/java-ext-wasm/raw/master/examples/simple.wasm">this one</a>, named simple.wasm.</p><p>The following Java program executes the sum exported function by passing 5 and 37 as arguments:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/732a961416cf3424deb1d970e3465b91/href">https://medium.com/media/732a961416cf3424deb1d970e3465b91/href</a></iframe><p>Great! We have successfully executed a Rust program, compiled to WebAssembly, in Java. As you can see, it is pretty straightforward. The API is very similar to the standard JavaScript API, or the other API we have designed for PHP, Python, Go, Ruby etc.</p><p>The assiduous reader might have noticed the [0] in .apply(5, 37)[0] pattern. A WebAssembly function can return zero to many values, and in this case, we are reading the first one.</p><blockquote>Note: Java values passed to WebAssembly exported functions are automatically downcasted to WebAssembly values. Types are inferred at runtime, and casting is done automatically. Thus, a WebAssembly function acts as any regular Java function.</blockquote><p>Technically, an exported function is a <em>functional interface</em> as defined by the Java Language Specification (i.e. it is a <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/FunctionalInterface.html">FunctionalInterface</a>). Thus, it is possible to write the following code where sum is an actual function (of kind org.wasmer.exports.Function):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bef18991d0ff4ac90600d658bdf42d86/href">https://medium.com/media/bef18991d0ff4ac90600d658bdf42d86/href</a></iframe><p>But a WebAssembly module not only exports functions, it also exports memory.</p><h3>Reading the memory</h3><p>A WebAssembly instance has one or more linear memories, a contiguous and byte-addressable range of memory spanning from offset 0 and extending up to a varying memory size, represented by the org.wasmer.Memory class. Let’s see how to use it. Consider the following Rust program:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/db66d69f2fb57910540487b2fdafafe8/href">https://medium.com/media/db66d69f2fb57910540487b2fdafafe8/href</a></iframe><p>The return_hello function returns a pointer to the statically allocated string. The string exists in the linear memory of the WebAssembly module. It is then possible to read it in Java:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/717614b91465d4c7d17e9e0c0dcb57cb/href">https://medium.com/media/717614b91465d4c7d17e9e0c0dcb57cb/href</a></iframe><p>As we can see, the Memory API provides a buffer method. It returns a <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html"><em>direct</em> byte buffer</a> (of kind java.nio.ByteBuffer) view of the memory. It’s a standard API for any Java developer. We think it’s best to not reinvent the wheel and use standard API as much as possible.</p><p>The WebAssembly memory is dissociated from the JVM memory, and thus from the garbage collector.</p><blockquote>You can read <a href="https://github.com/wasmerio/java-ext-wasm/blob/master/examples/GreetExample.java">the Greet Example</a> to see a more in-depth usage of the Memory API.</blockquote><h3>More documentation</h3><p>The project comes with a Makefile. The make javadoc command will generate a traditional local Javadoc for you, in the build/docs/javadoc/index.html file.</p><p>In addition, the project’s README.md file has an <a href="https://github.com/wasmerio/java-ext-wasm#api-of-the-wasmer-library">API of the </a><a href="https://github.com/wasmerio/java-ext-wasm#api-of-the-wasmer-library">wasmer library Section</a>.</p><p>Finally, the project comes with <a href="https://github.com/wasmerio/java-ext-wasm/tree/master/examples">a set of examples</a>. Use the make run-example EXAMPLE=Simple to run the SimpleExample.java example for instance.</p><h3>Performance</h3><p>WebAssembly aims at being safe, but also fast. Since Wasmer JNI is the <em>first</em> Java library to execute WebAssembly, we can’t compare to prior works in the Java ecosystem. However, you might know that Wasmer comes with 3 backends: Singlepass, Cranelift and LLVM. We’ve even written an article about it: <a href="https://medium.com/wasmer/a-webassembly-compiler-tale-9ef37aa3b537">A WebAssembly Compiler tale</a>. The Wasmer JNI library uses the Cranelift backend for the moment, which offers the best compromise between compilation-time and execution-time.</p><h3>Credits</h3><p>Asami (<a href="https://twitter.com/d0iasm">d0iasm</a> on Twitter) has improved this project during its internship at Wasmer under my guidance. She finished the internship before the release of the Wasmer JNI project, but she deserves credits for pushing the project forward! Good work Asami!</p><p>This is an opportunity to remind everyone that we hire anywhere in the world. Asami was working from Japan while I am working from Switzerland, and the rest of the team is from US, Spain, China etc. Feel free to contact me (<a href="https://twitter.com/mnt_io">@mnt_io</a> or <a href="https://twitter.com/syrusakbary">@syrusakbary</a> on Twitter) if you want to join us on this big adventure!</p><h3>Conclusion</h3><p>Wasmer JNI is a library to execute WebAssembly directly in Java. It embeds the WebAssembly runtime <a href="https://github.com/wasmerio/wasmer">Wasmer</a>. The first releases provide the core API with Module, Instance, and Memory. It comes pre-packaged as a JAR, one per architecture and per platform.</p><p><a href="https://github.com/wasmerio/java-ext-wasm">wasmerio/java-ext-wasm</a></p><p>The source code is open and hosted on Github at <a href="https://github.com/wasmerio/java-ext-wasm">https://github.com/wasmerio/java-ext-wasm</a>. We are constantly improving the project, so if you have feedback, issues, or feature requests please open an issue in the repository, or reach us on Twitter at <a href="https://twitter.com/wasmerio">@wasmerio</a> or <a href="https://twitter.com/mnt_io">@mnt_io</a>.</p><p>We look forward to see what you build with this!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=89e319d2ac7c" width="1" height="1" alt=""><hr><p><a href="https://blog.wasmer.io/announcing-the-first-java-library-to-run-webassembly-wasmer-jni-89e319d2ac7c">Announcing the first Java library to run WebAssembly: Wasmer JNI</a> was originally published in <a href="https://blog.wasmer.io/">Wasmer</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Announcing the first Postgres extension to run WebAssembly]]></title>
            <link>https://blog.wasmer.io/announcing-the-first-postgres-extension-to-run-webassembly-561af2cfcb1?source=rss-d1a15a1c8914------2</link>
            <guid isPermaLink="false">https://medium.com/p/561af2cfcb1</guid>
            <category><![CDATA[wasmer]]></category>
            <category><![CDATA[webassembly]]></category>
            <category><![CDATA[postgres]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[Ivan Enderlin]]></dc:creator>
            <pubDate>Thu, 29 Aug 2019 12:44:59 GMT</pubDate>
            <atom:updated>2019-08-30T07:56:27.850Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nTlfw6eJBe6oi3oVgQylfg.jpeg" /><figcaption>Elephant. <a href="https://www.pexels.com/photo/gray-elephant-1054666/">Source of the photo</a>.</figcaption></figure><p>WebAssembly is a portable binary format. That means the same program can run anywhere.</p><blockquote>To uphold this bold statement, each language, platform and system must be able to run WebAssembly — as fast and safely as possible.</blockquote><p>Let’s say it again. <a href="https://github.com/wasmerio/wasmer">Wasmer</a> is a WebAssembly runtime. We have successfully embedded the runtime in other languages:</p><ul><li>In Rust, as it is written in Rust</li><li>Using <a href="https://github.com/wasmerio/wasmer/tree/master/lib/runtime-c-api">C and C++ bindings</a></li><li>In PHP, using <a href="https://github.com/wasmerio/php-ext-wasm">php-ext-wasm</a></li><li>In Python, using <a href="https://github.com/wasmerio/python-ext-wasm">python-ext-wasm</a> — <a href="https://pypi.org/project/wasmer/">wasmer package on PyPI</a></li><li>In Ruby, using <a href="https://github.com/wasmerio/ruby-ext-wasm">ruby-ext-wasm</a> — <a href="https://rubygems.org/gems/wasmer">wasmer gem on RubyGems</a></li><li>In Go, using <a href="https://github.com/wasmerio/go-ext-wasm">go-ext-wasm</a> — see <a href="https://medium.com/wasmer/announcing-the-fastest-webassembly-runtime-for-go-wasmer-19832d77c050">the announcement</a>.</li></ul><p>The community has also embedded Wasmer in awesome projects:</p><ul><li>.NET/C#, using <a href="https://github.com/migueldeicaza/WasmerSharp">WasmerSharp</a></li><li>R, using <a href="https://github.com/dirkschumacher/wasmr">Wasmr</a>.</li></ul><p><strong>It is now time to continue the story and to hang around… </strong><a href="https://www.postgresql.org/"><strong>Postgres</strong></a><strong>!</strong></p><p>We are so happy to announce a new crazy idea: <strong>WebAssembly on Postgres</strong>. Yes, you read that correctly. On Postgres.</p><p><a href="https://github.com/wasmerio/postgres-ext-wasm">wasmerio/postgres-ext-wasm</a></p><h3>Calling a WebAssembly function from Postgres</h3><p>As usual, we have to go through the installation process. There is no package manager for Postgres, so it’s a manual step. The <a href="https://github.com/wasmerio/postgres-ext-wasm#installation">Installation Section of the documentation</a> explains the details; here is a summary:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e1d6ba6d7c309da44f29bf4a721ce9ab/href">https://medium.com/media/e1d6ba6d7c309da44f29bf4a721ce9ab/href</a></iframe><p>Once the extension is installed, activated and initialized, we can start having fun!</p><p>The current API is rather small, however basic features are available. The goal is to gather a community and to design a pragmatic API together, discover the expectations, how developers would use this new technology inside a database engine.</p><p>Let’s see how it works. To instantiate a WebAssembly module, we use the wasm_new_instance function. It takes 2 arguments: The absolute path to the WebAssembly module, and a prefix for the module exported functions. Indeed, if a module exports a function named sum, then a Postgres function named prefix_sum calling the sum function will be created dynamically.</p><p>Let’s see it in action. Let’s start by editing a Rust program that compiles to WebAssembly:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d09247a2cb85d0164062d43e10b0128/href">https://medium.com/media/8d09247a2cb85d0164062d43e10b0128/href</a></iframe><p>Once this file compiled to simple.wasm, we can instantiate the module, and call the exported sum function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3bee41939603cec3caef2b95ac0184a5/href">https://medium.com/media/3bee41939603cec3caef2b95ac0184a5/href</a></iframe><p><em>Et voilà ! </em>The ns_sum function calls the Rust sum function through WebAssembly! How fun is that 😄?</p><h3>Inspect a WebAssembly instance</h3><p>This section shows how to inspect a WebAssembly instance. At the same time, it quickly explains how the extension works under the hood.</p><p>The extension provides two foreign data wrappers, gathered together in the wasm foreign schema:</p><ul><li>wasm.instances is a table with the id and wasm_file columns, respectively for the unique instance ID, and the path of the WebAssembly module,</li><li>wasm.exported_functions is a table with the instance_id,name, inputs, and outputs columns, respectively for the instance ID of the exported function, its name, its input types (already formatted for Postgres), and its output types (already formatted for Postgres).</li></ul><p>Let’s see:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eca30aad0b601bcc56f835d8986db113/href">https://medium.com/media/eca30aad0b601bcc56f835d8986db113/href</a></iframe><p>Based on these information, the wasm Postgres extension is able to generate the SQL function to call the WebAssembly exported functions.</p><p>It sounds simplistic, and… to be honest, it is! The trick is to use <a href="https://www.postgresql.org/docs/current/fdwhandler.html">foreign data wrappers</a>, which is an awesome feature of Postgres.</p><h3>How fast is it, or: Is it an interesting alternative to PL/pgSQL?</h3><p>As we said, the extension API is rather small for now. The idea is to explore, to experiment, to have fun with WebAssembly inside a database. It is particularly interesting in two cases:</p><ol><li>To write extensions or procedures with any languages that compile to WebAssembly in place of <a href="https://www.postgresql.org/docs/10/plpgsql.html">PL/pgSQL</a>,</li><li>To remove a potential performance bottleneck where speed is involved.</li></ol><p>Thus we run a basic benchmark. Like most of the benchmarks out there, it must be taken with a grain of salt.</p><blockquote>The goal is to compare the execution time between WebAssembly and PL/pgSQL, and see how both approaches scale.</blockquote><p>The Postgres WebAssembly extension uses <a href="https://www.postgresql.org/docs/current/fdwhandler.html">Wasmer</a> as the runtime, compiled with the Cranelift backend (<a href="https://medium.com/wasmer/a-webassembly-compiler-tale-9ef37aa3b537">learn more about the different backends</a>). We run the benchmark with Postgres 10, on a MacBook Pro 15&quot; from 2016, 2.9Ghz Core i7 with 16Gb of memory.</p><p>The methodology is the following:</p><ul><li>Load both the plpgsql_fibonacci and the wasm_fibonacci functions,</li><li>Run them with a query like SELECT *_fibonacci(n) FROM generate_series(1, 1000) where n has the following values: 50, 500, and 5000, so that we can observe how both approaches scale,</li><li>Write the timings down,</li><li>Run this methodology multiple times, and compute the median of the results.</li></ul><p>Here come the results. The lower, the better.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*83zhTt2qeHi1qsAcNQOnGw.png" /><figcaption>Comparing WebAssembly vs. PL/pgSQL when computing the Fibonacci sequence with n=50, 500 and 5000.</figcaption></figure><p>We notice that the Postgres WebAssembly extension is faster to run numeric computations. The WebAssembly approach scales pretty well compared to the PL/pgSQL approach, <em>in this situation</em>.</p><h4>When to use the WebAssembly extension?</h4><p>So far, the extension only supports integers (on 32- and 64-bits). The extension doesn’t support strings <em>yet</em>. It also doesn’t support records, views or other Postgres types. Keep in mind this is the very first step.</p><p>Hence, it is too soon to tell whether WebAssembly can be an alternative to PL/pgSQL. But regarding the benchmark results above, we are sure they can live side-by-side, WebAssembly has clearly a place in the ecosystem! And we want to continue to pursue this exploration.</p><h3>Conclusion</h3><p>We are already talking with people that are interested in using WebAssembly inside databases. If you have any particular use cases, please reach us at <a href="https://wasmer.io/">wasmer.io</a>, or on Twitter at <a href="https://twitter.com/wasmerio">@wasmerio</a> directly or me <a href="https://twitter.com/mnt_io">@mnt_io</a>.</p><p>Everything is open source, as usual! Happy hacking.</p><p><a href="https://github.com/wasmerio/postgres-ext-wasm">wasmerio/postgres-ext-wasm</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=561af2cfcb1" width="1" height="1" alt=""><hr><p><a href="https://blog.wasmer.io/announcing-the-first-postgres-extension-to-run-webassembly-561af2cfcb1">Announcing the first Postgres extension to run WebAssembly</a> was originally published in <a href="https://blog.wasmer.io/">Wasmer</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Announcing the fastest WebAssembly runtime for Go: wasmer]]></title>
            <link>https://blog.wasmer.io/announcing-the-fastest-webassembly-runtime-for-go-wasmer-19832d77c050?source=rss-d1a15a1c8914------2</link>
            <guid isPermaLink="false">https://medium.com/p/19832d77c050</guid>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[rust]]></category>
            <category><![CDATA[webassembly]]></category>
            <category><![CDATA[benchmark]]></category>
            <category><![CDATA[go]]></category>
            <dc:creator><![CDATA[Ivan Enderlin]]></dc:creator>
            <pubDate>Wed, 29 May 2019 18:35:31 GMT</pubDate>
            <atom:updated>2019-05-29T18:35:31.596Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*65cBXtnql8Sc1IdqXrm9YQ.png" /><figcaption>Go loves WebAssembly — image attributions to the original <a href="https://vkt.sh/go-mongodb-driver-cookbook/">Gopher drawing</a>.</figcaption></figure><p>WebAssembly is a portable binary format. That means the same file can run anywhere.</p><blockquote>To uphold this bold statement, each language, platform and system must be able to run WebAssembly — as fast and safely as possible.</blockquote><p><a href="https://github.com/wasmerio/wasmer">Wasmer</a> is a WebAssembly runtime written in <a href="https://www.rust-lang.org/">Rust</a>. It goes without saying that the runtime can be used in any Rust application. We have also successfully embedded the runtime in other languages:</p><ul><li>Using <a href="https://github.com/wasmerio/wasmer/tree/master/lib/runtime-c-api">C and C++ bindings</a></li><li>In PHP, using <a href="https://github.com/wasmerio/php-ext-wasm">php-ext-wasm</a></li><li>In Python, using <a href="https://github.com/wasmerio/python-ext-wasm">python-ext-wasm</a> — <a href="https://pypi.org/project/wasmer/">wasmer package on PyPI</a></li><li>In Ruby, using <a href="https://github.com/wasmerio/ruby-ext-wasm">ruby-ext-wasm</a> — <a href="https://rubygems.org/gems/wasmer">wasmer gem on RubyGems</a></li><li><strong>It is now time to hang around </strong><a href="https://golang.org/"><strong>Go</strong></a><strong> 🐹!</strong></li></ul><p>We are super happy to announce github.com/wasmerio/go-ext-wasm/wasmer, a <a href="https://github.com/wasmerio/go-ext-wasm">Go library to run WebAssembly binaries, fast</a>.</p><p><a href="https://github.com/wasmerio/go-ext-wasm">wasmerio/go-ext-wasm</a></p><h3>Calling a WebAssembly function from Go</h3><p>First, let’s install wasmer in your go environment (<em>with cgo support</em>).</p><pre>export CGO_ENABLED=1; export CC=gcc; go install github.com/wasmerio/go-ext-wasm/wasmer</pre><p>Let’s jump immediately into some examples.github.com/wasmerio/go-ext-wasm/wasmer is a regular Go library. The installation is automated with import &quot;github.com/wasmerio/go-ext-wasm/wasmer&quot;.</p><p>Let’s get our hands dirty. We will write a program that compiles to WebAssembly easily, using Rust for instance:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dea691e8dcfb18f75fbdb1eead97ce5e/href">https://medium.com/media/dea691e8dcfb18f75fbdb1eead97ce5e/href</a></iframe><p>After compilation to WebAssembly, we get a file like <a href="https://github.com/wasmerio/go-ext-wasm/blob/master/wasmer/test/testdata/examples/simple.wasm">this one</a>, named simple.wasm.<br>The following Go program executes the sum function by passing 5 and 37 as arguments:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/23333ab7c7f258ae1f817a7f2b3a5fef/href">https://medium.com/media/23333ab7c7f258ae1f817a7f2b3a5fef/href</a></iframe><p>Great! We have successfully executed a WebAssembly file inside Go.</p><blockquote><em>Note: Go values passed to the WebAssembly exported function are automatically cast to WebAssembly values. Types are inferred and casting is done automatically. Thus, a WebAssembly function acts as any regular Go function.</em></blockquote><h3>WebAssembly calling Go funtions</h3><p>A WebAssembly module <em>exports</em> some functions, so that they can be called from the outside world. This is the entry point to execute WebAssembly.</p><p>Nonetheless, a WebAssembly module can also have <em>imported</em> functions. Let’s consider the following Rust program:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5959263fb308d9142c67825ec15039e7/href">https://medium.com/media/5959263fb308d9142c67825ec15039e7/href</a></iframe><p>The exported function add1 calls the sum function. Its implementation is absent, only its signature is defined. This is an “extern function”, and for WebAssembly, this is an <em>imported</em> function, because its implementation must be <em>imported</em>.</p><p>Let’s implement the sum function in Go! To do so, <em>need</em> to use <a href="https://blog.golang.org/c-go-cgo">cgo</a>:</p><ol><li>The sum function signature is defined in C (see the comment above import &quot;C&quot;),</li><li>The sum implementation is defined in Go. Notice the //export which is the way cgo uses to map Go code to C code,</li><li>NewImports is an API used to create WebAssembly imports. In this code &quot;sum&quot; is the WebAssembly imported function name, sum is the Go function pointer, and C.sum is the cgo function pointer,</li><li>Finally, NewInstanceWithImports is the constructor to use to instantiate the WebAssembly module with imports. That’s it.</li></ol><p>Let’s see the complete program:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/839d92c665528aae082fcfe189565dde/href">https://medium.com/media/839d92c665528aae082fcfe189565dde/href</a></iframe><h3>Reading the memory</h3><p>A WebAssembly instance has a linear memory. Let’s see how to read it. Consider the following Rust program:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f586ce239cfddfc2f595b17aa02acf6b/href">https://medium.com/media/f586ce239cfddfc2f595b17aa02acf6b/href</a></iframe><p>The return_hello function returns a pointer to a string. The string terminates by a null byte, <em>à la </em>C. Let’s jump on the Go side:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/91379db16442ea970f33085033b1ecd0/href">https://medium.com/media/91379db16442ea970f33085033b1ecd0/href</a></iframe><p>The return_hello function returns a pointer as an i32 value. We get its value by calling ToI32. Then, we fetch the memory data with instance.Memory.Data().</p><p>This function returns a slice over the WebAssembly instance memory. It can be used as any regular Go slice.</p><p>Fortunately for us, we already know the length of the string we want to read, so memory[pointer : pointer+13] is enough to read the bytes, that are then cast to a string. <em>Et voilà !</em></p><blockquote>You can read <a href="https://github.com/wasmerio/go-ext-wasm/blob/6934a0fa06558f77884398a2371de182593e6a6c/wasmer/test/example_greet_test.go">the Greet Example</a> to see a more advanced usage of the memory API.</blockquote><h3>Benchmarks</h3><p>So far, github.com/wasmerio/go-ext-wasm/wasmer has a nice API, but …<em>is it fast</em>?</p><p>Contrary to PHP or Ruby, there are already existing runtimes in the Go world to execute WebAssembly. The main candidates are:</p><ul><li><a href="https://github.com/perlin-network/life">Life</a>, from Perlin Network, a WebAssembly interpreter</li><li><a href="https://github.com/go-interpreter/wagon">Wagon</a>, from Go Interpreter, a WebAssembly interpreter and toolkit.</li></ul><p>In <a href="https://medium.com/wasmer/php-ext-wasm-migrating-from-wasmi-to-wasmer-4d1014f41c88">our blog post about the PHP extension</a>, we have used <a href="https://benchmarksgame-team.pages.debian.net/benchmarksgame/description/nbody.html">the n-body algorithm</a> to benchmark the performance. Life provides more benchmarks: <a href="https://en.wikipedia.org/wiki/Fibonacci_number">the Fibonacci algorithm</a> (the recursive version), <a href="https://en.wikipedia.org/wiki/Pollard%27s_rho_algorithm">the Pollard’s rho algorithm</a>, and the Snappy Compress operation. The latter works successfully with github.com/wasmerio/go-ext-wasm/wasmer but not with Life or Wagon. We have removed it from the benchmark suites. <a href="https://github.com/wasmerio/go-ext-wasm/tree/master/benchmarks">Benchmark sources</a> are online.</p><p>We use Life 20190521143330–57f3819c2df0, and Wagon 0.4.0, i.e. <em>the latest versions to date</em>.</p><p>The benchmark numbers represent the average result for 10 runs each. The computer that ran these benchmarks is a MacBook Pro 15&quot; from 2016, 2.9Ghz Core i7 with 16Gb of memory.</p><p>Results are grouped by benchmark algorithm on the X axis. The Y axis represents the time used to run the algorithm, expressed in milliseconds. The lower, the better.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*08ymx9shShohcPCKi1XjlA.png" /><figcaption>Speed comparison between Wasmer, Wagon and Life. Benchmark suites are the n-body, Fibonacci, and Pollard’s rho algorithms. Speed is expressed in ms. Lower is better.</figcaption></figure><p>While both Life and Wagon provide on average the same speed, Wasmer (github.com/wasmerio/go-ext/wasmer) is on average <strong>72 times faster</strong> 🎉.</p><p>It is important to know that Wasmer comes with 3 backends: <a href="https://github.com/wasmerio/wasmer/tree/master/lib/singlepass-backend">Singlepass</a>, <a href="https://github.com/wasmerio/wasmer/tree/master/lib/clif-backend">Cranelift</a>, and <a href="https://github.com/wasmerio/wasmer/tree/master/lib/llvm-backend">LLVM</a>. The default backend that is used by the Go library is Cranelift (<a href="https://github.com/CraneStation/cranelift">learn more about Cranelift</a>). Using LLVM will provide performance close to native, but we decided to start with Cranelift as it offers the best tradeoff between compilation-time and execution-time (<a href="https://medium.com/wasmer/a-webassembly-compiler-tale-9ef37aa3b537">learn more about the different backends</a>, when to use them, pros and cons etc.).</p><h3>Conclusion</h3><p><a href="https://github.com/wasmerio/go-ext-wasm">github.com/wasmerio/go-ext-wasm/wasmer</a> is a new Go library to execute WebAssembly binaries. It embeds the <a href="https://github.com/wasmerio/wasmer">Wasmer</a> runtime. The first version supports all the required API for the most common usages.</p><p>The current benchmarks (a mix from our benchmark suites and from Life suites) show that <strong>Wasmer is — on average — 72 times faster than Life and Wagon</strong>, the two major existing WebAssembly runtimes in the Go world.</p><p>If you want to follow the development, take a look at <a href="https://twitter.com/wasmerio">@wasmerio</a> and <a href="https://twitter.com/mnt_io">@mnt_io</a> on Twitter, or <a href="https://webassembly.social/@wasmer">@wasmer@webassembly.social</a> on Mastodon. And of course, everything is open source at <a href="https://github.com/wasmerio/go-ext-wasm">https://github.com/wasmerio/go-ext-wasm</a>.</p><p><a href="https://github.com/wasmerio/go-ext-wasm">wasmerio/go-ext-wasm</a></p><p>Thank you for your time, we can’t wait to see what you build with us!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=19832d77c050" width="1" height="1" alt=""><hr><p><a href="https://blog.wasmer.io/announcing-the-fastest-webassembly-runtime-for-go-wasmer-19832d77c050">Announcing the fastest WebAssembly runtime for Go: wasmer</a> was originally published in <a href="https://blog.wasmer.io/">Wasmer</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[++ php-ext-wasm: Migrating from wasmi to Wasmer]]></title>
            <link>https://blog.wasmer.io/php-ext-wasm-migrating-from-wasmi-to-wasmer-4d1014f41c88?source=rss-d1a15a1c8914------2</link>
            <guid isPermaLink="false">https://medium.com/p/4d1014f41c88</guid>
            <category><![CDATA[php]]></category>
            <category><![CDATA[webassembly]]></category>
            <category><![CDATA[extension]]></category>
            <category><![CDATA[wasm]]></category>
            <dc:creator><![CDATA[Ivan Enderlin]]></dc:creator>
            <pubDate>Wed, 03 Apr 2019 08:18:52 GMT</pubDate>
            <atom:updated>2019-04-04T07:04:43.694Z</atom:updated>
            <content:encoded><![CDATA[<h4>How php-ext-wasm has migrated from wasmi to Wasmer and now enjoys a 29x speedup, is faster than PHP itself, and is closer to native speed.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8zCiERXz0owQ83Ak--sbag.jpeg" /><figcaption>Elephant in the forest. <a href="https://www.pexels.com/photo/view-of-elephant-in-water-247431/">Source of the photo</a>.</figcaption></figure><p>First as a joke, now as a real product, I started to develop <a href="https://github.com/wasmerio/php-ext-wasm">php-ext-wasm</a>: a <a href="http://php.net/">PHP</a> extension allowing to execute <a href="https://webassembly.org/">WebAssembly</a> binaries.</p><p>The PHP virtual machine (VM) is <a href="https://github.com/php/php-src/">Zend Engine</a>. To write an extension, one needs to develop in C or C++. The extension was simple C bindings to a Rust library I also wrote. At that time, this Rust library was using <a href="https://github.com/paritytech/wasmi">wasmi</a> for the WebAssembly VM. I knew that wasmi wasn’t the fastest WebAssembly VM in the game, but the API is solid, well-tested, it compiles quickly, and is easy to hack. All the requirements to start a project!</p><p>After 6 hours of development, I got something working. I was able to run the following PHP program:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3aebecd9b6b69e73d04ff3c2b60db299/href">https://medium.com/media/3aebecd9b6b69e73d04ff3c2b60db299/href</a></iframe><p>The API is straightforward: create an instance (here of simple.wasm), then call functions on it (here sum with 1 and 2 as arguments). PHP values are transformed into WebAssembly values automatically. For the record, here is the simple.rs Rust program that is compiled to a WebAssembly binary:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a35e29ea44e63f95a31349daf975dd2/href">https://medium.com/media/0a35e29ea44e63f95a31349daf975dd2/href</a></iframe><p>It was great! 6 hours is a relatively small number of hours to go that far according to me.</p><p>However, I quickly noticed that wasmi is… slow. <a href="https://webassembly.org/">One of the promise of WebAssembly</a> is:</p><blockquote>WebAssembly aims to execute at native speed by taking advantage of <a href="https://webassembly.org/docs/portability/#assumptions-for-efficient-execution">common hardware capabilities</a> available on a wide range of platforms.</blockquote><p>And clearly, my extension wasn’t fulfilling this promise. Let’s see a basic comparison with a benchmark.</p><p>I chose <a href="https://benchmarksgame-team.pages.debian.net/benchmarksgame/description/nbody.html">the <em>n-body</em> algorithm</a> from <a href="https://benchmarksgame-team.pages.debian.net/benchmarksgame/">the Computer Language Benchmarks Game</a> from Debian, mostly because it’s relatively CPU intensive. Also, the algorithm has a simple interface: based on an integer, it returns a floating-point number; this API doesn’t involve any advanced instance memory API, which is perfect to test a proof-of-concept.</p><p>As a baseline, I’ve run the <em>n-body</em> algorithm <a href="https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/nbody-rust-7.html">written in Rust</a>, let’s call it rust-baseline. The same algorithm has been <a href="https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/nbody-php-3.html">written in PHP</a>, let’s call it php. Finally, the algorithm has been compiled from Rust to WebAssembly, and executed with the php-ext-wasm extension, let’s call that case php+wasmi. All results are for nbody(5000000):</p><ul><li>rust-baseline: 287ms,</li><li>php: 19,761ms,</li><li>php+wasmi: 67,622ms.</li></ul><p>OK, so… php-ext-wasm with wasmi is <strong>3.4 times slower</strong> than PHP itself, it is pointless to use WebAssembly in such conditions!</p><p>It confirms my first intuition though: In our case, wasmi is really great to mock something up, but it’s not fast enough for our expectations.</p><h3>Faster, faster, faster…</h3><p>I wanted to use <a href="https://github.com/CraneStation/cranelift">Cranelift</a> since the beginning. It’s a code generator, <em>à la</em> <a href="http://llvm.org/">LLVM</a> (excuse the brutal shortcut, the goal isn’t to explain what Cranelift is in details, but that’s a really awesome project!). To quote the project itself:</p><blockquote>Cranelift is a low-level retargetable code generator. It translates a <a href="https://cranelift.readthedocs.io/en/latest/ir.html">target-independent intermediate representation</a> into executable machine code.</blockquote><p>It basically means that the Cranelift API can be used to generate executable code.</p><p>It’s perfect! I can replace wasmi by Cranelift, and boom, profit. But… there is other ways to get even faster code execution — at the cost of a longer code compilation though.</p><p>For instance, LLVM can provide a very fast code execution, almost at native speed. Or we can generate assembly code dynamically. Well, there is multiple ways to achieve that. What if a project could provide a WebAssembly virtual machine with multiple backends?</p><h3>Enter Wasmer</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CU5Lb91yQekJSUSf8YGshQ.png" /></figure><p>And it was at that specific time that I’ve been hired by <a href="https://github.com/wasmerio/wasmer">Wasmer</a>. To be totally honest, I was looking at Wasmer a few weeks before. It was a surprise and a great opportunity for me. Well, the universe really wants this rewrite from wasmi to Wasmer, right 😅?</p><p>Wasmer is organized as a set of Rust libraries (called crates). There is even a wasmer-runtime-c-api crate which is a C and a C++ API on top of the wasmer-runtime crate and the wasmer-runtime-core crate, i.e. it allows running the WebAssembly virtual machine as you want, with the backend of your choice: <em>Cranelift</em>, <em>LLVM</em>, or <em>Dynasm</em> (at the time of writing). That’s perfect, it removes my Rust library between the PHP extension and wasmi. Then php-ext-wasm is reduced to a PHP extension without any Rust code, everything goes to wasmer-runtime-c-api. That’s sad to remove Rust from this project, but it relies on more Rust code!</p><p>Counting the time to make some patches on wasmer-runtime-c-api, I’ve been able to migrate php-ext-wasm to Wasmer in 5 days.</p><p>By default, php-ext-wasm uses Wasmer with the Cranelift backend, it does a great balance between compilation and execution time. It is really good. Let’s run the benchmark, with the addition of php+wasmer(cranelift):</p><ul><li>rust-baseline: 287ms,</li><li>php: 19,761ms,</li><li>php+wasmi: 67,622ms,</li><li>php+wasmer(cranelift): 2,365ms 🎉.</li></ul><p>Finally, the PHP extension provides a faster execution than PHP itself! php+wasmer(cranelift) is <strong>8.6 times faster</strong> than php to be exact. And it is <strong>28.6 times faster</strong> than php+wasmi. Can we reach the native speed (represented by rust-baseline here)? It’s very likely with LLVM. That’s for another article. I’m super happy with Cranelift for the moment. (See <a href="https://medium.com/wasmer/benchmarking-webassembly-runtimes-18497ce0d76e">our previous blog post to learn how we benchmark different backends in Wasmer, and other WebAssembly runtimes</a>).</p><h3>More Optimizations</h3><p>Wasmer provides more features, like module caching. Those features are now included in the PHP extension. When booting the nbody.wasm file (19kb), it took 4.2ms. By booting, I mean: reading the WebAssembly binary from a file, parsing it, validating it, compiling it to executable code and a WebAssembly module structure.</p><p>PHP execution model is: starts, runs, dies. Memory is freed for each request. If one wants to use php-ext-wasm, you don’t really want to pay that “<em>booting cost</em>” every time.</p><p>Hopefully, wasmer-runtime-c-api now provides a module serialization API, which is integrated into the PHP extension itself. It saves the “booting cost”, but it adds a “deserialization cost”. That second cost is smaller, but still, we need to know it exists.</p><p>Hopefully again, Zend Engine has an API to get persistent in-memory data between PHP executions. php-ext-wasm supports that API to get persistent modules, <em>et voilà</em>.</p><p>Now it takes <strong>4.2ms</strong> for the first boot of nbody.wasm and <strong>0.005ms</strong> for all the next boots. It’s 840 times faster!</p><h3>Conclusion</h3><p>Wasmer is a young — but mature — framework to build WebAssembly runtimes on top of. The default backend is Cranelift, and it shows its promises: It brings a correct balance between compilation time and execution time.</p><p>wasmi has been a good companion to develop a <em>Proof-Of-Concept</em>. This library has its place in other usages though, like very short-living WebAssembly binaries (I’m thinking of Ethereum contracts that compile to WebAssembly for instance, which is one of the actual use cases). It’s important to understand that no runtime is better than another, it depends on the use case.</p><p>The next step is to stabilize php-ext-wasm to release a 1.0.0 version.</p><p>See you there!</p><p>If you want to follow the development, take a look at <a href="https://twitter.com/wasmerio">@wasmerio</a> and <a href="https://twitter.com/mnt_io">@mnt_io</a> on Twitter.</p><p><a href="https://github.com/wasmerio/php-ext-wasm">wasmerio/php-ext-wasm</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4d1014f41c88" width="1" height="1" alt=""><hr><p><a href="https://blog.wasmer.io/php-ext-wasm-migrating-from-wasmi-to-wasmer-4d1014f41c88">🐘+🦀+🕸 php-ext-wasm: Migrating from wasmi to Wasmer</a> was originally published in <a href="https://blog.wasmer.io/">Wasmer</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>