Skip to content
← Back

src/syntax/process/compile-expressions/retry_stats.ghul

1
namespace Syntax.Process is
2
// Prototype measurement: how many body walks the compile-expressions
3
// retry loop needed per function body, as a histogram, printed at
4
// the end of the build when GHUL_RETRY_STATS is set.
5
class RETRY_STATS is
6
_histogram: Collections.MAP[int, int]? static
7
_walks: int static
8
9
enabled: bool static => System.Environment.get_environment_variable("GHUL_RETRY_STATS")?
10
11
// Reset the body's walk outputs before every retry walk rather
12
// than letting them accumulate; GHUL_NO_ITERATION_CLEAR opts out.
13
iteration_clear: bool static => !System.Environment.get_environment_variable("GHUL_NO_ITERATION_CLEAR")?
14
15
note(walks: int, function: Semantic.Symbols.Function?, location: Source.LOCATION) static is
16
if !enabled then
17
return
18
fi
19
20
if walks >= 20 then
21
let name = if function? then function.qualified_name else "<no symbol>" fi
22
23
IO.Std.error.write_line("retry stats: {name} at {location} did not converge")
24
fi
25
26
if !_histogram? then
27
_histogram = Collections.MAP[int, int]()
28
fi
29
30
let histogram = _histogram
31
32
let count mut = 0
33
histogram.try_get_value(walks, count ref)
34
histogram[walks] = count + 1
35
36
_walks = _walks + walks
37
si
38
39
report() static is
40
let histogram = _histogram
41
42
if !histogram? then
43
return
44
fi
45
46
let bodies mut = 0
47
48
for entry in histogram do
49
bodies = bodies + entry.value
50
od
51
52
IO.Std.error.write_line("retry stats: {bodies} bodies, {_walks} walks")
53
54
let keys = Collections.LIST[int](histogram.keys)
55
keys.sort()
56
57
for k in keys do
58
IO.Std.error.write_line(" {k} walk(s): {histogram[k]}")
59
od
60
si
61
si
62
si