From 27001a226d6700f9a22751d2ba9a56c0d3bb1211 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sun, 28 Oct 2018 09:12:58 -0700 Subject: [PATCH] Eliminate duplicate types in the type section 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. --- crates/gc/Cargo.toml | 2 +- crates/gc/src/lib.rs | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/crates/gc/Cargo.toml b/crates/gc/Cargo.toml index cd088a1f..522bf459 100644 --- a/crates/gc/Cargo.toml +++ b/crates/gc/Cargo.toml @@ -11,6 +11,6 @@ Support for removing unused items from a wasm executable """ [dependencies] -parity-wasm = "0.35" +parity-wasm = "0.35.1" log = "0.4" rustc-demangle = "0.1.9" diff --git a/crates/gc/src/lib.rs b/crates/gc/src/lib.rs index 12667a3d..cf82eca2 100644 --- a/crates/gc/src/lib.rs +++ b/crates/gc/src/lib.rs @@ -8,7 +8,7 @@ extern crate parity_wasm; extern crate log; extern crate rustc_demangle; -use std::collections::HashSet; +use std::collections::{HashSet, HashMap}; use std::iter; use std::mem; @@ -474,14 +474,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; } } }