From 0ec24b457f531fadfc133ed9bbb4fd8694e95ce5 Mon Sep 17 00:00:00 2001 From: Sam Leonard Date: Sat, 6 Feb 2021 21:09:15 +0000 Subject: [PATCH] Move to mimalloc for rust. (#10) --- .gitignore | 5 +++++ build-rs.sh | 9 ++++++--- src/main/c/almost_pseudo_random.c | 5 ++++- src/main/rust/.gitignore | 2 ++ src/main/rust/Cargo.toml | 11 +++++++++++ src/main/rust/{ => src/bin}/almost_pseudo_random.rs | 0 src/main/rust/{ => src/bin}/rust_raw.rs | 5 +++++ src/main/rust/{ => src/bin}/rust_safer.rs | 5 +++++ 8 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 src/main/rust/.gitignore create mode 100644 src/main/rust/Cargo.toml rename src/main/rust/{ => src/bin}/almost_pseudo_random.rs (100%) rename src/main/rust/{ => src/bin}/rust_raw.rs (98%) rename src/main/rust/{ => src/bin}/rust_safer.rs (98%) diff --git a/.gitignore b/.gitignore index 1e5a5e5..5aa07fd 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,8 @@ # csharp /bin /obj + + +# Added by cargo + +/target diff --git a/build-rs.sh b/build-rs.sh index 1612307..8b6bb24 100755 --- a/build-rs.sh +++ b/build-rs.sh @@ -1,6 +1,9 @@ #!/usr/bin/env sh mkdir -p build/rust -rustc -O -o build/rust/almost_pseudo_random src/main/rust/almost_pseudo_random.rs -rustc -O -o build/rust/rust_safer src/main/rust/rust_safer.rs -rustc -O -o build/rust/rust_raw src/main/rust/rust_raw.rs +cargo build --release --manifest-path src/main/rust/Cargo.toml +find src/main/rust/target/release \ + -maxdepth 1 \ + -executable \ + -type f \ + -exec cp -f {} build/rust \; diff --git a/src/main/c/almost_pseudo_random.c b/src/main/c/almost_pseudo_random.c index 68e52f2..3297ae4 100644 --- a/src/main/c/almost_pseudo_random.c +++ b/src/main/c/almost_pseudo_random.c @@ -1,5 +1,6 @@ /* * Copyright 2021 Kazimierz Pogoda + * Copyright 2021 Sam Leonard * * This file is part of java-2-times-faster-than-c. * @@ -26,10 +27,12 @@ double almost_pseudo_random(long ordinal) { return fmod(sin(((double) ordinal) * 100000.0) + 1.0, 1.0); } -void main() { +int main() { double checksum = 0; for (long i = 0; i < ITERATION_COUNT; i++) { checksum += almost_pseudo_random(i); } printf("checksum: %f\n", checksum); + + return 0; } diff --git a/src/main/rust/.gitignore b/src/main/rust/.gitignore new file mode 100644 index 0000000..a9d37c5 --- /dev/null +++ b/src/main/rust/.gitignore @@ -0,0 +1,2 @@ +target +Cargo.lock diff --git a/src/main/rust/Cargo.toml b/src/main/rust/Cargo.toml new file mode 100644 index 0000000..260008b --- /dev/null +++ b/src/main/rust/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "rust" +version = "0.1.0" +authors = ["Sam "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +# compile mimalloc for speed not security +mimalloc = { version = "~0.1.24", default-features = false } diff --git a/src/main/rust/almost_pseudo_random.rs b/src/main/rust/src/bin/almost_pseudo_random.rs similarity index 100% rename from src/main/rust/almost_pseudo_random.rs rename to src/main/rust/src/bin/almost_pseudo_random.rs diff --git a/src/main/rust/rust_raw.rs b/src/main/rust/src/bin/rust_raw.rs similarity index 98% rename from src/main/rust/rust_raw.rs rename to src/main/rust/src/bin/rust_raw.rs index de95221..59ee502 100644 --- a/src/main/rust/rust_raw.rs +++ b/src/main/rust/src/bin/rust_raw.rs @@ -19,6 +19,11 @@ use std::ptr; +use mimalloc::MiMalloc; + +#[global_allocator] +static GLOBAL: MiMalloc = MiMalloc; + const MAX_PAYLOAD_SIZE: i32 = 50; const INITIAL_NODE_COUNT: i32 = 10000; const MUTATION_COUNT: i64 = 1000000; diff --git a/src/main/rust/rust_safer.rs b/src/main/rust/src/bin/rust_safer.rs similarity index 98% rename from src/main/rust/rust_safer.rs rename to src/main/rust/src/bin/rust_safer.rs index a8b3b5a..8768a19 100644 --- a/src/main/rust/rust_safer.rs +++ b/src/main/rust/src/bin/rust_safer.rs @@ -19,6 +19,11 @@ use std::ptr; +use mimalloc::MiMalloc; + +#[global_allocator] +static GLOBAL: MiMalloc = MiMalloc; + const MAX_PAYLOAD_SIZE: i32 = 50; const INITIAL_NODE_COUNT: i32 = 10000; const MUTATION_COUNT: i64 = 1000000;