aboutsummaryrefslogtreecommitdiff
path: root/src/crates/candelabra/src/profiler/info.rs
blob: 488a8ba2c0c09e417d0a0b8aefcab937e7d82c52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
use std::collections::HashMap;
use std::str::FromStr;

use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};

use crate::{
    cost::{benchmark::OpName, Cost, CostModel, Estimator},
    types::ImplName,
};

/// The information we get from profiling.
/// Rather than keeping all results, we split them into 'similar enough' partitions,
/// with the idea that each partition will probably have the same best implementation.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct UsageProfile(pub Vec<ProfilerPartition>);

/// A vector of container lifetimes which have similar usage characteristics
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct ProfilerPartition {
    pub occurences: f64,
    pub avg_n: f64,
    pub avg_op_counts: HashMap<OpName, f64>,
}

/// Lifetime of a single allocated collection.
type CollectionLifetime = (f64, HashMap<OpName, usize>);

/// Breakdown of a cost value by operation
pub type CostBreakdown<'a> = HashMap<&'a OpName, Cost>;

/// A single result of container selection
#[derive(Clone, Debug)]
pub struct ContainerSplitSpec {
    pub before: ImplName,
    pub threshold: usize,
    pub after: ImplName,
}

impl UsageProfile {
    pub fn from(iter: impl Iterator<Item = Result<String>>) -> Result<Self> {
        Ok(Self(
            iter.map(|contents| parse_output(&contents?))
                .fold(Ok(vec![]), partition_costs)?,
        ))
    }

    pub fn check_for_nsplit(
        &mut self,
        candidates: &HashMap<&String, CostModel>,
    ) -> Option<(ContainerSplitSpec, Cost)> {
        self.0.sort_by_key(|p| p.avg_n as usize);
        if self.0.is_empty() {
            return None;
        }

        let costs_by_partitions = candidates
            .iter()
            .map(|(name, model)| {
                (
                    name,
                    self.0
                        .iter()
                        .map(|p| p.estimate_cost(&model))
                        .collect::<Vec<_>>(),
                )
            })
            .collect::<Vec<(_, _)>>();

        let top_by_partition = (0..self.0.len())
            .map(|i| {
                costs_by_partitions.iter().fold(
                    ("".to_string(), f64::MAX),
                    |acc @ (_, val), (name, c)| {
                        if val < c[i] {
                            acc
                        } else {
                            (name.to_string(), c[i])
                        }
                    },
                )
            })
            .collect::<Vec<_>>();

        let split_idx = top_by_partition
            .iter()
            .enumerate()
            // TODO: fudge?
            .find(|(idx, (best, _))| *idx > 0 && *best != top_by_partition[idx - 1].0)
            .map(|(idx, _)| idx)?;

        let split_is_proper = top_by_partition.iter().enumerate().all(|(i, (best, _))| {
            if i >= split_idx {
                *best == top_by_partition[split_idx].0
            } else {
                *best == top_by_partition[0].0
            }
        });

        if !split_is_proper {
            return None;
        }

        // calculate cost of switching
        let before = &top_by_partition[0].0;
        let after = &top_by_partition[split_idx].0;
        let before_model = candidates.get(before).unwrap();
        let after_model = candidates.get(after).unwrap();

        let copy_n = self.0[split_idx].avg_n;
        let switching_cost = after_model.by_op.get("insert")?.estimatef(copy_n)
            + before_model.by_op.get("clear")?.estimatef(copy_n);

        // see if it's "worth it"
        let before_costs = &costs_by_partitions
            .iter()
            .find(|(name, _)| **name == before)
            .unwrap()
            .1;
        let after_costs = &costs_by_partitions
            .iter()
            .find(|(name, _)| **name == after)
            .unwrap()
            .1;
        let not_switching_cost = &before_costs[split_idx..].iter().sum::<f64>()
            - &after_costs[split_idx..].iter().sum::<f64>();

        if not_switching_cost < switching_cost {
            None
        } else {
            Some((
                ContainerSplitSpec {
                    before: before.to_string(),
                    threshold: copy_n as usize,
                    after: after.to_string(),
                },
                top_by_partition.iter().map(|(_, v)| v).sum(),
            ))
        }
    }

    /// Estimate the cost of using the implementation with the given cost model
    pub fn estimate_cost(&self, cost_model: &CostModel) -> f64 {
        self.0
            .iter()
            .map(|cl| cl.estimate_cost(cost_model))
            .sum::<f64>()
    }

