Skip to content

Optimize OverlapIterator #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## Unreleased

No unreleased changes yet
- Add `start()` and `end()` method to the `Region` trait.
- Much faster `OverlapIterator`.

## [0.3.1] - 2023-12-04

Expand Down
14 changes: 6 additions & 8 deletions embedded-storage-async/src/nor_flash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,15 @@ impl Page {
size,
}
}

/// The end address of the page
const fn end(&self) -> u32 {
self.start + self.size as u32
}
}

impl Region for Page {
/// Checks if an address offset is contained within the page
fn contains(&self, address: u32) -> bool {
(self.start <= address) && (self.end() > address)
fn start(&self) -> u32 {
self.start
}

fn end(&self) -> u32 {
self.start + self.size as u32
}
}

Expand Down
19 changes: 8 additions & 11 deletions src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,15 @@ where
type Item = (&'a [u8], R, u32);

fn next(&mut self) -> Option<Self::Item> {
let mem_start = self.base_address;
let mem_end = self.base_address + self.memory.len() as u32;
while let Some(region) = self.regions.next() {
// TODO: This might be possible to do in a smarter way?
let mut block_range = (0..self.memory.len())
.skip_while(|index| !region.contains(self.base_address + *index as u32))
.take_while(|index| region.contains(self.base_address + *index as u32));
if let Some(start) = block_range.next() {
let end = block_range.last().unwrap_or(start) + 1;
return Some((
&self.memory[start..end],
region,
self.base_address + start as u32,
));
if mem_start < region.end() && mem_end >= region.start() {
let addr_start = core::cmp::max(mem_start, region.start());
let addr_end = core::cmp::min(mem_end, region.end());
let start = (addr_start - self.base_address) as usize;
let end = (addr_end - self.base_address) as usize;
return Some((&self.memory[start..end], region, addr_start));
}
}
None
Expand Down
10 changes: 9 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@ pub mod nor_flash;

/// A region denotes a contiguous piece of memory between two addresses.
pub trait Region {
/// Start address of the region of `Self`
fn start(&self) -> u32;

/// End address of the region of `Self`
fn end(&self) -> u32;

/// Check if `address` is contained in the region of `Self`
fn contains(&self, address: u32) -> bool;
fn contains(&self, address: u32) -> bool {
(address >= self.start()) && (address < self.end())
}
}

/// Transparent read only storage trait
Expand Down
14 changes: 6 additions & 8 deletions src/nor_flash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,15 @@ impl Page {
size,
}
}

/// The end address of the page
const fn end(&self) -> u32 {
self.start + self.size as u32
}
}

impl Region for Page {
/// Checks if an address offset is contained within the page
fn contains(&self, address: u32) -> bool {
(self.start <= address) && (self.end() > address)
fn start(&self) -> u32 {
self.start
}

fn end(&self) -> u32 {
self.start + self.size as u32
}
}

Expand Down
Loading