diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 130e1febf..c41d24574 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -10,14 +10,35 @@ env: CARGO_TERM_COLOR: always jobs: - build: - + check-format: runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Check formatting + run: cargo fmt -- --check + build: + runs-on: ubuntu-latest + needs: check-format steps: - uses: actions/checkout@v3 - name: Build run: cargo build --verbose + + clippy: + runs-on: ubuntu-latest + needs: build + steps: + - uses: actions/checkout@v3 + - name: Run Clippy + run: cargo clippy --all --all-features + + test: + runs-on: ubuntu-latest + needs: + - clippy + steps: + - uses: actions/checkout@v3 - name: Run tests run: cargo test --verbose -- --test-threads=1 - name: Install cargo-llvm-cov @@ -30,4 +51,3 @@ jobs: files: codecov.json fail_ci_if_error: true uses: codecov/codecov-action@v3 - diff --git a/CHANGELOG.md b/CHANGELOG.md index a1f70af10..1c4c57099 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,22 @@ # Changelog +## v0.2.5 (2024-01-31) + +**Improves** + +* Refactors count_octal_digits to use ilog2 internally. +* Refactors count_hex_digits to use ilog2 internally. + +The v0.2.4 benchmarks show that `ilog2`, used by `count_bits`, has the same performance regardless of the size of the integer input. + +The v0.2.4 benchmarks show that `count_octal_digits` and `count_hex_digits` functions, which were previously using `ilog(8)` and `ilog(16)` respectively, did show decreased performance based on the size of the integer input. + +![A graph of count-octal-digits benchmarks for v0.2.4](https://raw.githubusercontent.com/nordzilla/count-digits/main/benches/images/count-octal-digits-v0.2.4.png) + +The v0.2.5 `count_octal_digits` and `count_hex_digits` are now implemented internally using `ilog2`, and now share that consistent, improved performance across the board. + +![A graph of count-octal-digits benchmarks for v0.2.5](https://raw.githubusercontent.com/nordzilla/count-digits/main/benches/images/count-octal-digits-v0.2.5.png) + ## v0.2.4 (2024-01-30) **Adds** @@ -9,10 +26,13 @@ **Changes** -* Removes unnecessary `checked_ilog` functions from the implementations of various `CountDigits` functions. * Reworks testing macros to make the test output more granular. * Reworks the benchmark test boundaries to test iterations for each hexadecimal digit added to an integer, from one to max, for each integer type. +**Improves** + +* Removes unnecessary `checked_ilog` functions from the implementations of various `CountDigits` functions. + ## v0.2.3 (2024-01-26) **Changes** diff --git a/Cargo.toml b/Cargo.toml index 12205fa7e..ab7b3910e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "count-digits" -version = "0.2.4" +version = "0.2.5" authors = ["Erik Nordin "] description = "A no-std trait to count the digits of integer types in various number bases." homepage = "https://github.com/nordzilla/count-digits" diff --git a/benches/benchmark.rs b/benches/benchmark.rs index d666b80b8..efcc7c5fe 100644 --- a/benches/benchmark.rs +++ b/benches/benchmark.rs @@ -42,7 +42,7 @@ impl_maybe_from_u128!(u128, NonZeroU128); macro_rules! radix_boundaries { ($type:ty, $radix:expr) => { std::iter::successors(Some($radix as $type), move |n| n.checked_mul($radix)) - .map(|n| n - 1) + .map(|n| (n - 1) / 3) }; } @@ -50,9 +50,9 @@ macro_rules! comparison_bench_function { ($group:expr, $type:ty, $fn:ident, $input:expr) => { if let Some(n) = <$type>::maybe_from_u128(*$input) { $group.bench_with_input( - BenchmarkId::new(stringify!($type), $input), - $input, - |b, _| b.iter(|| CountDigits::$fn(black_box(n))), + BenchmarkId::new(stringify!($type), $input.count_hex_digits()), + &$input.count_hex_digits(), + |b, _| b.iter(move || CountDigits::$fn(black_box(n))), ); } }; diff --git a/benches/images/count-octal-digits-v0.2.4.png b/benches/images/count-octal-digits-v0.2.4.png new file mode 100644 index 000000000..8d4b5b6a6 Binary files /dev/null and b/benches/images/count-octal-digits-v0.2.4.png differ diff --git a/benches/images/count-octal-digits-v0.2.5.png b/benches/images/count-octal-digits-v0.2.5.png new file mode 100644 index 000000000..9da479db2 Binary files /dev/null and b/benches/images/count-octal-digits-v0.2.5.png differ diff --git a/src/lib.rs b/src/lib.rs index c9a809ae2..1cac58e53 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,7 +27,7 @@ //! --- //! //!
-//! The base-10 functions +//! The base-10 functions //! //! count_digits() //! @@ -110,9 +110,9 @@ //! assert_eq!(1, format!("{ }", -1_i8).strip_prefix('-').unwrap().chars().count()); //! assert_eq!(2, format!("{:x}", -1_i8).chars().count()); //! ``` -//! +//! //! ### Benchmarks -//! +//! //! * [table](https://nordzilla.github.io/count-digits) //! * [count_bits()](https://nordzilla.github.io/count-digits/count_bits/report/index.html) //! * [count_octal_digits()](https://nordzilla.github.io/count-digits/count_octal_digits/report/index.html) @@ -573,11 +573,11 @@ pub trait CountDigits: Copy + Sized { fn count_hex_digits(self) -> u32; /// Returns the count of digits in an integer as interpreted with the given [radix](https://en.wikipedia.org/wiki/Radix). - /// + /// ///
/// For radix 10, does not count the negative sign when counting negative, signed integers. - /// - /// For all other radix values, counts digits according to the + /// + /// For all other radix values, counts digits according to the /// twos-complement representation. ///
/// @@ -630,7 +630,7 @@ macro_rules! impl_count_digits { if self.is_negative() { $min_value_octal_digits } else { - 1 + self.checked_ilog(8).unwrap_or_default() + 1 + self.checked_ilog2().unwrap_or_default() / 3 } } @@ -640,7 +640,7 @@ macro_rules! impl_count_digits { if self.is_negative() { $min_value_hex_digits } else { - 1 + self.checked_ilog(16).unwrap_or_default() + 1 + self.checked_ilog2().unwrap_or_default() / 4 } } @@ -688,7 +688,7 @@ macro_rules! impl_count_digits { if self.is_negative() { $min_value_octal_digits } else { - 1 + self.get().ilog(8) + 1 + self.get().ilog2() / 3 } } @@ -698,7 +698,7 @@ macro_rules! impl_count_digits { if self.is_negative() { $min_value_hex_digits } else { - 1 + self.get().ilog(16) + 1 + self.get().ilog2() / 4 } } @@ -743,13 +743,13 @@ macro_rules! impl_count_digits { #[inline(always)] /// Returns the count of octal digits in an integer starting with the first non-zero digit. fn count_octal_digits(self) -> u32 { - 1 + self.checked_ilog(8).unwrap_or_default() + 1 + self.checked_ilog2().unwrap_or_default() / 3 } #[inline(always)] /// Returns the count of hexadecimal digits in an integer starting with the first non-zero digit. fn count_hex_digits(self) -> u32 { - 1 + self.checked_ilog(16).unwrap_or_default() + 1 + self.checked_ilog2().unwrap_or_default() / 4 } #[inline(always)] @@ -783,13 +783,13 @@ macro_rules! impl_count_digits { #[inline(always)] /// Returns the count of octal digits in an integer starting with the first non-zero digit. fn count_octal_digits(self) -> u32 { - 1 + self.get().ilog(8) + 1 + self.get().ilog2() / 3 } #[inline(always)] /// Returns the count of hexadecimal digits in an integer starting with the first non-zero digit. fn count_hex_digits(self) -> u32 { - 1 + self.get().ilog(16) + 1 + self.get().ilog2() / 4 } #[inline(always)]