Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-expr/src/state/node_timer.rs
6940 views
1
use std::sync::Mutex;
2
use std::time::{Duration, Instant};
3
4
use polars_core::prelude::*;
5
use polars_core::utils::NoNull;
6
7
type StartInstant = Instant;
8
type EndInstant = Instant;
9
10
type Nodes = Vec<String>;
11
type Ticks = Vec<(Duration, Duration)>;
12
13
#[derive(Clone)]
14
pub(super) struct NodeTimer {
15
query_start: Instant,
16
data: Arc<Mutex<(Nodes, Ticks)>>,
17
}
18
19
impl NodeTimer {
20
pub(super) fn new(query_start: Instant) -> Self {
21
Self {
22
query_start,
23
data: Arc::new(Mutex::new((Vec::with_capacity(16), Vec::with_capacity(16)))),
24
}
25
}
26
27
pub(super) fn store(&self, start: StartInstant, end: EndInstant, name: String) {
28
self.store_duration(
29
start.duration_since(self.query_start),
30
end.duration_since(self.query_start),
31
name,
32
)
33
}
34
35
pub(super) fn store_duration(&self, start: Duration, end: Duration, name: String) {
36
let mut data = self.data.lock().unwrap();
37
let nodes = &mut data.0;
38
nodes.push(name);
39
let ticks = &mut data.1;
40
ticks.push((start, end))
41
}
42
43
pub(super) fn finish(self) -> PolarsResult<DataFrame> {
44
let mut data = self.data.lock().unwrap();
45
let mut nodes = std::mem::take(&mut data.0);
46
nodes.push("optimization".to_string());
47
48
let mut ticks = std::mem::take(&mut data.1);
49
// first value is end of optimization
50
polars_ensure!(!ticks.is_empty(), ComputeError: "no data to time");
51
let start = ticks[0].0;
52
ticks.push((Duration::from_nanos(0), start));
53
let nodes_s = Column::new(PlSmallStr::from_static("node"), nodes);
54
let start: NoNull<UInt64Chunked> = ticks
55
.iter()
56
.map(|(start, _)| start.as_micros() as u64)
57
.collect();
58
let mut start = start.into_inner();
59
start.rename(PlSmallStr::from_static("start"));
60
61
let end: NoNull<UInt64Chunked> = ticks
62
.iter()
63
.map(|(_, end)| end.as_micros() as u64)
64
.collect();
65
let mut end = end.into_inner();
66
end.rename(PlSmallStr::from_static("end"));
67
68
let height = nodes_s.len();
69
let columns = vec![nodes_s, start.into_column(), end.into_column()];
70
let df = unsafe { DataFrame::new_no_checks(height, columns) };
71
df.sort(vec!["start"], SortMultipleOptions::default())
72
}
73
}
74
75