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
|
# Dissertation Visualisations
```elixir
Mix.install([
{:tucan, "~> 0.3.0"},
{:kino_vega_lite, "~> 0.1.8"},
{:json, "~> 1.4"},
{:explorer, "~> 0.8.0"},
{:kino_explorer, "~> 0.1.11"},
{:math, "~> 0.7.0"}
])
```
## Variables
```elixir
require Explorer.DataFrame
require Explorer.Series
alias Explorer.DataFrame, as: DF
alias Explorer.Series, as: SE
job_id = "current"
job_dir = Path.expand(~c"./" ++ job_id) |> Path.absname()
sections_dir = Path.join(job_dir, "sections")
cm_dir = Path.join([job_dir, "candelabra", "benchmark_results"])
criterion_dir = Path.join(job_dir, "criterion")
```
## Read cost model data
```elixir
{:ok, cost_model_files} = File.ls(cm_dir)
cost_model_files =
cost_model_files
|> Enum.map(fn fname -> Path.join(cm_dir, fname) |> Path.absname() end)
cost_model_files
```
<!-- livebook:{"reevaluate_automatically":true} -->
```elixir
# Parse cost model information
cost_models =
cost_model_files
|> Enum.map(fn fname ->
impl = Path.basename(fname) |> String.replace("_", ":")
contents = File.read!(fname)
contents = JSON.decode!(contents)
contents["model"]["by_op"]
|> Enum.map(fn {op, %{"coeffs" => coeffs}} ->
%{
op: op,
impl: impl,
coeffs: coeffs
}
end)
|> DF.new()
end)
|> DF.concat_rows()
```
```elixir
# Parse cost model information
cost_model_points =
cost_model_files
|> Enum.map(fn fname ->
impl = Path.basename(fname) |> String.replace("_", ":")
contents = File.read!(fname)
contents = JSON.decode!(contents)
contents["results"]["by_op"]
|> Enum.flat_map(fn {op, results} ->
Enum.map(results, fn [n, cost] ->
%{
op: op,
impl: String.split(impl, "::") |> List.last(),
n: n,
t: cost
}
end)
end)
|> DF.new()
end)
|> DF.concat_rows()
```
```elixir
cost_models
|> DF.filter(op == "contains")
```
## Cost model exploratory plots
```elixir
startn = 200
endn = 2000
resolution = 50
points_for = fn impl, op ->
%{"coeffs" => [coeffs]} =
DF.filter(cost_models, impl == ^impl and op == ^op)
|> DF.to_columns()
Enum.map(startn..endn//resolution, fn n ->
t =
(coeffs
|> Enum.take(3)
|> Enum.with_index()
|> Enum.map(fn {coeff, idx} -> coeff * n ** idx end)
|> Enum.sum()) + Enum.at(coeffs, 3) * Math.log2(n)
%{
impl: String.split(impl, "::") |> List.last(),
op: op,
n: n,
t: t
}
end)
|> DF.new()
end
```
<!-- livebook:{"reevaluate_automatically":true} -->
```elixir
inspect_op = "insert"
impls = ["BTreeSet", "EagerSortedVec", "EagerUniqueVec", "HashSet"]
Tucan.layers([
cost_models
|> DF.filter(op == ^inspect_op)
|> DF.distinct(["impl"])
|> DF.to_rows()
|> Enum.map(fn %{"impl" => impl} -> points_for.(impl, inspect_op) end)
|> DF.concat_rows()
|> DF.filter(impl in ^impls)
|> Tucan.lineplot("n", "t", color_by: "impl", clip: true),
# |> Tucan.Scale.set_y_domain(0, 200)
Tucan.scatter(
cost_model_points
|> DF.filter(op == ^inspect_op and impl in ^impls)
|> DF.group_by(["impl", "n"]),
# |> DF.summarise(t: mean(t)),
"n",
"t",
color_by: "impl",
clip: true
)
])
|> Tucan.Scale.set_x_domain(startn, endn)
|> Tucan.Scale.set_y_domain(0, 200)
|> Tucan.set_size(500, 500)
```
## Read benchmark data
```elixir
raw_benchmarks =
File.ls!(criterion_dir)
|> Enum.map(fn name ->
File.ls!(Path.join(criterion_dir, name))
|> Enum.map(fn p -> %{bench: name, subbench: p} end)
end)
|> List.flatten()
|> Enum.map(fn %{bench: bench, subbench: subbench} ->
File.ls!(Path.join([criterion_dir, bench, subbench]))
|> Enum.filter(fn x -> String.contains?(x, "Mapping2D") end)
|> Enum.map(fn x -> Path.join([criterion_dir, bench, subbench, x]) end)
|> Enum.map(fn dir ->
raw_results =
Path.join(dir, "estimates.json")
|> File.read!()
|> JSON.decode!()
%{
bench_id: bench <> "/" <> subbench,
proj: String.split(bench, "-") |> hd,
using:
Regex.scan(~r/\"(\w*)\", ([\w:]*)/, Path.basename(dir))
|> Enum.map(fn [_, ctn, impl] -> %{ctn: ctn, impl: impl} end),
mean: raw_results["mean"]["point_estimate"] / 10 ** 9
}
end)
end)
|> List.flatten()
|> DF.new()
```
```elixir
# Aggregate benchmark results by project, since we can only do assignments by project
uniq_proj_using =
DF.select(raw_benchmarks, ["proj", "using"])
|> DF.to_rows()
|> Enum.uniq()
|> DF.new()
uniq_proj_using
|> DF.mutate(vals: DF.filter(^raw_benchmarks, proj == proj and using == using))
# |> Enum.map(fn %{"proj" => proj, "using" => using} ->
# DF.filter(raw_benchmarks, proj == ^proj and using == ^using)
# # |> DF.summarise()
# end)
# |> DF.concat_rows()
```
```elixir
# Cost estimates by project, ctn, and implementation
projs = SE.distinct(benchmarks["proj"])
cost_estimates =
SE.transform(projs, fn proj_name ->
[_, table | _] =
Path.join(sections_dir, "compare-" <> proj_name)
|> File.read!()
|> String.split("& file \\\\\n\\hline\n")
table
|> String.split("\n\\end{tabular}")
|> hd
|> String.split("\n")
|> Enum.map(fn x -> String.split(x, " & ") end)
|> Enum.map(fn [ctn, impl, cost | _] ->
%{
proj: proj_name,
ctn: ctn,
impl:
impl
|> String.replace("\\_", "_"),
cost:
if String.contains?(cost, ".") do
String.to_float(cost)
else
String.to_integer(cost)
end
}
end)
end)
|> SE.to_list()
|> List.flatten()
|> DF.new()
```
```elixir
# Get cost of assignment from cost estimates
cost_of_assignment = fn proj, assignment ->
assignment
|> Enum.map(fn %{"ctn" => ctn, "impl" => impl} ->
DF.filter(cost_estimates, proj == ^proj and ctn == ^ctn and impl == ^impl)["cost"][0]
end)
|> Enum.sum()
end
cost_of_assignment.("example_stack", [%{"ctn" => "StackCon", "impl" => "std::vec::Vec"}])
```
```elixir
# Estimate cost for each benchmarked assignment
estimated_costs =
benchmarks
|> DF.to_rows_stream()
|> Enum.map(fn %{"bench_id" => bench_id, "proj" => proj, "using" => using} ->
%{
bench_id: bench_id,
using: using,
estimated_cost: cost_of_assignment.(proj, using)
}
end)
|> DF.new()
```
```elixir
# Compare each assignments position in the estimates to its position in the results
sorted_estimates =
estimated_costs
|> DF.group_by(["bench_id"])
|> DF.sort_by(estimated_cost)
sorted_results =
benchmarks
|> DF.group_by(["bench_id"])
|> DF.sort_by(mean)
position_comparison =
sorted_estimates
|> DF.to_rows_stream()
|> Enum.map(fn %{"bench_id" => bench_id, "using" => using} ->
%{
bench_id: bench_id,
using: using,
pos_estimate:
DF.filter(sorted_estimates, bench_id == ^bench_id)["using"]
|> SE.to_list()
|> Enum.find_index(fn u -> u == using end),
pos_results:
DF.filter(sorted_results, bench_id == ^bench_id)["using"]
|> SE.to_list()
|> Enum.find_index(fn u -> u == using end)
}
end)
|> DF.new()
```
```elixir
position_comparison
|> DF.filter(pos_estimate != pos_results)
|> DF.collect()
```
```elixir
position_comparison
|> DF.filter(pos_estimate == 0)
|> DF.select(["bench_id", "using"])
```
<!-- livebook:{"reevaluate_automatically":true} -->
```elixir
# Difference in execution time between worst and best selection
Tucan.bar(
benchmarks
|> DF.group_by("bench_id")
|> DF.summarise(range: max(mean) - min(mean)),
"bench_id",
"range",
orient: :horizontal,
clip: true
)
|> Tucan.Scale.set_x_domain(0, 5)
```
|