diff --git a/flake.nix b/flake.nix index d3f7caf..80ec2a0 100644 --- a/flake.nix +++ b/flake.nix @@ -19,6 +19,40 @@ inherit system; overlays = [ moz_overlay.overlay ]; }; + wavm = pkgs.stdenv.mkDerivation rec { + pname = "wavm"; + version = "0.0.0"; + + src = pkgs.fetchurl { + url = "https://github.com/WAVM/WAVM/releases/download/nightly%2F2022-05-14/wavm-0.0.0-prerelease-linux.tar.gz"; + sha256 = "sha256-+PpnwPJDty6XCjjuHVFwiHc1q+k0zPF11EbRpqSKfyY="; + }; + + nativeBuildInputs = [ + pkgs.autoPatchelfHook + ]; + + buildInputs = [ + pkgs.stdenv.cc.cc.lib + ]; + + sourceRoot = "."; + + installPhase = '' + mkdir -p $out/bin + cp -r bin/wavm $out/bin/ + cp -r include/ $out + cp -r lib64/ $out + cp -r share/ $out + #install -m755 -D studio-link-standalone-v${version} $out/bin/studio-link + ''; + + meta = with pkgs.lib; { + homepage = "https://wavm.github.io/"; + description = "WAVM WebAssembly Engine"; + platforms = platforms.linux; + }; + }; in { devShell = pkgs.mkShell { buildInputs = with pkgs; [ @@ -35,6 +69,8 @@ clang kakoune + wavm + hyperfine graph-cli (rust-bin.stable.latest.default.override { targets = [ "wasm32-wasi" ]; diff --git a/koka_bench/CMakeLists.txt b/koka_bench/CMakeLists.txt index 0ab96c7..c69071f 100644 --- a/koka_bench/CMakeLists.txt +++ b/koka_bench/CMakeLists.txt @@ -15,3 +15,6 @@ add_subdirectory(haskell) add_subdirectory(java) add_subdirectory(ocaml) add_subdirectory(swift) + +add_subdirectory(python) +add_subdirectory(scheme) diff --git a/koka_bench/copy_wrapper.sh b/koka_bench/copy_wrapper.sh new file mode 100755 index 0000000..cf919ee --- /dev/null +++ b/koka_bench/copy_wrapper.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +OUR_DIR="$(dirname $(readlink -f $0))" +SOURCE="$1" +OUT_DIR="$2" +OUT_NAME="$3" + +mkdir -p "$OUT_DIR" +cp $SOURCE "$OUT_DIR/$OUT_NAME" diff --git a/koka_bench/cpp/CMakeLists.txt b/koka_bench/cpp/CMakeLists.txt index 3183c1f..264f5da 100644 --- a/koka_bench/cpp/CMakeLists.txt +++ b/koka_bench/cpp/CMakeLists.txt @@ -2,7 +2,7 @@ set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED YES) set(CMAKE_CXX_EXTENSIONS NO) -foreach (source IN ITEMS rbtree.cpp nqueens.cpp cfold.cpp deriv.cpp) +foreach (source IN ITEMS rbtree.cpp nqueens.cpp cfold.cpp deriv.cpp fib.cpp) get_filename_component(name "${source}" NAME_WE) set(name "cpp-${name}") diff --git a/koka_bench/cpp/fib.cpp b/koka_bench/cpp/fib.cpp new file mode 100644 index 0000000..f002a40 --- /dev/null +++ b/koka_bench/cpp/fib.cpp @@ -0,0 +1,17 @@ +#include +#include +#include + +int fib(int n) { + if (n == 0) { + return 1; + } else if (n == 1) { + return 1; + } else { + return fib(n-1) + fib(n-2); + } +} +int main(int argc, char **argv) { + printf("%d\n", fib(atoi(argv[1]))); + return 0; +} diff --git a/koka_bench/koka/CMakeLists.txt b/koka_bench/koka/CMakeLists.txt index 34da0df..70dd914 100644 --- a/koka_bench/koka/CMakeLists.txt +++ b/koka_bench/koka/CMakeLists.txt @@ -1,4 +1,4 @@ -set(sources rbtree.kk nqueens.kk nqueens-int.kk cfold.kk deriv.kk) +set(sources rbtree.kk nqueens.kk nqueens-int.kk cfold.kk deriv.kk fib.kk) set(koka koka) diff --git a/koka_bench/koka/fib.kk b/koka_bench/koka/fib.kk new file mode 100644 index 0000000..6279802 --- /dev/null +++ b/koka_bench/koka/fib.kk @@ -0,0 +1,13 @@ +module nqueens +import std/num/int32 +import std/os/env + + +pub fun fib( n : int ) : div int + match n + 0 -> 1 + 1 -> 1 + x -> fib(x - 1) + fib(x - 2) + +pub fun main() + fib(get-args().head("").parse-int.default(30)).println diff --git a/koka_bench/kraken/CMakeLists.txt b/koka_bench/kraken/CMakeLists.txt index 81f65fe..c7c8dde 100644 --- a/koka_bench/kraken/CMakeLists.txt +++ b/koka_bench/kraken/CMakeLists.txt @@ -1,4 +1,4 @@ -set(sources rbtree.kp rbtree-opt.kp nqueens.kp cfold.kp deriv.kp) +set(sources rbtree.kp rbtree-opt.kp nqueens.kp cfold.kp deriv.kp fib.kp fib-let.kp) set(kraken "../../kraken_wrapper.sh") diff --git a/koka_bench/kraken/fib-let.kp b/koka_bench/kraken/fib-let.kp new file mode 100644 index 0000000..baab7d2 --- /dev/null +++ b/koka_bench/kraken/fib-let.kp @@ -0,0 +1,38 @@ + +((wrap (vau root_env (quote) +((wrap (vau (let1) +(let1 lambda (vau se (p b1) (wrap (eval (array vau p b1) se))) +(let1 current-env (vau de () de) +(let1 cons (lambda (h t) (concat (array h) t)) +(let1 Y (lambda (f3) + ((lambda (x1) (x1 x1)) + (lambda (x2) (f3 (wrap (vau app_env (& y) (lapply (x2 x2) y app_env))))))) +(let1 vY (lambda (f) + ((lambda (x3) (x3 x3)) + (lambda (x4) (f (vau de1 (& y) (vapply (x4 x4) y de1)))))) +(let1 let (vY (lambda (recurse) (vau de2 (vs b) (cond (= (len vs) 0) (eval b de2) + true (vapply let1 (array (idx vs 0) (idx vs 1) (array recurse (slice vs 2 -1) b)) de2))))) + (let ( + lcompose (lambda (g f) (lambda (& args) (lapply g (array (lapply f args))))) + rec-lambda (vau se (n p b) (eval (array Y (array lambda (array n) (array lambda p b))) se)) + + fib (rec-lambda fib (n) (cond (= 0 n) 1 + (= 1 n) 1 + true (let ( + fib_minus_1 (fib (- n 1)) + fib_minus_2 (fib (- n 2)) + ) (+ fib_minus_1 fib_minus_2)))) + + monad (array 'write 1 (str "running fib") (vau (written code) + (array 'args (vau (args code) + (array 'exit (fib (read-string (idx args 1)))) + )) + )) + + ) monad) +; end of all lets +)))))) +; impl of let1 +)) (vau de (s v b) (eval (array (array wrap (array vau (array s) b)) v) de))) +; impl of quote +)) (vau (x5) x5)) diff --git a/koka_bench/kraken/fib.kp b/koka_bench/kraken/fib.kp new file mode 100644 index 0000000..7fcfdbf --- /dev/null +++ b/koka_bench/kraken/fib.kp @@ -0,0 +1,35 @@ + +((wrap (vau root_env (quote) +((wrap (vau (let1) +(let1 lambda (vau se (p b1) (wrap (eval (array vau p b1) se))) +(let1 current-env (vau de () de) +(let1 cons (lambda (h t) (concat (array h) t)) +(let1 Y (lambda (f3) + ((lambda (x1) (x1 x1)) + (lambda (x2) (f3 (wrap (vau app_env (& y) (lapply (x2 x2) y app_env))))))) +(let1 vY (lambda (f) + ((lambda (x3) (x3 x3)) + (lambda (x4) (f (vau de1 (& y) (vapply (x4 x4) y de1)))))) +(let1 let (vY (lambda (recurse) (vau de2 (vs b) (cond (= (len vs) 0) (eval b de2) + true (vapply let1 (array (idx vs 0) (idx vs 1) (array recurse (slice vs 2 -1) b)) de2))))) + (let ( + lcompose (lambda (g f) (lambda (& args) (lapply g (array (lapply f args))))) + rec-lambda (vau se (n p b) (eval (array Y (array lambda (array n) (array lambda p b))) se)) + + fib (rec-lambda fib (n) (cond (= 0 n) 1 + (= 1 n) 1 + true (+ (fib (- n 1)) (fib (- n 2))))) + + monad (array 'write 1 (str "running fib") (vau (written code) + (array 'args (vau (args code) + (array 'exit (fib (read-string (idx args 1)))) + )) + )) + + ) monad) +; end of all lets +)))))) +; impl of let1 +)) (vau de (s v b) (eval (array (array wrap (array vau (array s) b)) v) de))) +; impl of quote +)) (vau (x5) x5)) diff --git a/koka_bench/kraken_wrapper.sh b/koka_bench/kraken_wrapper.sh index e64f7df..a3540ec 100755 --- a/koka_bench/kraken_wrapper.sh +++ b/koka_bench/kraken_wrapper.sh @@ -10,7 +10,14 @@ mv ./csc_out.wasm "$OUT_DIR/$OUT_NAME.wasm" printf '#!/usr/bin/env bash\nwasmtime "$(dirname $(readlink -f $0))/'"$OUT_NAME"'.wasm" $@' > "$OUT_DIR/$OUT_NAME" chmod 755 "$OUT_DIR/$OUT_NAME" +printf '#!/usr/bin/env bash\nWAVM_OBJECT_CACHE_DIR=$(pwd) wavm run "$(dirname $(readlink -f $0))/'"$OUT_NAME"'.wasm" $@' > "$OUT_DIR/$OUT_NAME-wavm" +chmod 755 "$OUT_DIR/$OUT_NAME-wavm" + + scheme --script "$OUR_DIR/../partial_eval.scm" $SOURCE no_compile mv ./csc_out.wasm "$OUT_DIR/$OUT_NAME-slow.wasm" printf '#!/usr/bin/env bash\nwasmtime "$(dirname $(readlink -f $0))/'"$OUT_NAME-slow"'.wasm" $@' > "$OUT_DIR/$OUT_NAME-slow" chmod 755 "$OUT_DIR/$OUT_NAME-slow" + +printf '#!/usr/bin/env bash\nWAVM_OBJECT_CACHE_DIR=$(pwd) wavm run "$(dirname $(readlink -f $0))/'"$OUT_NAME-slow"'.wasm" $@' > "$OUT_DIR/$OUT_NAME-slow-wavm" +chmod 755 "$OUT_DIR/$OUT_NAME-slow-wavm" diff --git a/koka_bench/python/CMakeLists.txt b/koka_bench/python/CMakeLists.txt new file mode 100644 index 0000000..4fc0b25 --- /dev/null +++ b/koka_bench/python/CMakeLists.txt @@ -0,0 +1,22 @@ + +set(copy_wrapper "../../copy_wrapper.sh") + +set(sources python-fib.py python-fib-let.py) +foreach (source IN LISTS sources) + + get_filename_component(name "${source}" NAME_WE) + + set(out_dir "${CMAKE_CURRENT_BINARY_DIR}/out/bench") + set(out_path "${out_dir}/${name}") + + add_custom_command( + OUTPUT ${out_path} + COMMAND ${copy_wrapper} "${CMAKE_CURRENT_SOURCE_DIR}/${source}" ${out_dir} ${name} + DEPENDS ${source} + VERBATIM) + + add_custom_target(update-${name} ALL DEPENDS "${out_path}") + add_executable(${name}-exe IMPORTED) + set_target_properties(${name}-exe PROPERTIES IMPORTED_LOCATION "${out_path}") +endforeach () + diff --git a/koka_bench/python/python-fib-let.py b/koka_bench/python/python-fib-let.py new file mode 100755 index 0000000..81685e4 --- /dev/null +++ b/koka_bench/python/python-fib-let.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python3 + +import sys +def fib(n): + if n == 0: + return 1 + elif n == 1: + return 1 + else: + r1 = fib(n-1) + r2 = fib(n-2) + return r1 + r2 +print(fib(int(sys.argv[1]))) diff --git a/koka_bench/python/python-fib.py b/koka_bench/python/python-fib.py new file mode 100755 index 0000000..7dc1a67 --- /dev/null +++ b/koka_bench/python/python-fib.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python3 + +import sys +def fib(n): + if n == 0: + return 1 + elif n == 1: + return 1 + else: + return fib(n-1) + fib(n-2) +print(fib(int(sys.argv[1]))) diff --git a/koka_bench/scheme/CMakeLists.txt b/koka_bench/scheme/CMakeLists.txt new file mode 100644 index 0000000..429f3b9 --- /dev/null +++ b/koka_bench/scheme/CMakeLists.txt @@ -0,0 +1,22 @@ + +set(copy_wrapper "../../copy_wrapper.sh") + +set(sources scheme-fib.scm scheme-fib-let.scm) +foreach (source IN LISTS sources) + + get_filename_component(name "${source}" NAME_WE) + + set(out_dir "${CMAKE_CURRENT_BINARY_DIR}/out/bench") + set(out_path "${out_dir}/${name}") + + add_custom_command( + OUTPUT ${out_path} + COMMAND ${copy_wrapper} "${CMAKE_CURRENT_SOURCE_DIR}/${source}" ${out_dir} ${name} + DEPENDS ${source} + VERBATIM) + + add_custom_target(update-${name} ALL DEPENDS "${out_path}") + add_executable(${name}-exe IMPORTED) + set_target_properties(${name}-exe PROPERTIES IMPORTED_LOCATION "${out_path}") +endforeach () + diff --git a/koka_bench/scheme/scheme-fib-let.scm b/koka_bench/scheme/scheme-fib-let.scm new file mode 100755 index 0000000..5d8ae47 --- /dev/null +++ b/koka_bench/scheme/scheme-fib-let.scm @@ -0,0 +1,8 @@ +#!/usr/bin/env -S scheme --script +(pretty-print ((letrec ((fib (lambda (n) (cond ((equal? n 0) 1) + ((equal? n 1) 1) + (#t (let ( + (r1 (fib (- n 1))) + (r2 (fib (- n 2))) + ) (+ r1 r2))))))) + fib) (read (open-input-string (list-ref (command-line) 1))))) diff --git a/koka_bench/scheme/scheme-fib.scm b/koka_bench/scheme/scheme-fib.scm new file mode 100755 index 0000000..1d55658 --- /dev/null +++ b/koka_bench/scheme/scheme-fib.scm @@ -0,0 +1,5 @@ +#!/usr/bin/env -S scheme --script +(pretty-print ((letrec ((fib (lambda (n) (cond ((equal? n 0) 1) + ((equal? n 1) 1) + (#t (+ (fib (- n 1)) (fib (- n 2)))))))) + fib) (read (open-input-string (list-ref (command-line) 1))))) diff --git a/koka_bench/test.sh b/koka_bench/test.sh index 49fed51..fceb09c 100755 --- a/koka_bench/test.sh +++ b/koka_bench/test.sh @@ -16,16 +16,18 @@ mkdir -p slow find build -type f -name \*slow\* -exec mv {} slow \; cp ./build/kraken/out/bench/kraken-* ./slow - +nix develop -i -c bash -c 'ulimit -s unlimited && find build -type f -executable -name \*fib\* -printf "\"%p 30\"\n" | xargs hyperfine --ignore-failure --warmup 2 --export-markdown fib_table.md --export-csv fib_table.csv' nix develop -i -c bash -c 'ulimit -s unlimited && find build -type f -executable -name \*nqueens\* -printf "\"%p 10\"\n" | xargs hyperfine --ignore-failure --warmup 2 --export-markdown nqueens_table.md --export-csv nqueens_table.csv' nix develop -i -c bash -c 'ulimit -s unlimited && find build -type f -executable -name \*rbtree\* -printf "\"%p 42000\"\n" | xargs hyperfine --ignore-failure --warmup 2 --export-markdown rbtree_table.md --export-csv rbtree_table.csv' nix develop -i -c bash -c 'ulimit -s unlimited && find build -type f -executable -name \*cfold\* -printf "\"%p 5\"\n" | xargs hyperfine --ignore-failure --warmup 2 --export-markdown cfold_table.md --export-csv cfold_table.csv' nix develop -i -c bash -c 'ulimit -s unlimited && find build -type f -executable -name \*deriv\* -printf "\"%p 8\"\n" | xargs hyperfine --ignore-failure --warmup 2 --export-markdown deriv_table.md --export-csv deriv_table.csv' -#nix develop -i -c bash -c 'ulimit -s unlimited && find slow -type f -executable -name \*nqueens\* -printf "\"%p 7\"\n" | xargs hyperfine --ignore-failure --warmup 2 --export-markdown slow_nqueens_table.md --export-csv slow_nqueens_table.csv' -#nix develop -i -c bash -c 'ulimit -s unlimited && find slow -type f -executable -name \*cfold\* -printf "\"%p 5\"\n" | xargs hyperfine --ignore-failure --warmup 2 --export-markdown slow_cfold_table.md --export-csv slow_cfold_table.csv' -#nix develop -i -c bash -c 'ulimit -s unlimited && find slow -type f -executable -name \*deriv\* -printf "\"%p 3\"\n" | xargs hyperfine --ignore-failure --warmup 2 --export-markdown slow_deriv_table.md --export-csv slow_deriv_table.csv' -#nix develop -i -c bash -c 'ulimit -s unlimited && find slow -type f -executable -name \*rbtree\* -printf "\"%p 100\"\n" | xargs hyperfine --ignore-failure --warmup 2 --export-markdown slow_rbtree_table.md --export-csv slow_rbtree_table.csv' + +nix develop -i -c bash -c 'ulimit -s unlimited && find slow -type f -executable -name \*nqueens\* -printf "\"%p 7\"\n" | xargs hyperfine --ignore-failure --warmup 2 --export-markdown slow_nqueens_table.md --export-csv slow_nqueens_table.csv' +nix develop -i -c bash -c 'ulimit -s unlimited && find slow -type f -executable -name \*cfold\* -printf "\"%p 5\"\n" | xargs hyperfine --ignore-failure --warmup 2 --export-markdown slow_cfold_table.md --export-csv slow_cfold_table.csv' +nix develop -i -c bash -c 'ulimit -s unlimited && find slow -type f -executable -name \*deriv\* -printf "\"%p 3\"\n" | xargs hyperfine --ignore-failure --warmup 2 --export-markdown slow_deriv_table.md --export-csv slow_deriv_table.csv' +nix develop -i -c bash -c 'ulimit -s unlimited && find slow -type f -executable -name \*rbtree\* -printf "\"%p 100\"\n" | xargs hyperfine --ignore-failure --warmup 2 --export-markdown slow_rbtree_table.md --export-csv slow_rbtree_table.csv' +nix develop -i -c bash -c 'ulimit -s unlimited && find slow -type f -executable -name \*fib\* -printf "\"%p 30\"\n" | xargs hyperfine --ignore-failure --warmup 2 --export-markdown slow_fib_table.md --export-csv slow_fib_table.csv' for x in *_table.csv do