Skip to content

Commit

Permalink
2023-03: Fix memory allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall committed Dec 3, 2023
1 parent 5c148ba commit 4678993
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 2 deletions.
1 change: 1 addition & 0 deletions crates/core/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ pub mod map_windows;
pub mod md5;
pub mod parser;
pub mod permutation;
pub mod triple_window_iterator;
pub mod tuple_window_iterator;
pub mod u256;
88 changes: 88 additions & 0 deletions crates/core/src/common/triple_window_iterator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
pub struct TripleWindowIterator<I, T>
where
I: Iterator<Item = T>,
T: Copy,
{
iterator: I,
first_item: Option<T>,
second_item: Option<T>,
}

impl<I, T> Iterator for TripleWindowIterator<I, T>
where
I: Iterator<Item = T>,
T: Copy,
{
type Item = (T, T, T);

fn next(&mut self) -> Option<Self::Item> {
loop {
match self.first_item {
Some(first_item) => match self.second_item {
Some(second_item) => {
let next_element = self.iterator.next()?;
self.first_item = Some(second_item);
self.second_item = Some(next_element);
return Some((first_item, second_item, next_element));
}
None => {
self.second_item = Some(self.iterator.next()?);
}
},
None => {
self.first_item = Some(self.iterator.next()?);
}
}
}
}
}

impl<I, T> ExactSizeIterator for TripleWindowIterator<I, T>
where
I: ExactSizeIterator<Item = T>,
T: Copy,
{
fn len(&self) -> usize {
let result = usize::from(self.first_item.is_some())
+ usize::from(self.second_item.is_some())
+ self.iterator.len();
if result > 0 {
result - 2
} else {
result
}
}
}

pub trait TripleWindowIteratorExt: Iterator {
fn triple_windows(self) -> TripleWindowIterator<Self, Self::Item>
where
Self::Item: Copy,
Self: Sized,
{
TripleWindowIterator {
iterator: self,
first_item: None,
second_item: None,
}
}
}

impl<I: Iterator> TripleWindowIteratorExt for I {}

#[test]
fn test() {
let a = [1, 2, 3, 4, 5, 6];
let mut it = a.into_iter().triple_windows();
assert_eq!(it.len(), 4);
assert_eq!(it.next(), Some((1, 2, 3)));
assert_eq!(it.len(), 3);
assert_eq!(it.next(), Some((2, 3, 4)));
assert_eq!(it.len(), 2);
assert_eq!(it.next(), Some((3, 4, 5)));
assert_eq!(it.len(), 1);
assert_eq!(it.next(), Some((4, 5, 6)));
assert_eq!(it.len(), 0);
assert_eq!(it.next(), None);
assert_eq!(it.len(), 0);
}
5 changes: 3 additions & 2 deletions crates/core/src/year2023/day03.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::common::map_windows::MapWindowsIterator;
use crate::common::triple_window_iterator::TripleWindowIteratorExt;
use crate::input::Input;

