From 35907dad8212eb85390d87531f3389d57011a650 Mon Sep 17 00:00:00 2001 From: Brian Hicks Date: Tue, 24 Aug 2021 17:23:35 -0500 Subject: [PATCH] use a bump allocator again --- BENCHMARKING.md | 21 +++++++++++++++++++++ Cargo.lock | 38 ++++++++++++++++++++++++++++++++++++-- Cargo.toml | 3 ++- src/main.rs | 4 ++++ 4 files changed, 63 insertions(+), 3 deletions(-) diff --git a/BENCHMARKING.md b/BENCHMARKING.md index c29baa5..308f225 100644 --- a/BENCHMARKING.md +++ b/BENCHMARKING.md @@ -170,3 +170,24 @@ Summary ``` Yep! + +## Bump Allocation (again) + +Let's try the bump allocator again... it seemed like a fluke that it produced such a severe performance degradation. + +``` +$ hyperfine './result/bin/similar-sort define < /usr/share/dict/words' './target/release/similar-sort benchmark < /usr/share/dict/words' +Benchmark #1: ./result/bin/similar-sort define < /usr/share/dict/words + Time (mean ± σ): 286.2 ms ± 5.0 ms [User: 251.4 ms, System: 72.5 ms] + Range (min … max): 280.8 ms … 298.4 ms 10 runs + +Benchmark #2: ./target/release/similar-sort benchmark < /usr/share/dict/words + Time (mean ± σ): 111.3 ms ± 3.4 ms [User: 94.7 ms, System: 16.0 ms] + Range (min … max): 104.1 ms … 118.5 ms 24 runs + +Summary + './target/release/similar-sort benchmark < /usr/share/dict/words' ran + 2.57 ± 0.09 times faster than './result/bin/similar-sort define < /usr/share/dict/words' +``` + +That seems more like what I'd expect! diff --git a/Cargo.lock b/Cargo.lock index 13b4edb..e006877 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -21,7 +21,7 @@ version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" dependencies = [ - "winapi", + "winapi 0.3.9", ] [[package]] @@ -32,7 +32,7 @@ checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" dependencies = [ "hermit-abi", "libc", - "winapi", + "winapi 0.3.9", ] [[package]] @@ -62,6 +62,17 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bump_alloc" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0272cee31aeb08593d1b75da4d942cb24ff6f4a08571a1d410aa7e7500ad8b37" +dependencies = [ + "kernel32-sys", + "libc", + "winapi 0.3.9", +] + [[package]] name = "cc" version = "1.0.69" @@ -206,6 +217,16 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683" +[[package]] +name = "kernel32-sys" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" +dependencies = [ + "winapi 0.2.8", + "winapi-build", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -372,6 +393,7 @@ dependencies = [ name = "similar-sort" version = "2.0.0" dependencies = [ + "bump_alloc", "color-eyre", "rayon", "strsim 0.10.0", @@ -526,6 +548,12 @@ version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" +[[package]] +name = "winapi" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" + [[package]] name = "winapi" version = "0.3.9" @@ -536,6 +564,12 @@ dependencies = [ "winapi-x86_64-pc-windows-gnu", ] +[[package]] +name = "winapi-build" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" + [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index d91660b..2a3e86a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,8 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -structopt = "0.3.22" +bump_alloc = "0.1.0" color-eyre = "0.5.7" rayon = "1.5.1" strsim = "0.10.0" +structopt = "0.3.22" diff --git a/src/main.rs b/src/main.rs index 6490164..20e103c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,9 @@ use std::io::{self, stdin, stdout, BufRead, BufWriter, Write}; use strsim::levenshtein; use structopt::StructOpt; +#[global_allocator] +static A: bump_alloc::BumpAlloc = bump_alloc::BumpAlloc::new(); + // works like `sort`, but sorts according to Levenshtein distance instead of // alphanumerically. #[derive(StructOpt)] @@ -42,6 +45,7 @@ fn try_main() -> Result<()> { for (_, candidate) in distances { writeln!(out, "{}", candidate).context("could not write to stdout")?; } + out.flush().context("could not finish writing to stdout")?; Ok(()) }