Skip to content
Permalink
Browse files

Avoid 'free functions'

  • Loading branch information...
marcomontalbano committed Feb 28, 2019
1 parent 7406a88 commit 3223185dcdad056ed24e1281027b4ab871a4efd5
Showing with 71 additions and 53 deletions.
  1. +7 −7 README.md
  2. +2 −2 src-js/libs/mod.js
  3. +2 −0 src-js/main.js
  4. +1 −1 src-js/web/chart.js
  5. +3 −2 src-js/web/js.worker.js
  6. +3 −2 src-js/web/rs.worker.js
  7. +26 −18 src/libs/matrix.rs
  8. +25 −19 src/libs/primes.rs
  9. +2 −2 src/main.rs
@@ -87,16 +87,16 @@ $ cargo build --release
Now that we have built our code, we can run it:

```sh
$ ./target/release/wasm-vs-js-benchmark primes-get_primes 11
$ ./target/release/wasm-vs-js-benchmark Primes::get_primes 11
```

We can also use `cargo run` to compile and then run it, all in one step:

```sh
$ cargo run primes-get_primes 11
$ cargo run Primes::get_primes 11
# compile and run our project with optimizations
$ cargo run --release primes-get_primes 11
$ cargo run --release Primes::get_primes 11
```

Last but not least, we'll compile our project to `.wasm`:
@@ -151,14 +151,14 @@ These benchmarks are recorded on a MacBook Pro (15-inch, 2016) having these spec
$ cargo build --release
$ hyperfine --warmup 3 --export-markdown BENCHMARK.md \
'./target/release/wasm-vs-js-benchmark primes-get_primes 100000' \
'./target/release/wasm-vs-js-benchmark matrix-multiply 500 500'
'./target/release/wasm-vs-js-benchmark Primes::get_primes 100000' \
'./target/release/wasm-vs-js-benchmark Matrix::multiply 500 500'
```

| Command | Mean [s] | Min…Max [s] |
|:---|---:|---:|
| `./target/release/wasm-vs-js-benchmark primes-get_primes 100000` | 1.211 ± 0.018 | 1.196…1.255 |
| `./target/release/wasm-vs-js-benchmark matrix-multiply 500 500` | 0.435 ± 0.016 | 0.417…0.469 |
| `./target/release/wasm-vs-js-benchmark Primes::get_primes 100000` | 1.182 ± 0.003 | 1.178…1.188 |
| `./target/release/wasm-vs-js-benchmark Matrix::multiply 500 500` | 0.410 ± 0.007 | 0.402…0.422 |


### WebAssembly vs Javascript
@@ -1,2 +1,2 @@
export * from './primes';
export * from './matrix';
export const primes = require('./primes');
export const matrix = require('./matrix');
@@ -3,10 +3,12 @@ import { promiseSequential } from './web/utility';

promiseSequential([
createBenchmarkChart({
module: 'primes',
method: 'get_primes',
args: [100000]
}),
createBenchmarkChart({
module: 'matrix',
method: 'multiply',
args: [500, 500]
})
@@ -96,7 +96,7 @@ export const createBenchmarkChart = payload => {
element_chartContainer.classList.add('chart-container');

let element_chartCanvas = document.createElement('canvas');
let chart = _createChart(element_chartCanvas, `${payload.method}(${payload.args.join(', ')})`);
let chart = _createChart(element_chartCanvas, `${payload.module}.${payload.method}(${payload.args.join(', ')})`);

element_chartContainer.appendChild(element_chartCanvas);
document.getElementById('root').appendChild(element_chartContainer);
@@ -4,8 +4,9 @@ import { measure } from './performance';

onmessage = e => {

const performance = measure(e.data.payload.method, () => {
return mod[e.data.payload.method](...e.data.payload.args)
const method = `${e.data.payload.module}_${e.data.payload.method}`;
const performance = measure(method, () => {
return mod[e.data.payload.module][e.data.payload.method](...e.data.payload.args)
})

postMessage({
@@ -8,8 +8,9 @@ onmessage = e => {
// WebAssembly.instantiateStreaming(fetch(wasm), { './wasm_vs_js_benchmark': _exp }).then(results => {
fetch(wasm).then(response => response.arrayBuffer()).then(bytes => WebAssembly.instantiate(bytes, { './wasm_vs_js_benchmark': _exp })).then(results => {

const performance = measure(e.data.payload.method, () => {
return results.instance.exports[e.data.payload.method](...e.data.payload.args)
const method = `${e.data.payload.module}_${e.data.payload.method}`;
const performance = measure(method, () => {
return results.instance.exports[method](...e.data.payload.args)
})

postMessage({
@@ -2,29 +2,37 @@ extern crate wasm_bindgen;

use wasm_bindgen::prelude::*;

#[allow(dead_code)]
fn _multiply(a: Vec<Vec<i32>>, b: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
#[wasm_bindgen]
pub struct Matrix {

}

#[wasm_bindgen]
impl Matrix {

let mut c: Vec<Vec<i32>> = Vec::new();
fn _multiply(a: Vec<Vec<i32>>, b: Vec<Vec<i32>>) -> Vec<Vec<i32>> {

for i in 0..a.len() {
c.push(vec![0; b.len()]);
for j in 0..b[0].len() {
for k in 0..b.len() {
c[i][j] += a[i][k] * b[k][j];
let mut c: Vec<Vec<i32>> = Vec::new();

for i in 0..a.len() {
c.push(vec![0; b.len()]);
for j in 0..b[0].len() {
for k in 0..b.len() {
c[i][j] += a[i][k] * b[k][j];
}
}
}

return c;
}

return c;
}
pub fn multiply(a: usize, b: usize) -> i32 {
return Matrix::_multiply(
vec![vec![1; a]; a],
vec![vec![1; b]; b]
)[0][0];
}

#[wasm_bindgen]
pub fn multiply(a: usize, b: usize) -> i32 {
return _multiply(
vec![vec![1; a]; a],
vec![vec![1; b]; b]
)[0][0];
}

#[cfg(test)]
@@ -34,14 +42,14 @@ mod tests {

#[test]
fn test_private_multiply() {
assert_eq!(_multiply(
assert_eq!(Matrix::_multiply(
vec![vec![1, 2], vec![3, 4]],
vec![vec![1, 2], vec![3, 4]]
), vec![vec![7, 10], vec![15, 22]]);
}

#[test]
fn test_multiply() {
assert_eq!(multiply(10,10), 10);
assert_eq!(Matrix::multiply(10,10), 10);
}
}
@@ -2,32 +2,38 @@ extern crate wasm_bindgen;

use wasm_bindgen::prelude::*;

#[allow(dead_code)]
fn is_prime(n: i32) -> bool {
#[wasm_bindgen]
pub struct Primes {

}

#[wasm_bindgen]
impl Primes {

pub fn is_prime(n: i32) -> bool {

for x in 2..n {
if n % x == 0 {
return false;
for x in 2..n {
if n % x == 0 {
return false;
}
}

return n >= 2;
}

return n >= 2;
}
pub fn get_primes(n: i32) -> i32 {
let mut i = 0;

#[wasm_bindgen]
#[allow(dead_code)]
pub fn get_primes(n: i32) -> i32 {
let mut i = 0;
for x in 2..n+1 {

for x in 2..n+1 {
if Primes::is_prime(x) {
i += 1;
}

if is_prime(x) {
i += 1;
}

return i;
}

return i;
}

#[cfg(test)]
@@ -38,12 +44,12 @@ mod tests {

#[test]
fn test_is_prime() {
assert_eq!(is_prime(2), true);
assert_eq!(is_prime(97), true);
assert_eq!(Primes::is_prime(2), true);
assert_eq!(Primes::is_prime(97), true);
}

#[test]
fn test_get_primes() {
assert_eq!(get_primes(11), 5);
assert_eq!(Primes::get_primes(11), 5);
}
}
@@ -11,8 +11,8 @@ fn main() {
let s: String = invoked_fn.into();

let result = match &s[..] {
"primes-get_primes" => libs::primes::get_primes(args[0].parse().unwrap()),
"matrix-multiply" => libs::matrix::multiply(args[0].parse().unwrap(), args[1].parse().unwrap()),
"Primes::get_primes" => libs::primes::Primes::get_primes(args[0].parse().unwrap()),
"Matrix::multiply" => libs::matrix::Matrix::multiply(args[0].parse().unwrap(), args[1].parse().unwrap()),
_ => 0
};

0 comments on commit 3223185

Please sign in to comment.
You can’t perform that action at this time.