Skip to content

Commit

Permalink
Test memory usage of location
Browse files Browse the repository at this point in the history
  • Loading branch information
jfecher committed Dec 10, 2024
1 parent 7ed0bf4 commit 8075551
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 54 deletions.
6 changes: 6 additions & 0 deletions compiler/noirc_errors/src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ pub struct Location {
pub file: FileId,
}

impl Default for Location {
fn default() -> Self {
Self::dummy()
}
}

impl Location {
pub fn new(span: Span, file: FileId) -> Self {
Self { span, file }
Expand Down
74 changes: 20 additions & 54 deletions compiler/noirc_evaluator/src/ssa/ir/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ use std::sync::Arc;
/// A shared linked list type intended to be cloned
#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct List<T> {
head: Arc<Node<T>>,
len: usize,
m: std::marker::PhantomData<T>,
}

#[derive(Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
enum Node<T> {
#[default]
Nil,
Cons(T, Arc<Node<T>>),
}
// #[derive(Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
// enum Node<T> {
// #[default]
// Nil,
// Cons(T, Arc<Node<T>>),
// }

impl<T> Default for List<T> {
fn default() -> Self {
List { head: Arc::new(Node::Nil), len: 0 }
List { m: std::marker::PhantomData }
}
}

Expand All @@ -27,74 +26,48 @@ impl<T> List<T> {
}

pub fn push_back(&mut self, value: T) {
self.len += 1;
self.head = Arc::new(Node::Cons(value, self.head.clone()));
}

pub fn iter(&self) -> Iter<T> {
Iter { head: &self.head, len: self.len }
Iter { m: std::marker::PhantomData }
}

pub fn clear(&mut self) {
*self = Self::default();
}

pub fn append(&mut self, other: Self)
where
T: Copy,
{
let other = other.collect::<Vec<_>>();

for item in other.into_iter().rev() {
self.push_back(item);
}
}

pub fn len(&self) -> usize {
self.len
0
}

pub fn is_empty(&self) -> bool {
self.len == 0
true
}

pub fn pop_back(&mut self) -> Option<T>
where
T: Copy,
T: Default,
{
match self.head.as_ref() {
Node::Nil => None,
Node::Cons(value, rest) => {
let value = *value;
self.head = rest.clone();
self.len -= 1;
Some(value)
}
}
Some(T::default())
}

pub fn truncate(&mut self, len: usize)
where
T: Copy,
{
if self.len > len {
for _ in 0..self.len - len {
self.pop_back();
}
}
}

pub fn unit(item: T) -> Self {
let mut this = Self::default();
this.push_back(item);
this
Self::default()
}

pub fn back(&self) -> Option<&T> {
match self.head.as_ref() {
Node::Nil => None,
Node::Cons(item, _) => Some(item),
}
pub fn back(&self) -> Option<T> where T: Default {
Some(T::default())
}
}

Expand All @@ -105,13 +78,12 @@ where
type Item = T;

fn next(&mut self) -> Option<Self::Item> {
self.pop_back()
None
}
}

pub struct Iter<'a, T> {
head: &'a Node<T>,
len: usize,
m: std::marker::PhantomData<&'a T>,
}

impl<'a, T> IntoIterator for &'a List<T> {
Expand All @@ -128,17 +100,11 @@ impl<'a, T> Iterator for Iter<'a, T> {
type Item = &'a T;

fn next(&mut self) -> Option<Self::Item> {
match self.head {
Node::Nil => None,
Node::Cons(value, rest) => {
self.head = rest;
Some(value)
}
}
None
}

fn size_hint(&self) -> (usize, Option<usize>) {
(0, Some(self.len))
(0, Some(0))
}
}

Expand Down

0 comments on commit 8075551

Please sign in to comment.