    /// Get a breakdown of the cost by operation
    pub fn cost_breakdown<'a>(&self, cost_model: &'a CostModel) -> CostBreakdown<'a> {
        cost_model
            .by_op
            .iter()
            .map(|(op, estimator)| {
                (
                    op,
                    self.0
                        .iter()
                        .map(|cl| cl.op_cost(op, estimator))
                        .sum::<f64>(),
                )
            })
            .collect()
    }
}

impl ProfilerPartition {
    pub fn avg_op_count(&self, op: &str) -> f64 {
        *self
            .avg_op_counts
            .get(op)
            .expect("invalid op passed to op_count")
    }

    pub fn estimate_cost(&self, cost_model: &CostModel) -> f64 {
        cost_model
            .by_op
            .iter()
            .map(|(op, estimator)| self.op_cost(op, estimator))
            .sum::<f64>()
    }

    pub fn op_cost(&self, op: &str, estimator: &Estimator) -> f64 {
        estimator.estimatef(self.avg_n) * self.avg_op_count(op) * self.occurences
    }

    /// Get a breakdown of the cost by operation
    pub fn cost_breakdown<'a>(&'a self, cost_model: &'a CostModel) -> CostBreakdown<'a> {
        self.avg_op_counts
            .keys()
            .flat_map(|op| Some((op, self.op_cost(op, cost_model.by_op.get(op)?))))
            .collect()
    }
    fn add_lifetime(&mut self, (n, ops): (f64, HashMap<String, usize>)) {
        self.avg_n = self.avg_n + (n - self.avg_n) / (self.occurences + 1.0);
        for (op, count) in ops {
            let count = count as f64;
            self.avg_op_counts
                .entry(op)
                .and_modify(|avg| *avg = *avg + (count - *avg) / (self.occurences + 1.0))
                .or_insert(count);
        }
        self.occurences += 1.0;
    }
}

/// Attempt to compress an iterator of collection lifetimes into as few partitions as possible
fn partition_costs(
    acc: Result<Vec<ProfilerPartition>>,
    cl: Result<CollectionLifetime>,
) -> Result<Vec<ProfilerPartition>> {
    // error short circuiting
    let (mut acc, (n, ops)) = (acc?, cl?);

    // attempt to find a partition with a close enough n value
    let (closest_idx, closest_delta) =
        acc.iter()
            .enumerate()
            .fold((0, f64::MAX), |acc @ (_, val), (idx, partition)| {
                let delta = (partition.avg_n - n).abs();
                if delta < val {
                    (idx, delta)
                } else {
                    acc
                }
            });

    if closest_delta < 100.0 {
        acc[closest_idx].add_lifetime((n, ops));
    } else {
        // add a new partition
        acc.push(ProfilerPartition {
            occurences: 1.0,
            avg_n: n,
            avg_op_counts: ops.into_iter().map(|(k, v)| (k, v as f64)).collect(),
        })
    }

    Ok(acc)
}