pub fn solve(input: &Input) -> Result<u64, String> {
Ok(input
.text
.lines()
.map(str::as_bytes)
.map_windows_stable(|[&above, &middle, &below]| {
.triple_windows()
.map(|(above, middle, below)| {
middle
.iter()
.enumerate()
Expand Down

1 comment on commit 4678993

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@                     Benchmark Difference                     @@
#      Name   Old (instructions)   New (instructions)   Change (%)
   2022_1_1              399,902              399,902            0
   2022_1_2              405,974              405,974            0
   2022_2_1              386,977              386,977            0
   2022_2_2              386,977              386,977            0
   2022_3_1              186,150              186,150            0
   2022_3_2              170,951              170,951            0
   2022_4_1              702,619              702,619            0
   2022_4_2              699,124              699,124            0
   2022_5_1              713,538              713,538            0
   2022_5_2              697,212              697,212            0
   2022_6_1               19,204               19,204            0
   2022_6_2               53,476               53,476            0
   2022_7_1              300,380              300,380            0
   2022_7_2              301,017              301,017            0
   2022_8_1            1,736,626            1,736,626            0
   2022_8_2            2,319,799            2,319,799            0
   2022_9_1            3,454,050            3,454,050            0
   2022_9_2            4,255,767            4,255,767            0
  2022_10_1               50,275               50,275            0
  2022_10_2               50,818               50,818            0
  2022_11_1               95,051               95,051            0
  2022_11_2            1,417,142            1,416,429            0
  2022_12_1            1,282,278            1,282,278            0
  2022_12_2            1,230,654            1,230,654            0
  2022_13_1              275,374              275,374            0
  2022_13_2              214,057              214,057            0
  2022_14_1            3,232,915            3,232,915            0
  2022_14_2            3,891,311            3,891,311            0
  2022_15_1              136,770              136,770            0
  2022_15_2              150,550              150,550            0
  2022_16_1              694,807              694,852            0
  2022_16_2            3,271,042            3,270,591            0
  2022_17_1            1,240,592            1,240,592            0
  2022_17_2            4,148,828            4,145,694            0
  2022_18_1            1,466,924            1,466,924            0
  2022_18_2            3,251,813            3,251,813            0
  2022_19_1            1,809,279            1,809,279            0
  2022_19_2            3,222,855            3,222,855            0
  2022_20_1            4,057,226            4,057,226            0
  2022_20_2           33,765,454           33,765,454            0
  2022_21_1            3,026,302            3,024,495            0
  2022_21_2            3,078,592            3,079,272            0
  2022_22_1            1,267,419            1,267,419            0
  2022_22_2            1,265,235            1,265,235            0
  2022_23_1            2,005,621            2,005,621            0
  2022_23_2           94,549,038           94,549,038            0
  2022_24_1            1,130,918            1,130,918            0
  2022_24_2            3,209,874            3,209,874            0
  2022_25_1               36,557               36,557            0
Benchmark Instructions (count) Instructions (%)
2022_23_2 94,549,038 48.3
2022_20_2 33,765,454 17.3
2022_9_2 4,255,767 2.2
2022_17_2 4,145,694 2.1
2022_20_1 4,057,226 2.1
2022_14_2 3,891,311 2.0
2022_9_1 3,454,050 1.8
2022_16_2 3,270,591 1.7
2022_18_2 3,251,813 1.7
2022_14_1 3,232,915 1.7
2022_19_2 3,222,855 1.6
2022_24_2 3,209,874 1.6
2022_21_2 3,079,272 1.6
2022_21_1 3,024,495 1.5
2022_8_2 2,319,799 1.2
2022_23_1 2,005,621 1.0
2022_19_1 1,809,279 0.9
2022_8_1 1,736,626 0.9
2022_18_1 1,466,924 0.7
2022_11_2 1,416,429 0.7
2022_12_1 1,282,278 0.7
2022_22_1 1,267,419 0.6
2022_22_2 1,265,235 0.6
2022_17_1 1,240,592 0.6
2022_12_2 1,230,654 0.6
2022_24_1 1,130,918 0.6
2022_5_1 713,538 0.4
2022_4_1 702,619 0.4
2022_4_2 699,124 0.4
2022_5_2 697,212 0.4
2022_16_1 694,852 0.4
2022_1_2 405,974 0.2
2022_1_1 399,902 0.2
2022_2_2 386,977 0.2
2022_2_1 386,977 0.2
2022_7_2 301,017 0.2
2022_7_1 300,380 0.2
2022_13_1 275,374 0.1
2022_13_2 214,057 0.1
2022_3_1 186,150 0.1
2022_3_2 170,951 0.1
2022_15_2 150,550 0.1
2022_15_1 136,770 0.1
2022_11_1 95,051 0.0
2022_6_2 53,476 0.0
2022_10_2 50,818 0.0
2022_10_1 50,275 0.0
2022_25_1 36,557 0.0
2022_6_1 19,204 0.0

Please sign in to comment.