File: spesh_diff.p6

package info (click to toggle)
moarvm 2020.12%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 18,652 kB
  • sloc: ansic: 268,178; perl: 8,186; python: 1,316; makefile: 768; sh: 287
file content (218 lines) | stat: -rwxr-xr-x 6,646 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env perl6

use v6;

multi sub MAIN() {
    say q:to/USAGE/;
        This tool can be used as followed:

            spesh_diff.p6 output_from_mvm.txt

            spesh_diff.p6 --matcher="'foobar'" output.txt

            spesh_diff.p6 --matcher="/^ to $/" output.txt

            spesh_diff.p6 --matcher="diff => / '+' .*? 'nyi' /" output.txt


        This tool takes the output MoarVM generates to a file when you call it
        with MVM_SPESH_LOG environment variable set to a filename and splits it
        into two folders, optionally selects a subset of the participating cuids
        and then diffs everything for you using git diff.

        It will output colors always, so if you want to use less to paginate, you
        will need to supply -r to less.

        You can supply any valid Perl 6 code for the matcher flag. If you don't
        supply a Pair, a Pair from name to your matcher will be generated for
        you. You can filter based on these properties:

            before      all text in the "before" section
            after       all text in the "after" section
            name        the name of the method/sub/...
            cuid        the cuid
            diff        the output of the diff command
                        (watch out, these contain ansi color codes)
        USAGE
}

class Spesh is rw {
    has @.beforelines;
    has @.afterlines;
    has $.before;
    has $.after;
    has Str $.name;
    has Str $.cuid;
    has Str $.diff;
}

enum Target <Before Logged Facts Specialized>;

sub supersmartmatch($thing) {
    given $thing {
        when Pair {
            say "making a supersmartmatcher for $thing.key() -> $thing.value().perl()";
            return -> $to_match { $to_match."$thing.key()"() ~~ supersmartmatch($thing.value) }
        }
        when Str {
            return -> $to_match { $to_match ~~ / $($thing) / }
        }
        when Junction {
            return -> $to_match { $to_match ~~ supersmartmatch($thing) }
        }
        default {
            return $thing
        }
    }
}

multi sub MAIN($filename?, :$matcher?) {
    my %speshes;
    my Target $target;

    my $linecount;
    my Int $lines_total;

    if $filename {
        $lines_total = qqx/ wc -l '$filename' /.words[0].Int;
        $*ARGFILES = open($filename, :r);
    } else {
        $*ARGFILES = $*IN;
    }

    my $ssm = do if $matcher {
        my Mu $matcher_evald = EVAL $matcher;
        if $matcher_evald.WHAT ~~ Str | Regex | Junction {
            supersmartmatch (name => $matcher_evald);
        } else {
            supersmartmatch $matcher_evald;
        }
    } else {
        True
    }

    sub notegraph($kind) {
        state $printed = 0;
        state $few = 0;
        my $did_print = False;
        if $kind eq "." {
            if ++$few %% 50 {
                $*ERR.print(".");
                $did_print = True;
            }
        } else {
            $*ERR.print($kind);
            $did_print = True;
        }
        if $did_print and ++$printed %% 80 {
            if $lines_total {
                my $part = $linecount / $lines_total;
                $*ERR.print(($part * 100).fmt(" % 3.2f%%"));
                my $est_time_left = (1 - $part) * (now - INIT now) / $part;
                $*ERR.print(($est_time_left / 60).fmt(" %d") ~ ($est_time_left % 60).fmt(":%02ds"));
            }
            $*ERR.print("\n");
        }
    }

    try mkdir "spesh_diffs_before";
    try mkdir "spesh_diffs_after";

    my Spesh $current;

    for lines(:eager) {
        my $line = $_;
        $linecount++;
        when /^'  ' / {
            given $target {
                when Before {
                    $current.beforelines.push: $line;
                    notegraph(".");
                }
                when Specialized {
                    $current.afterlines.push: $line;
                    notegraph(".");
                }
            }
        }
        when /^
              [$<kind>=I 'nserting logging for specialization of ' | $<kind>=F 'inished specialization of '] \'
                $<name>=[<-[\']>*] \'
                ' (cuid: ' $<cuid>=[<-[\)]>+] ')'
            / {
            my $cuid = $<cuid>;
            if $<kind> eq 'I' {
                # want to build a new one
                while %speshes{$cuid}:exists {
                    $cuid ~= "_";
                }

                $current .= new(name => $<name>.Str, cuid => $<cuid>.Str, diff => "");
                %speshes{$current.cuid} = $current;
            } elsif $<kind> eq 'F' {
                # want to find the last one added that exists.
                while %speshes{$cuid}:exists {
                    $cuid ~= '_';
                }
                # so we chop off a _ again
                $cuid = $cuid.substr(0, *-1);

                $current = %speshes{$cuid};

                warn "couldn't find a before-image for cuid $cuid" unless $current;

                $target = Specialized;
            }
            notegraph($<kind>);
        }
        when /^ 'Before:'/ {
            $target = Before;
        }
        when /^ 'After:'/ {
            $current.before = $current.beforelines.join("\n");
            $current.beforelines = @();
            $target = Logged;
        }
        when /^ 'Facts:'/ {
            if $target ~~ Specialized {
                given $current {
                    .after = .afterlines.join("\n");
                    .afterlines = @();
                    spurt "spesh_diffs_before/{.cuid}.txt", "{.name} (before)\n{.before}";
                    spurt "spesh_diffs_after/{.cuid}.txt", "{.name} (after)\n{.after}";
                    unless $matcher {
                        .before = "";
                        .after = "";
                    }
                }
            }
            $target = Facts;
        }
    }

    say "we've parsed $linecount lines";
    say "we have the following cuids:";

    my @results;
    my @interesting;

    for %speshes.values {
        next if not .after.defined;

        @results.push: $_.diff = qq:x"git diff --patience --color=always --no-index spesh_diffs_before/{.cuid}.txt spesh_diffs_after/{.cuid}.txt";
        my $matched = $matcher && $_ ~~ $ssm;
        @interesting.push: $_.diff if $matched;

        printf "%30s %s (%s)\n", .cuid, ($matched ?? "*" !! " "), .name;
    }

    for @interesting || @results {
        .say
    }
    if $matcher and 1 < @interesting < @results {
        note "matcher selected {+@interesting} out of {+@results} cuids";
    }
    if $matcher and 0 == @interesting {
        note "matcher matched no cuids"
    }
}