From 2260a714b01a89622de5d2e57b2b0cfbea3dd684 Mon Sep 17 00:00:00 2001 From: Roman Lebedev Date: Wed, 15 Apr 2026 05:23:09 +0300 Subject: [PATCH] OffsetArray2DRef: and go back to "stupid" approach, which optimizes better... --- src/std/bound_coord/mod.rs | 6 ++- src/std/bound_numerics/mod.rs | 14 +++++-- src/std/ndslice/Cargo.toml | 1 - src/std/ndslice/offsetarray2dref/iteration.rs | 39 +++++++------------ src/std/ndslice/offsetarray2dref/mod.rs | 28 ++++++++++--- src/std/wrapping_numerics_arith/mod.rs | 18 ++++++--- 6 files changed, 66 insertions(+), 40 deletions(-) diff --git a/src/std/bound_coord/mod.rs b/src/std/bound_coord/mod.rs index f494b1b8..87de03c9 100644 --- a/src/std/bound_coord/mod.rs +++ b/src/std/bound_coord/mod.rs @@ -20,7 +20,11 @@ macro_rules! wrap { #[inline] #[must_use] - pub const fn value(&self) -> $ty { + pub fn value(&self) -> $ty { + #[expect(unsafe_code, clippy::undocumented_unsafe_blocks)] + unsafe { + core::hint::assert_unchecked(self.value < self.domain); + } self.value } diff --git a/src/std/bound_numerics/mod.rs b/src/std/bound_numerics/mod.rs index 6d742210..60988e4f 100644 --- a/src/std/bound_numerics/mod.rs +++ b/src/std/bound_numerics/mod.rs @@ -15,7 +15,7 @@ where #[inline] #[must_use] pub fn new(domain: T) -> Option { - if domain == ConstZero::ZERO { + if domain <= ConstZero::ZERO { return None; } Some(Self { domain }) @@ -23,7 +23,11 @@ where #[inline] #[must_use] - pub const fn domain(&self) -> &T { + pub fn domain(&self) -> &T { + #[expect(unsafe_code, clippy::undocumented_unsafe_blocks)] + unsafe { + core::hint::assert_unchecked(self.domain > ConstZero::ZERO); + } &self.domain } } @@ -64,7 +68,11 @@ where #[inline] #[must_use] - pub const fn value(&self) -> &T { + pub fn value(&self) -> &T { + #[expect(unsafe_code, clippy::undocumented_unsafe_blocks)] + unsafe { + core::hint::assert_unchecked(self.value < *self.domain); + } &self.value } diff --git a/src/std/ndslice/Cargo.toml b/src/std/ndslice/Cargo.toml index e0510d52..0ddcbbd0 100644 --- a/src/std/ndslice/Cargo.toml +++ b/src/std/ndslice/Cargo.toml @@ -15,7 +15,6 @@ workspace = true [dependencies] rawspeed-common-exact_ops = { workspace = true } rawspeed-std = { workspace = true } -rawspeed-std-range_rotation = { workspace = true } [lib] path = "mod.rs" diff --git a/src/std/ndslice/offsetarray2dref/iteration.rs b/src/std/ndslice/offsetarray2dref/iteration.rs index 2ed7b957..952d4174 100644 --- a/src/std/ndslice/offsetarray2dref/iteration.rs +++ b/src/std/ndslice/offsetarray2dref/iteration.rs @@ -1,9 +1,5 @@ +use crate::offsetarray2dref::OffsetArray2DRef; use rawspeed_std::coord_common::{ColIndex, Coord2D, RowIndex}; -use rawspeed_std_range_rotation::range_rotation::rotated_range::{ - RotatableRange as _, RotatedRange, -}; - -use crate::{array2dref::Array2DRef, offsetarray2dref::OffsetArray2DRef}; #[non_exhaustive] #[must_use] @@ -17,7 +13,7 @@ pub struct Rows<'a, 'b, T> { #[derive(Debug, Clone)] pub struct RowIterator<'a, 'b, T> { underlying: &'b OffsetArray2DRef<'a, T>, - rotated_rows: as IntoIterator>::IntoIter, + rows: as IntoIterator>::IntoIter, } #[non_exhaustive] @@ -40,14 +36,14 @@ pub struct Columns<'a, 'b, 'c, T> { #[derive(Debug, Clone)] pub struct ColumnIterator<'a, 'b, 'c, T> { underlying: &'c Row<'a, 'b, T>, - rotated_cols: as IntoIterator>::IntoIter, + cols: as IntoIterator>::IntoIter, } #[non_exhaustive] #[must_use] #[derive(Debug, Clone, Copy)] pub struct Element<'a, 'b, T> { - underlying: &'b Array2DRef<'a, T>, + underlying: &'b OffsetArray2DRef<'a, T>, coord: Coord2D, } @@ -75,12 +71,8 @@ impl<'a, 'b, T> RowIterator<'a, 'b, T> { #[inline] fn new(underlying: &'b OffsetArray2DRef<'a, T>) -> Self { let rows = 0..underlying.num_rows().get(); - let rotated_rows = rows.rotate(*underlying.origin.row()); - let rotated_rows = rotated_rows.into_iter(); - Self { - underlying, - rotated_rows, - } + let rows = rows.into_iter(); + Self { underlying, rows } } } @@ -90,7 +82,7 @@ impl<'a, 'b, T> Iterator for RowIterator<'a, 'b, T> { #[inline] fn next(&mut self) -> Option { - let row = self.rotated_rows.next()?; + let row = self.rows.next()?; Some(Row::new(self.underlying, RowIndex::new(row))) } } @@ -132,12 +124,8 @@ impl<'a, 'b, 'c, T> ColumnIterator<'a, 'b, 'c, T> { #[inline] fn new(underlying: &'c Row<'a, 'b, T>) -> Self { let cols = 0..underlying.underlying.row_length().get(); - let rotated_cols = cols.rotate(*underlying.underlying.origin.col()); - let rotated_cols = rotated_cols.into_iter(); - Self { - underlying, - rotated_cols, - } + let cols = cols.into_iter(); + Self { underlying, cols } } } @@ -147,15 +135,18 @@ impl<'a, 'b, T> Iterator for ColumnIterator<'a, 'b, '_, T> { #[inline] fn next(&mut self) -> Option { - let col = self.rotated_cols.next()?; + let col = self.cols.next()?; let coord = Coord2D::new(self.underlying.row, ColIndex::new(col)); - Some(Element::new(&self.underlying.underlying.data, coord)) + Some(Element::new(self.underlying.underlying, coord)) } } impl<'a, 'b, T> Element<'a, 'b, T> { #[inline] - const fn new(underlying: &'b Array2DRef<'a, T>, coord: Coord2D) -> Self { + const fn new( + underlying: &'b OffsetArray2DRef<'a, T>, + coord: Coord2D, + ) -> Self { Self { underlying, coord } } } diff --git a/src/std/ndslice/offsetarray2dref/mod.rs b/src/std/ndslice/offsetarray2dref/mod.rs index 979ece0e..201fa9ea 100644 --- a/src/std/ndslice/offsetarray2dref/mod.rs +++ b/src/std/ndslice/offsetarray2dref/mod.rs @@ -37,24 +37,40 @@ impl<'a, T> OffsetArray2DRef<'a, T> { #[inline] #[must_use] fn get_row(&self, row: RowIndex) -> Option<&'a [T]> { + if row.val() >= self.num_rows().val().get() { + return None; + } let row = - BoundRowIndex::new(RowCount::new(self.data.num_rows().get()), row)?; + BoundRowIndex::new(RowCount::new(self.data.num_rows().get()), row); + #[expect(unsafe_code, clippy::undocumented_unsafe_blocks)] + let row = unsafe { row.unwrap_unchecked() }; let row = WrappingRowIndex::from(row) + self.origin.row(); - self.data.get_row(**row) + let row = self.data.get_row(**row); + #[expect(unsafe_code, clippy::undocumented_unsafe_blocks)] + let row = unsafe { row.unwrap_unchecked() }; + Some(row) } #[inline] #[must_use] - pub fn get_elt(&self, index: Coord2D) -> Option<&T> { - let row = self.get_row(index.row())?; + pub fn get_elt(&self, index: Coord2D) -> Option<&'a T> { + let row = self.get_row(RowIndex::new(*index.row()))?; + if *index.col() >= self.row_length().val().get() { + return None; + } let col = BoundColIndex::new( RowLength::new(self.data.row_length().get()), index.col(), - )?; + ); + #[expect(unsafe_code, clippy::undocumented_unsafe_blocks)] + let col = unsafe { col.unwrap_unchecked() }; let col = WrappingColIndex::from(col) + self.origin.col(); let col = ***col; - row.get(col) + let col = row.get(col); + #[expect(unsafe_code, clippy::undocumented_unsafe_blocks)] + let col = unsafe { col.unwrap_unchecked() }; + Some(col) } #[inline] diff --git a/src/std/wrapping_numerics_arith/mod.rs b/src/std/wrapping_numerics_arith/mod.rs index 65c77e7f..316792f7 100644 --- a/src/std/wrapping_numerics_arith/mod.rs +++ b/src/std/wrapping_numerics_arith/mod.rs @@ -56,7 +56,10 @@ where (lhs_lo, borrow) = lhs_lo.borrowing_sub(rhs_lo, borrow); (lhs_hi, borrow) = lhs_hi.borrowing_sub(rhs_hi, borrow); assert!(!borrow); - assert_eq!(lhs_hi, T::ZERO); + #[expect(unsafe_code, clippy::undocumented_unsafe_blocks)] + unsafe { + core::hint::assert_unchecked(lhs_hi == T::ZERO); + } lhs_lo } } @@ -134,13 +137,15 @@ where } if let Ok(ibound) = domain.try_into() { - return rhs + let res = rhs .checked_rem(ibound) .unwrap() .checked_add(ibound) .unwrap() - .try_into() - .unwrap(); + .try_into(); + #[expect(unsafe_code, clippy::undocumented_unsafe_blocks)] + let res = unsafe { res.unwrap_unchecked() }; + return res; } rhs.cast_unsigned().wrapping_add(domain) @@ -164,7 +169,10 @@ where let lhs = **self; let sum = SumAndCarry(lhs.carrying_add(rhs, false)); let rem = sum % **domain; - WrappingUnsigned::new(BoundUnsigned::new(*domain, rem).unwrap()) + let res = BoundUnsigned::new(*domain, rem); + #[expect(unsafe_code, clippy::undocumented_unsafe_blocks)] + let res = unsafe { res.unwrap_unchecked() }; + WrappingUnsigned::new(res) } }