Path: blob/main/crates/polars-expr/src/state/node_timer.rs
6940 views
use std::sync::Mutex;1use std::time::{Duration, Instant};23use polars_core::prelude::*;4use polars_core::utils::NoNull;56type StartInstant = Instant;7type EndInstant = Instant;89type Nodes = Vec<String>;10type Ticks = Vec<(Duration, Duration)>;1112#[derive(Clone)]13pub(super) struct NodeTimer {14query_start: Instant,15data: Arc<Mutex<(Nodes, Ticks)>>,16}1718impl NodeTimer {19pub(super) fn new(query_start: Instant) -> Self {20Self {21query_start,22data: Arc::new(Mutex::new((Vec::with_capacity(16), Vec::with_capacity(16)))),23}24}2526pub(super) fn store(&self, start: StartInstant, end: EndInstant, name: String) {27self.store_duration(28start.duration_since(self.query_start),29end.duration_since(self.query_start),30name,31)32}3334pub(super) fn store_duration(&self, start: Duration, end: Duration, name: String) {35let mut data = self.data.lock().unwrap();36let nodes = &mut data.0;37nodes.push(name);38let ticks = &mut data.1;39ticks.push((start, end))40}4142pub(super) fn finish(self) -> PolarsResult<DataFrame> {43let mut data = self.data.lock().unwrap();44let mut nodes = std::mem::take(&mut data.0);45nodes.push("optimization".to_string());4647let mut ticks = std::mem::take(&mut data.1);48// first value is end of optimization49polars_ensure!(!ticks.is_empty(), ComputeError: "no data to time");50let start = ticks[0].0;51ticks.push((Duration::from_nanos(0), start));52let nodes_s = Column::new(PlSmallStr::from_static("node"), nodes);53let start: NoNull<UInt64Chunked> = ticks54.iter()55.map(|(start, _)| start.as_micros() as u64)56.collect();57let mut start = start.into_inner();58start.rename(PlSmallStr::from_static("start"));5960let end: NoNull<UInt64Chunked> = ticks61.iter()62.map(|(_, end)| end.as_micros() as u64)63.collect();64let mut end = end.into_inner();65end.rename(PlSmallStr::from_static("end"));6667let height = nodes_s.len();68let columns = vec![nodes_s, start.into_column(), end.into_column()];69let df = unsafe { DataFrame::new_no_checks(height, columns) };70df.sort(vec!["start"], SortMultipleOptions::default())71}72}737475