Skip to content

Commit

Permalink
minify wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
jwasinger committed Nov 23, 2020
1 parent 85bee0e commit b99bca1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
19 changes: 16 additions & 3 deletions wasm-engines/scripts/fill_rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ def get_rust_bytes(hex_str):
tmp = reduce(lambda x, y: x+', '+y, tmp)
return '[ '+tmp+' ]'

def minify_wasm(wasm_out_dir, wasm_minified_dir):
rust_wasm_minify_cmd = "/scripts/wasm_minify.sh {} {}".format(wasm_out_dir, wasm_minified_dir)
proc = subprocess.Popen(rust_wasm_minify_cmd, cwd='/root', stderr=subprocess.STDOUT, stdout=subprocess.PIPE, shell=True)
return_code = proc.wait(None)
if return_code != 0:
stdoutlines = [str(line, 'utf8') for line in proc.stdout]
print("error:")
print("".join(stdoutlines))
sys.exit(-1)

def fill_rust(benchname, input, rust_code_dir, wasm_out_dir, native_out_dir):
#rustsrc = "{}/rust-code/src/bench.rs".format(os.path.abspath(benchname))
#rustsrc = "{}/rust-code".format(os.path.abspath(benchname))
Expand Down Expand Up @@ -97,7 +107,6 @@ def fill_rust(benchname, input, rust_code_dir, wasm_out_dir, native_out_dir):
print(("").join(stdoutlines), end="")
if return_code != 0:
sys.exit(-1)
# wasm is at ./target/wasm32-unknown-unkown/release/sha1_wasm.wasm
exec_out_path=os.path.join(native_out_dir, input['name'])

wasmbin = "{}/target/wasm32-unknown-unknown/release/{}_wasm.wasm".format(filldir, benchname_rust)
Expand All @@ -111,14 +120,16 @@ def fill_rust(benchname, input, rust_code_dir, wasm_out_dir, native_out_dir):

def main():
parser = argparse.ArgumentParser()
parser.add_argument('--wasmoutdir', help='full path of dir containing wasm files')
parser.add_argument("--wasmminifieddir", help='full path to output minified wasm files')
parser.add_argument('--wasmoutdir', help='full path of dir containing non-minified wasm files')
parser.add_argument('--rustcodedir', help='comma-separated list of engines to benchmark')
parser.add_argument('--inputvectorsdir', help='comma-separated list of engines to benchmark')
parser.add_argument('--nativeoutdir', help='directory to put binaries compiled from rust code')

args = vars(parser.parse_args())

wasm_out_dir = args['wasmoutdir']
wasm_minified_dir = args['wasmminifieddir']
native_out_dir = args['nativeoutdir']
rust_code_dir = args['rustcodedir']
input_vectors_dir = args['inputvectorsdir']
Expand All @@ -141,7 +152,9 @@ def main():

for input in bench_inputs:
print("bench input:", input['name'])
native_input_times = fill_rust(benchname, input, rust_code_dir, wasm_out_dir, native_out_dir)
fill_rust(benchname, input, rust_code_dir, wasm_out_dir, native_out_dir)

minify_wasm(wasm_out_dir, wasm_minified_dir)

if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion wasm-engines/scripts/fill_rust.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ NATIVE_DIR=$RESULTS_DIR/rustnative

cd /scripts
pip3 install -r ./requirements.txt
python3.8 fill_rust.py --wasmoutdir $WASM_FILE_DIR --rustcodedir $RUST_CODE_DIR --inputvectorsdir $INPUT_VECTORS_DIR --nativeoutdir /rust-bin
python3.8 fill_rust.py --wasmminifieddir $WASM_MINIFIED_DIR --wasmoutdir $WASM_FILE_DIR --rustcodedir $RUST_CODE_DIR --inputvectorsdir $INPUT_VECTORS_DIR --nativeoutdir /rust-bin
24 changes: 24 additions & 0 deletions wasm-engines/scripts/wasm_minify.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#! /bin/bash

WASM_FILE_DIR=$1
WASM_MINIFIED_DIR=$2

echo "building wasm minifier tool"

git clone --single-branch --branch minify-tool https://github.com/ewasm/sentinel-rs.git sentinel-minify-tool
# .cargo/config sets default build target to wasm. we want to build x86 binary
# and use the command line tool, so delete .cargo/config
rm sentinel-minify-tool/.cargo/config
cd sentinel-minify-tool/wasm-utils/cli
cargo build --bin wasm-minify
# built binary sentinel-rs/wasm-utils/target/debug/wasm-minify

echo "minifying wasm files..."
rm -rf $WASM_MINIFIED_DIR
mkdir -p ${WASM_MINIFIED_DIR}

for filename in $WASM_FILE_DIR/*.wasm
do
dest="${WASM_MINIFIED_DIR}/$(basename $filename)"
/root/sentinel-minify-tool/wasm-utils/target/debug/wasm-minify "${filename}" "$dest"
done

0 comments on commit b99bca1

Please sign in to comment.