Code Monkey home page Code Monkey logo

polyglotr's Introduction

Use C, Rust and Assemblyscript in an R pacakge

Lifecycle: experimental

The goal of polyglotr is just to show how you can use three different languages through WebAssembly in an R package using wasmr. All of this is very experimental.

Example

library(polyglotr)

The package exposes an inefficient, recursive fibonacci algorithm written in languages that can compile to WebAssembly.

fib_r <- function(n) {
  if (n < 2) return(n)
  Recall(n - 1) + Recall(n - 2)
}

Each of the following functions calls a bundled binary wasm file in the inst directory that was compiled from the respective language. The binary file is distributed with the package, but not necessarily the source files. The user does not need to compile anything (apart from the wasmr package).

fib_c(20)
#> [1] 6765
fib_rust(20)
#> [1] 6765
fib_assemblyscript(20)
#> [1] 6765
fib_go(20)
#> [1] 6765
fib_r(20)
#> [1] 6765

The benchmark is not meant to compare different compilers, just to give a rough feeling about timing. In fact most of the compilers will probably emit very similiar wasm code. Also each function call reads the wasm file from disk and instantiates it.

I also used tinygo to compile the go code which adds some interface code resulting in a 4kb file. There might be flags to further optimize the generated code.

bench::mark(
  fib_c(20),
  fib_rust(20),
  fib_assemblyscript(20),
  fib_go(20),
  fib_r(20)
)
#> # A tibble: 5 x 6
#>   expression                  min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>             <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 fib_c(20)                1.21ms   1.39ms     439.     12.2KB     2.07
#> 2 fib_rust(20)             1.19ms   1.32ms     618.     12.1KB     2.03
#> 3 fib_assemblyscript(20)   1.17ms   1.27ms     661.     11.4KB     4.29
#> 4 fib_go(20)               2.75ms   2.98ms     280.     15.7KB     2.06
#> 5 fib_r(20)               14.17ms  14.79ms      60.4        0B    33.5

polyglotr's People

Contributors

dirkschumacher avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.