/// Parse the output of the profiler
fn parse_output(contents: &str) -> Result<(f64, HashMap<OpName, usize>)> {
    let mut lines = contents.lines().map(usize::from_str);
    let missing_line_err = || anyhow!("wrong number of lines in ");
    let n = lines.next().ok_or_else(missing_line_err)??;
    let mut op_counts = HashMap::new();
    op_counts.insert(
        "contains".to_string(),
        lines.next().ok_or_else(missing_line_err)??,
    );
    op_counts.insert(
        "insert".to_string(),
        lines.next().ok_or_else(missing_line_err)??,
    );
    op_counts.insert(
        "clear".to_string(),
        lines.next().ok_or_else(missing_line_err)??,
    );
    op_counts.insert(
        "remove".to_string(),
        lines.next().ok_or_else(missing_line_err)??,
    );
    op_counts.insert(
        "first".to_string(),
        lines.next().ok_or_else(missing_line_err)??,
    );
    op_counts.insert(
        "last".to_string(),
        lines.next().ok_or_else(missing_line_err)??,
    );
    op_counts.insert(
        "nth".to_string(),
        lines.next().ok_or_else(missing_line_err)??,
    );
    op_counts.insert(
        "push".to_string(),
        lines.next().ok_or_else(missing_line_err)??,
    );
    op_counts.insert(
        "pop".to_string(),
        lines.next().ok_or_else(missing_line_err)??,
    );
    op_counts.insert(
        "get".to_string(),
        lines.next().ok_or_else(missing_line_err)??,
    );

    Ok((n as f64, op_counts))
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use crate::{
        cost::{CostModel, Estimator},
        profiler::info::partition_costs,
    };

    use super::{ProfilerPartition, UsageProfile};

    const EPSILON: f64 = 1e-5;
    fn assert_feq(left: f64, right: f64, msg: &'static str) {
        assert!((left - right).abs() < EPSILON, "{}", msg);
    }

    fn linear_estimator() -> Estimator {
        Estimator {
            coeffs: [0.0, 1.0, 0.0, 0.0],
            transform_x: (0.0, 1.0),
            transform_y: (0.0, 1.0),
        }
    }

    #[test]
    fn test_cost_single_partition() {
        let info = UsageProfile(vec![ProfilerPartition {
            occurences: 1.0,
            avg_n: 100.0,
            avg_op_counts: {
                let mut map = HashMap::new();
                map.insert("insert".to_string(), 100.0);
                map
            },
        }]);

        let model = CostModel {
            by_op: {
                let mut map = HashMap::new();
                map.insert("insert".to_string(), linear_estimator());
                map
            },
        };

        let cost = dbg!(info.estimate_cost(&model));
        assert_feq(cost, 10_000.0, "per op = 100 * 100 ops");
    }

    #[test]
    fn test_cost_multi_partitions_sums() {
        let info = UsageProfile(vec![
            ProfilerPartition {
                occurences: 1.0,
                avg_n: 100.0,
                avg_op_counts: {
                    let mut map = HashMap::new();
                    map.insert("insert".to_string(), 100.0);
                    map
                },
            },
            ProfilerPartition {
                occurences: 1.0,
                avg_n: 10.0,
                avg_op_counts: {
                    let mut map = HashMap::new();
                    map.insert("insert".to_string(), 10.0);
                    map
                },
            },
        ]);

        let model = CostModel {
            by_op: {
                let mut map = HashMap::new();
                map.insert("insert".to_string(), linear_estimator());
                map
            },
        };

        let cost = dbg!(info.estimate_cost(&model));
        assert_feq(cost, 10_100.0, "100ns/op * 100 ops + 10ns/op * 10 ops");
    }

    #[test]
    fn test_cost_multi_partitions_sums_weighted() {
        let info = UsageProfile(vec![
            ProfilerPartition {
                occurences: 2.0,
                avg_n: 100.0,
                avg_op_counts: {
                    let mut map = HashMap::new();
                    map.insert("insert".to_string(), 100.0);
                    map
                },
            },
            ProfilerPartition {
                occurences: 1.0,
                avg_n: 10.0,
                avg_op_counts: {
                    let mut map = HashMap::new();
                    map.insert("insert".to_string(), 10.0);
                    map
                },
            },
        ]);

        let model = CostModel {
            by_op: {
                let mut map = HashMap::new();
                map.insert("insert".to_string(), linear_estimator());
                map
            },
        };

        let cost = dbg!(info.estimate_cost(&model));
        assert_feq(cost, 20_100.0, "100ns/op * 100 ops * 2 + 10ns/op * 10 ops");
    }

    #[test]
    fn test_partition_costs_merges_duplicates() {
        let cl = (100.0, {
            let mut map = HashMap::new();
            map.insert("insert".to_string(), 10);
            map
        });
        let outp = vec![Ok(cl.clone()), Ok(cl)]
            .into_iter()
            .fold(Ok(vec![]), partition_costs)
            .unwrap();

        assert_eq!(outp.len(), 1, "merged duplicates");
        assert_eq!(outp[0].occurences, 2.0, "weight updated");
        assert_feq(outp[0].avg_n, 100.0, "average n correct");
        assert_feq(
            *outp[0].avg_op_counts.get("insert").unwrap(),
            10.0,
            "average n correct",
        );
    }

    #[test]
    fn test_partition_costs_merges_close() {
        let outp = vec![
            Ok((100.0, {
                let mut map = HashMap::new();
                map.insert("insert".to_string(), 50);
                map
            })),
            Ok((110.0, {
                let mut map = HashMap::new();
                map.insert("insert".to_string(), 100);
                map
            })),
        ]
        .into_iter()
        .fold(Ok(vec![]), partition_costs)
        .unwrap();

        assert_eq!(outp.len(), 1, "merged duplicates");
        assert_eq!(outp[0].occurences, 2.0, "weight updated");
        assert_feq(outp[0].avg_n, 105.0, "average n correct");
        assert_feq(
            *outp[0].avg_op_counts.get("insert").unwrap(),
            75.0,
            "average n correct",
        );
    }
    #[test]
    fn test_partition_costs_keeps_separate() {
        let outp = vec![
            Ok((100.0, {
                let mut map = HashMap::new();
                map.insert("insert".to_string(), 10);
                map
            })),
            Ok((999999.0, {
                let mut map = HashMap::new();
                map.insert("insert".to_string(), 10);
                map
            })),
        ]
        .into_iter()
        .fold(Ok(vec![]), partition_costs)
        .unwrap();

        assert_eq!(
            outp.len(),
            2,
            "large difference in n values causes partition"
        );
    }
}