Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array view instead of direct wasm memory access #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ default = ["console_error_panic_hook"]
[dependencies]
cfg-if = "0.1.2"
wasm-bindgen = "0.2"
js-sys = "0.3"

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
Expand Down
20 changes: 8 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ extern crate web_sys;
mod utils;

use std::fmt;
use std::mem;
use wasm_bindgen::prelude::*;
use web_sys::console;

Expand Down Expand Up @@ -71,17 +72,9 @@ impl Universe {
fn live_neighbor_count(&self, row: u32, column: u32) -> u8 {
let mut count = 0;

let north = if row == 0 {
self.height - 1
} else {
row - 1
};
let north = if row == 0 { self.height - 1 } else { row - 1 };

let south = if row == self.height - 1 {
0
} else {
row + 1
};
let south = if row == self.height - 1 { 0 } else { row + 1 };

let west = if column == 0 {
self.width - 1
Expand Down Expand Up @@ -208,8 +201,11 @@ impl Universe {
self.cells = (0..self.width * height).map(|_i| Cell::Dead).collect();
}

pub fn cells(&self) -> *const Cell {
self.cells.as_ptr()
pub fn cells(&self) -> js_sys::Uint8Array {
unsafe {
let u8_cells = mem::transmute::<&Vec<Cell>, &Vec<u8>>(&self.cells);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can avoid transmute here and instead cast the slice to pointer, convert the pointer, and construct a new slice from that:

    let u8_cells: &[u8] = unsafe {
        let ptr = self.cells.as_ptr() as *const u8;
        let bytelen = self.cells.len() * std::mem::size_of::<Cell>();
        std::slice::from_raw_parts(ptr, bytelen)
    };

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=98b83b545fce22aec8fdf4a5e2ed5c55

js_sys::Uint8Array::view(&u8_cells)
}
}

pub fn toggle_cell(&mut self, row: u32, column: u32) {
Expand Down
4 changes: 1 addition & 3 deletions www/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Universe, Cell } from "wasm-game-of-life";
import { memory } from "wasm-game-of-life/wasm_game_of_life_bg";

const CELL_SIZE = 5; // px
const GRID_COLOR = "#CCCCCC";
Expand Down Expand Up @@ -126,8 +125,7 @@ const getIndex = (row, column) => {
};

const drawCells = () => {
const cellsPtr = universe.cells();
const cells = new Uint8Array(memory.buffer, cellsPtr, width * height);
const cells = universe.cells();

ctx.beginPath();

Expand Down