Skip to content

Commit

Permalink
Eliminate duplicate types in the type section
Browse files Browse the repository at this point in the history
This commit updates the `wasm-gc` pass of wasm-bindgen to eliminate
duplicate types in the type section, effectively enabling a gc of the
type section itself. The main purpose here is ensure that code generated
by `wasm-bindgen` itself doesn't have to go too far out of its way to
deduplicate at generation time, but rather it can rely on the gc pass to
clean up.

Note that this currently depends on paritytech/parity-wasm#231, but this
can be updated if that ends up not landing.
  • Loading branch information
alexcrichton committed Oct 28, 2018
1 parent 7038542 commit 16c015a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,4 @@ members = [

[patch.crates-io]
wasm-bindgen = { path = '.' }
parity-wasm = { git = 'https://github.com/alexcrichton/parity-wasm', branch = 'hash' }
18 changes: 12 additions & 6 deletions crates/gc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extern crate log;
extern crate rustc_demangle;

use std::mem;
use std::collections::HashSet;
use std::collections::{HashSet, HashMap};

use parity_wasm::elements::*;

Expand Down Expand Up @@ -457,14 +457,20 @@ impl<'a> RemapContext<'a> {
let mut memories = Vec::new();

if let Some(s) = m.type_section() {
let mut removed = 0;
for i in 0..(s.types().len() as u32) {
if analysis.types.contains(&i) {
types.push(i - removed);
let mut ntypes = 0;
let mut map = HashMap::new();
for (i, ty) in s.types().iter().enumerate() {
if analysis.types.contains(&(i as u32)) {
if let Some(prev) = map.get(&ty) {
types.push(*prev);
continue
}
map.insert(ty, ntypes);
types.push(ntypes);
ntypes += 1;
} else {
debug!("gc type {}", i);
types.push(u32::max_value());
removed += 1;
}
}
}
Expand Down

0 comments on commit 16c015a

Please sign in to comment.