aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAria <me@aria.rip>2023-11-30 13:08:00 +0000
committerAria <me@aria.rip>2023-11-30 13:08:00 +0000
commit14fda2d4901134e786290e8177a517e5291d66ab (patch)
treecb02139ced31e9fab4206c9988872123aa1c11b8
parent5b1497c52864b6230f1aa4162bcda345ff9b0323 (diff)
feat(library): WIP add profiling wrapper
-rw-r--r--src/crates/library/src/lib.rs3
-rw-r--r--src/crates/library/src/profiler.rs130
2 files changed, 133 insertions, 0 deletions
diff --git a/src/crates/library/src/lib.rs b/src/crates/library/src/lib.rs
index 19ff7af..1c39482 100644
--- a/src/crates/library/src/lib.rs
+++ b/src/crates/library/src/lib.rs
@@ -4,6 +4,9 @@
pub mod traits;
+mod profiler;
+pub use profiler::ProfilerWrapper;
+
mod eager_sorted_vector;
mod eager_unique_vector;
mod lazy_sorted_vector;
diff --git a/src/crates/library/src/profiler.rs b/src/crates/library/src/profiler.rs
new file mode 100644
index 0000000..214a376
--- /dev/null
+++ b/src/crates/library/src/profiler.rs
@@ -0,0 +1,130 @@
+use std::{backtrace::Backtrace, fs::File, io::Write, time::SystemTime};
+
+use crate::traits::{Container, Indexable, Stack};
+
+pub struct ProfilerWrapper<T> {
+ alloc_site: Backtrace,
+ inner: T,
+ sum_ns: usize,
+ n_contains: usize,
+ n_insert: usize,
+ n_clear: usize,
+ n_remove: usize,
+ n_first: usize,
+ n_last: usize,
+ n_nth: usize,
+ n_push: usize,
+ n_pop: usize,
+}
+
+impl<T: Default> Default for ProfilerWrapper<T> {
+ fn default() -> Self {
+ Self {
+ alloc_site: Backtrace::capture(),
+ inner: T::default(),
+ sum_ns: 0,
+ n_contains: 0,
+ n_insert: 0,
+ n_clear: 0,
+ n_remove: 0,
+ n_first: 0,
+ n_last: 0,
+ n_nth: 0,
+ n_push: 0,
+ n_pop: 0,
+ }
+ }
+}
+
+impl<T: Container<E>, E> Container<E> for ProfilerWrapper<T> {
+ fn len(&mut self) -> usize {
+ self.inner.len()
+ }
+
+ fn contains(&mut self, x: &E) -> bool {
+ self.sum_ns += self.inner.len();
+ self.n_contains += 1;
+ self.inner.contains(x)
+ }
+
+ fn is_empty(&mut self) -> bool {
+ self.inner.is_empty()
+ }
+
+ fn insert(&mut self, elt: E) {
+ self.sum_ns += self.inner.len();
+ self.n_insert += 1;
+ self.inner.insert(elt)
+ }
+
+ fn clear(&mut self) {
+ self.sum_ns += self.inner.len();
+ self.n_clear += 1;
+ self.inner.clear()
+ }
+
+ fn remove(&mut self, elt: E) -> Option<E> {
+ self.sum_ns += self.inner.len();
+ self.n_remove += 1;
+ self.inner.remove(elt)
+ }
+}
+
+impl<T: Indexable<E> + Container<E>, E> Indexable<E> for ProfilerWrapper<T> {
+ fn first(&mut self) -> Option<&E> {
+ self.sum_ns += self.inner.len();
+ self.n_first += 1;
+ self.inner.first()
+ }
+
+ fn last(&mut self) -> Option<&E> {
+ self.sum_ns += self.inner.len();
+ self.n_last += 1;
+ self.inner.last()
+ }
+
+ fn nth(&mut self, n: usize) -> Option<&E> {
+ self.sum_ns += self.inner.len();
+ self.n_nth += 1;
+ self.inner.nth(n)
+ }
+}
+
+impl<T: Stack<E> + Container<E>, E> Stack<E> for ProfilerWrapper<T> {
+ fn push(&mut self, elt: E) {
+ self.sum_ns += self.inner.len();
+ self.n_push += 1;
+ self.inner.push(elt)
+ }
+
+ fn pop(&mut self) -> Option<E> {
+ self.sum_ns += self.inner.len();
+ self.n_pop += 1;
+ self.inner.pop()
+ }
+}
+
+impl<T> Drop for ProfilerWrapper<T> {
+ fn drop(&mut self) {
+ let mut f = File::create(format!(
+ "/tmp/candelabra-profile-{}",
+ SystemTime::now()
+ .duration_since(SystemTime::UNIX_EPOCH)
+ .unwrap()
+ .as_millis()
+ ))
+ .unwrap();
+
+ writeln!(f, "{}", self.sum_ns).unwrap();
+ writeln!(f, "{}", self.n_contains).unwrap();
+ writeln!(f, "{}", self.n_insert).unwrap();
+ writeln!(f, "{}", self.n_clear).unwrap();
+ writeln!(f, "{}", self.n_remove).unwrap();
+ writeln!(f, "{}", self.n_first).unwrap();
+ writeln!(f, "{}", self.n_last).unwrap();
+ writeln!(f, "{}", self.n_nth).unwrap();
+ writeln!(f, "{}", self.n_push).unwrap();
+ writeln!(f, "{}", self.n_pop).unwrap();
+ writeln!(f, "{:?}", self.alloc_site).unwrap();
+ }
+}