@@ -75,18 +75,68 @@ class HLL::Backend::MoarVM {
75
75
$ escaped_squote := q { \\' } ;
76
76
}
77
77
78
+ my $ id_to_thing := nqp ::hash();
79
+
78
80
sub post_process_call_graph_node ($ node ) {
79
- if $ node <allocations > {
80
- for $ node <allocations > -> % alloc_info {
81
- my $ type := % alloc_info <type >;
82
- % alloc_info <type > := $ type . HOW . name ($ type );
81
+ try {
82
+ if nqp ::existskey($ node , " allocations" ) {
83
+ for $ node <allocations > -> % alloc_info {
84
+ my $ type := % alloc_info <type >;
85
+ unless nqp ::existskey($ id_to_thing , % alloc_info <id >) {
86
+ $ id_to_thing {% alloc_info <id >} := $ type . HOW . name ($ type );
87
+ }
88
+ nqp ::deletekey(% alloc_info , " type" );
89
+ }
83
90
}
91
+ unless nqp ::existskey($ id_to_thing , $ node <id >) {
92
+ my $ shared_data := nqp ::hash(
93
+ " file" , $ node <file >,
94
+ " line" , $ node <line >,
95
+ " name" , $ node <name >,
96
+ );
97
+ $ id_to_thing {$ node <id >} := $ shared_data ;
98
+ }
99
+ nqp ::deletekey($ node , " file" );
100
+ nqp ::deletekey($ node , " line" );
101
+ nqp ::deletekey($ node , " name" );
102
+ if nqp ::existskey($ node , " callees" ) {
103
+ for $ node <callees > {
104
+ post_process_call_graph_node($ _ );
105
+ }
106
+ }
107
+ CATCH {
108
+ note (nqp ::getmessage($! ));
109
+ }
110
+ }
111
+ }
112
+
113
+ sub sorted_keys ($ hash ) {
114
+ my @ keys ;
115
+ for $ hash {
116
+ nqp :: push (@ keys , $ _ . key );
84
117
}
85
- if $ node <callees > {
86
- for $ node <callees > {
87
- post_process_call_graph_node($ _ );
118
+ if + @ keys == 0 {
119
+ return @ keys ;
120
+ }
121
+
122
+ # we expect on the order of 6 or 7 keys here, so bubble sort is fine.
123
+ my int $ start := 0 ;
124
+ my int $ numkeys := + @ keys ;
125
+ my str $ swap ;
126
+ my int $ current ;
127
+ while $ start < $ numkeys - 1 {
128
+ $ current := 0 ;
129
+ while $ current < $ numkeys - 1 {
130
+ if @ keys [$ current ] lt @ keys [$ current + 1 ] {
131
+ $ swap := @ keys [$ current ];
132
+ @ keys [$ current ] := @ keys [$ current + 1 ];
133
+ @ keys [$ current + 1 ] := $ swap ;
134
+ }
135
+ $ current ++ ;
88
136
}
137
+ $ start ++ ;
89
138
}
139
+ return @ keys ;
90
140
}
91
141
92
142
sub to_json ($ obj ) {
@@ -107,17 +157,17 @@ class HLL::Backend::MoarVM {
107
157
elsif nqp ::ishash($ obj ) {
108
158
nqp ::push_s(@ pieces , ' {' );
109
159
my $ first := 1 ;
110
- for $ obj {
160
+ for sorted_keys( $ obj ) {
111
161
if $ first {
112
162
$ first := 0 ;
113
163
}
114
164
else {
115
165
nqp ::push_s(@ pieces , ' ,' );
116
166
}
117
167
nqp ::push_s(@ pieces , ' "' );
118
- nqp ::push_s(@ pieces , $ _ . key );
168
+ nqp ::push_s(@ pieces , $ _ );
119
169
nqp ::push_s(@ pieces , ' ":' );
120
- to_json($ _ . value );
170
+ to_json($ obj { $ _ } );
121
171
}
122
172
nqp ::push_s(@ pieces , ' }' );
123
173
}
@@ -155,6 +205,8 @@ class HLL::Backend::MoarVM {
155
205
post_process_call_graph_node($ _ <call_graph >);
156
206
}
157
207
208
+ nqp :: unshift ($ data , $ id_to_thing );
209
+
158
210
if $ want_json {
159
211
to_json($ data );
160
212
nqp ::printfh($ profile_fh , nqp :: join (' ' , @ pieces ));
0 commit comments