rust-anything 3.4.0

Rewrite your app in Rust, one piece at a time, checked against the original's recorded behavior at every step. TypeScript/JavaScript, Python, C, Go, Ruby, Java, Kotlin, PHP and C#
Documentation
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
//! `profile`: run a real workload under the language's own profiler and say what to port.
//!
//! Porting a leaf function rarely pays: each call into Rust costs more than the leaf's work.
//! What pays is moving a hot loop with its callers, keeping data on the Rust side, and using all
//! cores. This step finds those: hot functions in the repository, by inclusive time, with the
//! callers that spend it, call counts where the profiler has them, and a recommendation each.

use crate::walk::Canonical;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::process::Command;

use anyhow::{Context, Result, bail};
use serde::Serialize;
use serde_json::Value;

use crate::lang::Lang;

pub const SCHEMA: &str = "rust-anything/profile@1";

#[derive(Debug, Serialize)]
pub struct Caller {
    pub name: String,
    pub file: String,
    pub line: u32,
    /// Time of the callee spent under this caller.
    pub ms: f64,
    /// Calls from this caller, when the profiler counts calls.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub calls: Option<u64>,
}

#[derive(Debug, Serialize)]
pub struct Hot {
    pub name: String,
    pub file: String,
    pub line: u32,
    /// The analyzed unit holding the function, and whether the unit exports it.
    pub unit: Option<String>,
    pub exported: bool,
    pub self_ms: f64,
    pub total_ms: f64,
    pub total_pct: f64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub calls: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ns_per_call: Option<f64>,
    pub callers: Vec<Caller>,
    pub advice: String,
    /// The unit may move to Rust (not marked keep or blocked).
    pub portable: bool,
    /// Why `bridge` keeps this exported function in the source language (a callback, a RegExp,
    /// a generator...), judged by the same rules `bridge` applies. `go` never picks it.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub js_only: Option<String>,
    /// Parameters of the exported function.
    pub params: Vec<String>,
    /// `function:param` for `bridge --batch` when the advice is to batch it.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub batch: Option<String>,
    /// Share of this function's time spent waiting on child processes it started, itself or
    /// through the functions it calls. `go` passes over a function that mostly waits.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub child_pct: Option<f64>,
}

impl Hot {
    /// Most of its time goes to waiting on child processes: the same work in Rust still waits.
    pub fn mostly_waits(&self) -> bool {
        self.child_pct.is_some_and(|p| p >= 50.0)
    }
}

/// Time a function in user code (the repository or the workload script) spent waiting on child
/// processes it started (`execSync`, `subprocess.run`, `os.system`...), itself or through a
/// library.
#[derive(Debug, Clone, Serialize)]
pub struct ChildProcess {
    /// The program, when the call passes it as a literal (`sleep` for `execSync("sleep 0.2")`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub command: Option<String>,
    pub file: String,
    /// The call's line when the function makes one such call, else the function's line.
    pub line: u32,
    /// The function that makes the call.
    pub function: String,
    pub ms: f64,
    /// Percent of the whole run.
    pub share: f64,
}

#[derive(Debug, Serialize)]
pub struct Report {
    pub schema: &'static str,
    pub language: Lang,
    pub command: Vec<String>,
    pub total_ms: f64,
    /// Share of the run spent in the repository's own code (the rest: runtime, libraries).
    pub repo_pct: f64,
    /// Every function with at least 1% of the run, hottest first; the first `SHOWN` are
    /// serialized and printed, and `go` picks from all of them.
    #[serde(serialize_with = "shown")]
    pub hot: Vec<Hot>,
    /// Functions the repository already compiles to machine code (numba, Cython, JAX).
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub compiled: Vec<Compiled>,
    /// Percent of the run spent waiting on child processes; 0 below 1%.
    #[serde(skip_serializing_if = "is_zero")]
    pub child_pct: f64,
    /// Where the run waits on child processes, longest first (each at least 1% of the run).
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub child_processes: Vec<ChildProcess>,
}

/// Hot functions in the report and in `--json`.
pub const SHOWN: usize = 25;

fn shown<S: serde::Serializer>(hot: &[Hot], s: S) -> std::result::Result<S::Ok, S::Error> {
    s.collect_seq(hot.iter().take(SHOWN))
}

fn is_zero(x: &f64) -> bool {
    *x == 0.0
}

/// What to do instead of porting when the time goes to child processes.
pub const CHILD_ADVICE: &str = "Porting code will not speed this up: keep one long-lived process \
                                open, batch the commands, or call the API directly.";

impl Report {
    /// "38% of the run waits on child processes (`sleep` from `lib.js:6`)." With several,
    /// "(mostly `sleep` from ...)".
    pub fn child_summary(&self) -> Option<String> {
        if self.child_pct < 1.0 {
            return None;
        }
        let Some(top) = self.child_processes.first() else {
            return Some(format!(
                "{:.0}% of the run waits on child processes.",
                self.child_pct
            ));
        };
        let from = match &top.command {
            Some(c) => format!("`{c}` from `{}:{}`", top.file, top.line),
            None => format!("from `{}` at `{}:{}`", top.function, top.file, top.line),
        };
        let mostly = if self.child_processes.len() > 1 {
            "mostly "
        } else {
            ""
        };
        Some(format!(
            "{:.0}% of the run waits on child processes ({mostly}{from}).",
            self.child_pct
        ))
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct Compiled {
    pub name: String,
    pub file: String,
    pub line: u32,
    pub by: &'static str,
}

/// Every Python function in the repository a decorator compiles.
fn compiled_in(root: &Path, lang: Lang) -> Vec<Compiled> {
    if lang != Lang::Python {
        return Vec::new();
    }
    let Ok(ws) = crate::lang::python::discover(root) else {
        return Vec::new();
    };
    ws.files
        .iter()
        .filter(|f| f.kind == crate::ts::discover::FileKind::Source)
        .flat_map(|f| {
            let text = std::fs::read_to_string(root.join(&f.path)).unwrap_or_default();
            crate::risks::python_compiled(&text)
                .into_iter()
                .map(|(name, line, by)| Compiled {
                    name,
                    file: f.path.clone(),
                    line,
                    by,
                })
                .collect::<Vec<_>>()
        })
        .collect()
}

/// The body of the Python function `name` defined at or just after `line`.
fn python_body(text: &str, name: &str, line: u32) -> Option<String> {
    let lines: Vec<&str> = text.lines().collect();
    let start = (line as usize).saturating_sub(1);
    let def = (start..lines.len().min(start + 20)).find(|&i| {
        let t = lines[i].trim_start();
        t.strip_prefix("def ")
            .or_else(|| t.strip_prefix("async def "))
            .is_some_and(|d| d.starts_with(name) && d[name.len()..].trim_start().starts_with('('))
    })?;
    let indent = |l: &str| l.len() - l.trim_start().len();
    let base = indent(lines[def]);
    let body: Vec<&str> = lines[def + 1..]
        .iter()
        .take_while(|l| l.trim().is_empty() || indent(l) > base)
        .copied()
        .collect();
    Some(body.join("\n"))
}

fn calls(body: &str, name: &str) -> bool {
    body.match_indices(&format!("{name}(")).any(|(i, _)| {
        body[..i]
            .chars()
            .next_back()
            .is_none_or(|c| !(c.is_alphanumeric() || c == '_'))
    })
}

/// Advice for a hot function that is compiled already, or that calls compiled code.
fn compiled_advice(root: &Path, h: &Hot, compiled: &[Compiled]) -> Option<String> {
    if let Some(c) = compiled
        .iter()
        .find(|c| c.file == h.file && c.name == h.name)
    {
        return Some(format!(
            "Already compiled by {} (`{}:{}`): a Rust port is unlikely to run it much faster. \
             Port it for a pure Rust app, not for speed.",
            c.by, c.file, c.line
        ));
    }
    let text = std::fs::read_to_string(root.join(&h.file)).ok()?;
    let body = python_body(&text, &h.name, h.line)?;
    let called: Vec<&Compiled> = compiled.iter().filter(|c| calls(&body, &c.name)).collect();
    let first = called.first()?;
    let names: Vec<String> = called.iter().map(|c| format!("`{}`", c.name)).collect();
    Some(format!(
        "{} It calls {} compiled by {} (`{}:{}`): the profiler counts that compiled time here, so \
         porting this may gain little; port the Python around those calls, or measure without them.",
        h.advice,
        names.join(", "),
        first.by,
        first.file,
        first.line
    ))
}

/// Loads the project-wide types `lang::exports` reads on this thread (C typedefs, C# shapes), as
/// `bridge` does before it judges a unit.
pub fn use_project_types(root: &Path, lang: Lang) {
    match lang {
        Lang::C => crate::lang::c::use_typedefs(root),
        Lang::Csharp => crate::lang::csharp::use_shapes(root),
        _ => {}
    }
}

/// The exported functions of `file`, read as `bridge` reads them (for TS/JS a sibling `.d.ts` and
/// type aliases applied), each with the reason `bridge` would keep it in the source language. Call
/// [`use_project_types`] first, as `bridge` does.
pub fn exports_judged(
    root: &Path,
    lang: Lang,
    file: &str,
    text: &str,
) -> Vec<(crate::ts::exports::ExportedFn, Option<String>)> {
    if lang == Lang::Ts {
        let types = crate::bridge::types_in_scope(root, file, text);
        crate::bridge::ts_functions(root, file, text, &types)
            .into_iter()
            .map(|f| {
                let reason = crate::bridge::js_only_reason(&f, &types);
                (f, reason)
            })
            .collect()
    } else {
        crate::lang::exports(lang, file, text)
            .0
            .into_iter()
            .map(|f| {
                let reason = crate::bridge::js_only_reason(&f, &[]);
                (f, reason)
            })
            .collect()
    }
}

/// One function's numbers before advice: key is (file, name, line).
#[derive(Default)]
struct Acc {
    self_ms: f64,
    total_ms: f64,
    calls: Option<u64>,
    callers: HashMap<(String, String, u32), (f64, Option<u64>)>,
    /// Waiting on child processes this function started itself.
    spawn_ms: f64,
    /// Waiting on child processes under this function, its callees' included.
    waits_ms: f64,
}

type Key = (String, String, u32);

pub fn profile(root: &Path, command: &[String], lang: Option<Lang>) -> Result<Report> {
    if command.is_empty() {
        bail!(
            "give the workload to run after `--`, e.g. `rust-anything profile . -- node bench.mjs`"
        );
    }
    let root = root
        .canonical()
        .with_context(|| format!("resolving {}", root.display()))?;
    let lang = lang.unwrap_or_else(|| Lang::detect(&root));
    let work = std::env::temp_dir().join(format!("rust-anything-profile-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&work);
    std::fs::create_dir_all(&work)?;
    let no_waits = |(functions, total_ms)| (functions, 0.0, total_ms);
    let (functions, unowned_ms, total_ms) = match lang {
        Lang::Ts => node(&root, command, &work)?,
        Lang::Python => python(&root, command, &work)?,
        Lang::Go => no_waits(go(&root, command, &work)?),
        Lang::Java => no_waits(java(&root, command, &work)?),
        Lang::Kotlin => bail!(
            "`profile` does not run Kotlin workloads yet; use JFR (`jfr print --stack-depth 256`) \
             and port the hot loops it shows"
        ),
        Lang::Ruby => no_waits(ruby(&root, command, &work)?),
        Lang::C => bail!(
            "`profile` runs TS/JS, Python, Go, Java and Ruby workloads; for C, use `perf record -g` \
             and port the hot loops it shows"
        ),
        Lang::Php => bail!(
            "`profile` does not run PHP workloads yet; use SPX or Xdebug's profiler and port the \
             hot loops it shows"
        ),
        Lang::Csharp => bail!(
            "`profile` does not run C# workloads yet; use `dotnet-trace` or your IDE's profiler and \
             port the hot loops it shows"
        ),
        Lang::Powershell => bail!(
            "`profile` does not run PowerShell workloads yet. Time one `pwsh` start first \
             (`Measure-Command {{ pwsh -NoProfile -c 1 }}`): when a tool starts a shell per \
             command, start-up is the slow part and no port fixes it; keep one shell open. For a \
             script whose own logic is slow, time its functions with `Measure-Command` and port \
             those"
        ),
    };
    let _ = std::fs::remove_dir_all(&work);
    Ok(report(
        &root, lang, command, functions, unowned_ms, total_ms,
    ))
}

/// A frame's file: repository-relative, or absolute for user code elsewhere (a workload
/// script); `None` for the runtime and installed libraries.
fn relative(root: &Path, file: &str) -> Option<String> {
    let decoded;
    let path = if let Some(url_path) = file.strip_prefix("file://") {
        decoded = percent_encoding::percent_decode_str(url_path).decode_utf8_lossy();
        decoded.as_ref()
    } else {
        file
    };
    if !path.starts_with('/') && !path.contains(":\\") {
        let rel = path.trim_start_matches("./");
        return (!rel.starts_with(['<', '~']) && !rel.contains(':') && root.join(rel).is_file())
            .then(|| rel.replace('\\', "/")); // node:internal/..., <frozen ...>, ~
    }
    // Installed libraries and runtimes: npm, Python, Ruby gems and stdlib, Go modules and GOROOT.
    if path.split(['/', '\\']).any(|p| {
        matches!(
            p,
            "node_modules"
                | "site-packages"
                | "dist-packages"
                | ".venv"
                | "venv"
                | "gems"
                | "vendor"
        )
    }) || python_runtime(path)
        || ["/lib/ruby/", "/pkg/mod/"].iter().any(|d| path.contains(d))
        || goroot().is_some_and(|g| path.starts_with(g))
    {
        return None;
    }
    let p = PathBuf::from(path);
    Some(match p.strip_prefix(root) {
        Ok(rel) => rel.to_string_lossy().replace('\\', "/"),
        Err(_) => path.to_string(),
    })
}

/// A Python installation's library: a `lib` or `lib64` folder holding `python3` or `python3.N`.
fn python_runtime(path: &str) -> bool {
    let parts: Vec<&str> = path.split(['/', '\\']).collect();
    parts.windows(2).any(|w| {
        matches!(w[0], "lib" | "lib64")
            && w[1].strip_prefix("python3").is_some_and(|v| {
                v.is_empty()
                    || v.strip_prefix('.')
                        .is_some_and(|n| !n.is_empty() && n.bytes().all(|b| b.is_ascii_digit()))
            })
    })
}

/// Go's own sources, which are not user code.
fn goroot() -> Option<&'static str> {
    static GOROOT: std::sync::OnceLock<Option<String>> = std::sync::OnceLock::new();
    GOROOT
        .get_or_init(|| {
            let out = crate::process::output(
                Command::new("go").args(["env", "GOROOT"]),
                crate::process::QUICK,
            )
            .ok()?;
            let root = String::from_utf8(out.stdout).ok()?.trim().to_string();
            (!root.is_empty()).then(|| format!("{root}/"))
        })
        .as_deref()
}

/// Whether a frame file is inside the repository (relative).
fn in_repo(file: &str) -> bool {
    !file.starts_with('/')
}

// ---- node --cpu-prof ------------------------------------------------------------------------

/// Functions, ms of waits on child processes no user function owns, and the run's ms.
type Waited = (HashMap<Key, Acc>, f64, f64);

fn node(root: &Path, command: &[String], work: &Path) -> Result<Waited> {
    let dir = work.join("node");
    std::fs::create_dir_all(&dir)?;
    let mut options = std::env::var("NODE_OPTIONS").unwrap_or_default();
    let dir_option = dir
        .to_string_lossy()
        .replace('\\', "\\\\")
        .replace('"', "\\\"");
    options.push_str(&format!(" --cpu-prof --cpu-prof-dir=\"{dir_option}\""));
    let status = crate::process::status(
        Command::new(&command[0])
            .stdout(std::io::stderr())
            .args(&command[1..])
            .current_dir(root)
            .env("NODE_OPTIONS", options.trim()),
        crate::process::TESTS,
    )
    .with_context(|| format!("running {}", command[0]))?;
    if !status.success() {
        bail!("the workload failed ({status})");
    }
    let mut functions: HashMap<Key, Acc> = HashMap::new();
    let mut total = 0.0;
    let mut unowned = 0.0;
    for entry in std::fs::read_dir(&dir)?.flatten() {
        let profile: Value = serde_json::from_str(&std::fs::read_to_string(entry.path())?)?;
        total += merge_cpuprofile(root, &profile, &mut functions, &mut unowned);
    }
    if total == 0.0 {
        bail!("no CPU profile was written: is the workload a node process?");
    }
    Ok((functions, unowned, total))
}

/// A `.cpuprofile` node: its frame when it is user code, its own time, its children.
struct Frame {
    key: Option<Key>,
    self_ms: f64,
    children: Vec<u64>,
    /// `execSync` and its kin: the thread blocks until the child process exits.
    spawn: bool,
}

/// `node:child_process` functions that block until the child exits.
const NODE_SPAWNS: [&str; 3] = ["execSync", "execFileSync", "spawnSync"];

/// Adds one `.cpuprofile` to `functions`, and to `unowned` the waits on child processes with no
/// user frame above them; returns its duration in ms.
fn merge_cpuprofile(
    root: &Path,
    profile: &Value,
    functions: &mut HashMap<Key, Acc>,
    unowned: &mut f64,
) -> f64 {
    let Some(nodes) = profile["nodes"].as_array() else {
        return 0.0;
    };
    let samples = profile["samples"].as_array().map_or(0, Vec::len).max(1);
    let duration_ms = (profile["endTime"].as_f64().unwrap_or(0.0)
        - profile["startTime"].as_f64().unwrap_or(0.0))
        / 1000.0;
    let per_sample = duration_ms / samples as f64;
    let mut by_id: HashMap<u64, Frame> = HashMap::new();
    let mut is_child = HashSet::new();
    for n in nodes {
        let id = n["id"].as_u64().unwrap_or(0);
        let frame = &n["callFrame"];
        let name = frame["functionName"].as_str().unwrap_or("").to_string();
        let url = frame["url"].as_str().unwrap_or("");
        let line = u32::try_from(frame["lineNumber"].as_i64().unwrap_or(0) + 1).unwrap_or(0);
        let key = relative(root, url).map(|file| {
            let shown = if name.is_empty() {
                format!("(anonymous):{line}")
            } else {
                name.clone()
            };
            (file, shown, line)
        });
        let children: Vec<u64> = n["children"]
            .as_array()
            .map_or_else(Vec::new, |c| c.iter().filter_map(Value::as_u64).collect());
        is_child.extend(children.iter().copied());
        let self_ms = n["hitCount"].as_f64().unwrap_or(0.0) * per_sample;
        let spawn = url == "node:child_process" && NODE_SPAWNS.contains(&name.as_str());
        by_id.insert(
            id,
            Frame {
                key,
                self_ms,
                children,
                spawn,
            },
        );
    }
    // Inclusive time per node, depth first.
    fn total(id: u64, by_id: &HashMap<u64, Frame>, memo: &mut HashMap<u64, f64>) -> f64 {
        if let Some(t) = memo.get(&id) {
            return *t;
        }
        let Some(frame) = by_id.get(&id) else {
            return 0.0;
        };
        let t = frame.self_ms
            + frame
                .children
                .iter()
                .map(|c| total(*c, by_id, memo))
                .sum::<f64>();
        memo.insert(id, t);
        t
    }
    let mut memo = HashMap::new();
    // Walk from the roots, tracking the repo frames on the stack: recursion counts once, and a
    // callee's caller is the nearest *named* repo frame above it (callbacks are skipped).
    let roots: Vec<u64> = by_id
        .keys()
        .filter(|id| !is_child.contains(id))
        .copied()
        .collect();
    let mut stack: Vec<(u64, Vec<Key>)> = roots.into_iter().map(|r| (r, Vec::new())).collect();
    while let Some((id, active)) = stack.pop() {
        let Some(Frame {
            key,
            self_ms,
            children,
            spawn,
        }) = by_id.get(&id)
        else {
            continue;
        };
        if *spawn {
            // The outermost blocking call: everything under it is the wait (`execSync` calls
            // `spawnSync`). It goes to the nearest named user frame, as callers do, and counts
            // once for each frame on the stack.
            let waited = total(id, &by_id, &mut memo);
            if let Some(caller) = active
                .iter()
                .rev()
                .find(|c| !c.1.starts_with("(anonymous)"))
                .or(active.last())
            {
                functions.entry(caller.clone()).or_default().spawn_ms += waited;
                let mut seen = HashSet::new();
                for k in active.iter().filter(|k| seen.insert(*k)) {
                    functions.entry(k.clone()).or_default().waits_ms += waited;
                }
            } else {
                *unowned += waited;
            }
            continue;
        }
        let mut next = active.clone();
        if let Some(k) = key {
            let inclusive = total(id, &by_id, &mut memo);
            let acc = functions.entry(k.clone()).or_default();
            acc.self_ms += self_ms;
            if !active.contains(k) {
                acc.total_ms += inclusive;
                if let Some(caller) = active
                    .iter()
                    .rev()
                    .find(|c| !c.1.starts_with("(anonymous)"))
                    && caller != k
                {
                    acc.callers.entry(caller.clone()).or_default().0 += inclusive;
                }
            }
            // Anonymous callbacks stay on the stack too; the caller lookup above skips them.
            next.push(k.clone());
        }
        for c in children {
            stack.push((*c, next.clone()));
        }
    }
    duration_ms
}

// ---- Python cProfile ------------------------------------------------------------------------

const PSTATS_PY: &str = r#"
import json, os, pstats, sys, sysconfig
stats = pstats.Stats(sys.argv[1]).stats
stdlib = os.path.realpath(sysconfig.get_paths()["stdlib"])
def in_stdlib(file):
    return file[:1] not in ("", "<", "~") and os.path.dirname(os.path.realpath(file)) == stdlib
out = []
for (file, line, name), (cc, nc, tt, ct, callers) in stats.items():
    out.append({
        "file": file, "line": line, "name": name, "calls": nc, "self": tt * 1000, "total": ct * 1000,
        "stdlib": in_stdlib(file),
        "callers": [{"file": f, "line": l, "name": n, "calls": c[1], "total": c[3] * 1000} for (f, l, n), c in callers.items()],
    })
json.dump(out, open(sys.argv[2], "w"))
"#;

fn python(root: &Path, command: &[String], work: &Path) -> Result<Waited> {
    let program = Path::new(&command[0])
        .file_name()
        .map(|n| n.to_string_lossy().into_owned())
        .unwrap_or_default();
    if !program.starts_with("python") {
        bail!(
            "for Python, give the interpreter and script: `rust-anything profile . -- python3 bench.py`"
        );
    }
    let stats = work.join("profile.pstats");
    let start = std::time::Instant::now();
    let status = crate::process::status(
        Command::new(&command[0])
            .stdout(std::io::stderr())
            .args(["-m", "cProfile", "-o"])
            .arg(&stats)
            .args(&command[1..])
            .current_dir(root),
        crate::process::TESTS,
    )
    .with_context(|| format!("running {}", command[0]))?;
    let total_ms = start.elapsed().as_secs_f64() * 1000.0;
    if !status.success() {
        bail!("the workload failed ({status})");
    }
    let json = work.join("profile.json");
    let script = work.join("pstats_json.py");
    std::fs::write(&script, PSTATS_PY)?;
    let status = crate::process::status(
        Command::new(&command[0])
            .stdout(std::io::stderr())
            .arg(&script)
            .arg(&stats)
            .arg(&json),
        crate::process::TESTS,
    )?;
    if !status.success() {
        bail!("reading the cProfile output failed");
    }
    let entries: Vec<Value> = serde_json::from_str(&std::fs::read_to_string(&json)?)?;
    let (functions, unowned) = ingest_entries(root, &entries);
    Ok((functions, unowned, total_ms))
}

/// Function entries from a tracing profiler (cProfile, the Ruby tracer): file, line, name,
/// calls, self and total ms, and callers with their calls and ms. Also returns the ms of waits on
/// child processes that no user function owns.
fn ingest_entries(root: &Path, entries: &[Value]) -> (HashMap<Key, Acc>, f64) {
    let mut functions: HashMap<Key, Acc> = HashMap::new();
    for e in entries {
        let raw_file = e["file"].as_str().unwrap_or("");
        let Some(file) = relative(root, raw_file) else {
            continue;
        };
        let key = (
            file,
            e["name"].as_str().unwrap_or("").to_string(),
            u32::try_from(e["line"].as_u64().unwrap_or(0)).unwrap_or(0),
        );
        let acc = functions.entry(key).or_default();
        acc.self_ms += e["self"].as_f64().unwrap_or(0.0);
        acc.total_ms += e["total"].as_f64().unwrap_or(0.0);
        acc.calls = Some(acc.calls.unwrap_or(0) + e["calls"].as_u64().unwrap_or(0));
        for c in e["callers"].as_array().into_iter().flatten() {
            let Some(cfile) = relative(root, c["file"].as_str().unwrap_or("")) else {
                continue;
            };
            let ckey = (
                cfile,
                c["name"].as_str().unwrap_or("").to_string(),
                u32::try_from(c["line"].as_u64().unwrap_or(0)).unwrap_or(0),
            );
            let slot = acc.callers.entry(ckey).or_default();
            slot.0 += c["total"].as_f64().unwrap_or(0.0);
            slot.1 = Some(slot.1.unwrap_or(0) + c["calls"].as_u64().unwrap_or(0));
        }
    }
    let unowned = child_waits(root, entries, &mut functions);
    (functions, unowned)
}

/// Python's calls that wait for a child process: `subprocess.run` and its kin, a `Popen`'s
/// `communicate`, `wait` and `__exit__` (`with Popen(...)`), and `os.system` (a Python wrapper on some versions, a builtin on
/// others).
fn python_spawn(file: &str, name: &str) -> bool {
    let base = file.rsplit(['/', '\\']).next().unwrap_or(file);
    (base == "subprocess.py"
        && matches!(
            name,
            "run"
                | "call"
                | "check_call"
                | "check_output"
                | "getoutput"
                | "getstatusoutput"
                | "communicate"
                | "wait"
                | "__exit__"
        ))
        || (base == "os.py" && name == "system")
        || (file == "~"
            && matches!(
                name,
                "<built-in method posix.system>" | "<built-in method nt.system>"
            ))
}

/// Waits on child processes in a tracing profile (Python's `subprocess` calls). The caller edges
/// are followed through library frames too (`fetch` -> a library's `run_git` ->
/// `subprocess.run`): each user function gets the waits under it (`waits_ms`) and the ones it
/// starts with no user function in between (`spawn_ms`).
fn child_waits(root: &Path, entries: &[Value], functions: &mut HashMap<Key, Acc>) -> f64 {
    let raw = |v: &Value| -> Key {
        (
            v["file"].as_str().unwrap_or("").to_string(),
            v["name"].as_str().unwrap_or("").to_string(),
            u32::try_from(v["line"].as_u64().unwrap_or(0)).unwrap_or(0),
        )
    };
    // The profiled interpreter's standard library, not a repository or dependency module that
    // happens to be called `subprocess.py`.
    let stdlib: HashSet<&str> = entries
        .iter()
        .filter(|e| e["stdlib"].as_bool() == Some(true))
        .filter_map(|e| e["file"].as_str())
        .collect();
    let is_spawn =
        |k: &Key| python_spawn(&k.0, &k.1) && (k.0 == "~" || stdlib.contains(k.0.as_str()));
    if !entries.iter().any(|e| is_spawn(&raw(e))) {
        return 0.0;
    }
    let mut ids: HashMap<Key, usize> = HashMap::new();
    let mut keys: Vec<Key> = Vec::new();
    let mut id = |k: Key| -> usize {
        *ids.entry(k.clone()).or_insert_with(|| {
            keys.push(k);
            keys.len() - 1
        })
    };
    let mut edges: Vec<(usize, usize, f64)> = Vec::new(); // (caller, callee, ms)
    let mut spawn_edges: Vec<(usize, f64)> = Vec::new(); // (caller, ms)
    let mut totals: Vec<(usize, f64)> = Vec::new();
    for e in entries {
        let me = raw(e);
        let spawner = is_spawn(&me);
        let i = id(me);
        totals.push((i, e["total"].as_f64().unwrap_or(0.0)));
        for c in e["callers"].as_array().into_iter().flatten() {
            let ck = raw(c);
            let ms = c["total"].as_f64().unwrap_or(0.0);
            if spawner {
                // The outermost waiting call: `subprocess.run` calls `communicate`, which calls
                // `wait`; each wait counts once.
                if !is_spawn(&ck) {
                    spawn_edges.push((id(ck), ms));
                }
            } else if !is_spawn(&ck) {
                edges.push((id(ck), i, ms));
            }
        }
    }
    let n = keys.len();
    let mut g = Graph {
        total: vec![0.0; n],
        spawn: vec![0.0; n],
        callees: vec![Vec::new(); n],
        callers: vec![Vec::new(); n],
    };
    for (i, t) in totals {
        g.total[i] += t;
    }
    for (c, ms) in spawn_edges {
        g.spawn[c] += ms;
    }
    for (caller, callee, ms) in edges {
        if caller != callee && g.total[callee] > 0.0 {
            g.callees[caller].push((callee, (ms / g.total[callee]).min(1.0)));
            g.callers[callee].push(caller);
        }
    }
    let user: Vec<Option<Key>> = keys
        .iter()
        .map(|k| {
            (!is_spawn(k))
                .then(|| relative(root, &k.0).map(|f| (f, k.1.clone(), k.2)))
                .flatten()
        })
        .collect();
    let waits = g.confirmed();
    // Waits started by library frames and reaching a frame through library frames only, so a
    // user function's own waits are not listed again for its user caller, nor for itself when
    // it recurses through a library (`ast.NodeVisitor`).
    let foreign = g.waits(|c| user[c].is_none());
    let mut owned = 0.0;
    for (i, k) in user.iter().enumerate() {
        let Some(acc) = k.as_ref().and_then(|k| functions.get_mut(k)) else {
            continue;
        };
        acc.waits_ms += waits[i];
        let started = g.spawn[i]
            + g.callees[i]
                .iter()
                .filter(|(c, _)| user[*c].is_none())
                .map(|(c, share)| share * foreign[*c])
                .sum::<f64>();
        acc.spawn_ms += started;
        owned += started;
    }
    (g.spawn.iter().sum::<f64>() - owned).max(0.0)
}

/// A call graph over a tracing profile's frames.
struct Graph {
    total: Vec<f64>,
    /// Waits on child processes started by this frame itself.
    spawn: Vec<f64>,
    /// caller -> [(callee, share of the callee's time spent under this caller)]
    callees: Vec<Vec<(usize, f64)>>,
    callers: Vec<Vec<usize>>,
}

impl Graph {
    /// Waits under each frame, counting only the frames `only` accepts: its own, plus each
    /// callee's in the share of the callee's time spent under it, never more than its own time.
    /// Recursion (a cycle of calls) shares one total, so no wait goes around a cycle twice.
    fn waits(&self, only: impl Fn(usize) -> bool) -> Vec<f64> {
        let n = self.total.len();
        // Only frames above a wait matter.
        let mut relevant = vec![false; n];
        let mut todo: Vec<usize> = (0..n).filter(|&i| self.spawn[i] > 0.0 && only(i)).collect();
        while let Some(i) = todo.pop() {
            if !std::mem::replace(&mut relevant[i], true) {
                todo.extend(self.callers[i].iter().copied().filter(|&c| !relevant[c]));
            }
        }
        let mut waits = vec![0.0; n];
        let mut component = vec![usize::MAX; n];
        for (id, scc) in sccs(&self.callees, &relevant).into_iter().enumerate() {
            for &m in &scc {
                component[m] = id;
            }
            let own: f64 = scc
                .iter()
                .filter(|&&m| only(m))
                .map(|&m| self.spawn[m])
                .sum();
            let under: f64 = scc
                .iter()
                .flat_map(|&m| self.callees[m].iter())
                .filter(|(c, _)| relevant[*c] && component[*c] != id && only(*c))
                .map(|(c, share)| share * waits[*c])
                .sum();
            for &m in &scc {
                waits[m] = (own + under).min(self.total[m].max(self.spawn[m]));
            }
        }
        waits
    }
}

impl Graph {
    /// Waits known to happen under each frame: its own, plus from each callee the part the
    /// callee's other callers cannot account for (its waits less the time it spent under them).
    /// A per-caller share would charge a shared helper's waits to callers that only ran its CPU
    /// work. Calls within a recursive component confirm nothing for the other members.
    fn confirmed(&self) -> Vec<f64> {
        let n = self.total.len();
        let mut relevant = vec![false; n];
        let mut todo: Vec<usize> = (0..n).filter(|&i| self.spawn[i] > 0.0).collect();
        while let Some(i) = todo.pop() {
            if !std::mem::replace(&mut relevant[i], true) {
                todo.extend(self.callers[i].iter().copied().filter(|&c| !relevant[c]));
            }
        }
        let mut waits = vec![0.0; n];
        let mut component = vec![usize::MAX; n];
        for (id, scc) in sccs(&self.callees, &relevant).into_iter().enumerate() {
            for &m in &scc {
                component[m] = id;
            }
            for &m in &scc {
                let under: f64 = self.callees[m]
                    .iter()
                    .filter(|(c, _)| relevant[*c] && component[*c] != id)
                    .map(|&(c, share)| (waits[c] - (1.0 - share) * self.total[c]).max(0.0))
                    .sum();
                waits[m] = (self.spawn[m] + under).min(self.total[m].max(self.spawn[m]));
            }
        }
        waits
    }
}

/// Strongly connected components of the relevant frames, callees before their callers
/// (Tarjan's algorithm, without recursion).
fn sccs(edges: &[Vec<(usize, f64)>], relevant: &[bool]) -> Vec<Vec<usize>> {
    let n = edges.len();
    let mut index = vec![usize::MAX; n];
    let mut low = vec![0; n];
    let mut on_stack = vec![false; n];
    let mut stack = Vec::new();
    let mut out = Vec::new();
    let mut next = 0;
    for start in (0..n).filter(|&i| relevant[i]) {
        if index[start] != usize::MAX {
            continue;
        }
        index[start] = next;
        low[start] = next;
        next += 1;
        stack.push(start);
        on_stack[start] = true;
        // (frame, next edge to look at)
        let mut work = vec![(start, 0usize)];
        while let Some(&(v, e)) = work.last() {
            if let Some(&(w, _)) = edges[v].get(e) {
                if let Some(top) = work.last_mut() {
                    top.1 += 1;
                }
                if !relevant[w] {
                    continue;
                }
                if index[w] == usize::MAX {
                    index[w] = next;
                    low[w] = next;
                    next += 1;
                    stack.push(w);
                    on_stack[w] = true;
                    work.push((w, 0));
                } else if on_stack[w] {
                    low[v] = low[v].min(index[w]);
                }
                continue;
            }
            work.pop();
            if let Some(&(parent, _)) = work.last() {
                low[parent] = low[parent].min(low[v]);
            }
            if low[v] == index[v] {
                let mut scc = Vec::new();
                while let Some(w) = stack.pop() {
                    on_stack[w] = false;
                    scc.push(w);
                    if w == v {
                        break;
                    }
                }
                out.push(scc);
            }
        }
    }
    out
}

// ---- stack samples (Go pprof, Java JFR) -----------------------------------------------------

/// Adds sampled stacks, each root first with `None` for frames outside user code, and the time
/// it stands for. A function's total counts once per sample (recursion); its caller is the
/// nearest named user frame above it (lambdas and closures are skipped).
fn merge_samples(samples: &[(Vec<Option<Key>>, f64)], functions: &mut HashMap<Key, Acc>) {
    for (stack, ms) in samples {
        let mut seen: Vec<&Key> = Vec::new();
        let mut named: Option<&Key> = None;
        for frame in stack {
            let Some(k) = frame else { continue };
            if !seen.contains(&k) {
                seen.push(k);
                let acc = functions.entry(k.clone()).or_default();
                acc.total_ms += ms;
                if let Some(caller) = named.filter(|c| *c != k) {
                    acc.callers.entry(caller.clone()).or_default().0 += ms;
                }
            }
            if !k.1.starts_with("(anonymous)") {
                named = Some(k);
            }
        }
        if let Some(Some(leaf)) = stack.last() {
            functions.entry(leaf.clone()).or_default().self_ms += ms;
        }
    }
}

/// A Go function's own name: `example.com/x/pkg.Slugify` is `Slugify`, `pkg.(*T).M` is `M`;
/// closures (`Outer.func1`) are anonymous.
fn go_name(full: &str) -> String {
    let tail = full.rsplit('/').next().unwrap_or(full);
    let rest = tail.split_once('.').map_or(tail, |(_, r)| r);
    let parts: Vec<&str> = rest.split('.').collect();
    match parts.as_slice() {
        [.., outer, f] if f.starts_with("func") && f[4..].chars().all(|c| c.is_ascii_digit()) => {
            format!("(anonymous) in {outer}")
        }
        [.., last] => last.to_string(),
        [] => rest.to_string(),
    }
}

/// `go tool pprof -raw` output: samples (leaf first, by location id) and locations (each one or
/// more frames, innermost first: `func file:line:col s=start`).
fn parse_pprof_raw(root: &Path, raw: &str) -> Vec<(Vec<Option<Key>>, f64)> {
    let mut locations: HashMap<u64, Vec<Option<Key>>> = HashMap::new();
    let mut samples: Vec<(Vec<u64>, f64)> = Vec::new();
    let mut section = "";
    let mut current: Option<u64> = None;
    // Which sample value is CPU time: `samples/count cpu/nanoseconds` puts it second.
    let mut value_index = 1;
    for line in raw.lines() {
        match line.trim_end() {
            "Samples:" => {
                section = "samples";
                continue;
            }
            "Locations" => {
                section = "locations";
                continue;
            }
            "Mappings" => {
                section = "";
                continue;
            }
            _ => {}
        }
        match section {
            "samples" if line.contains(':') => {
                let (values, ids) = line.split_once(':').unwrap_or_default();
                let values: Vec<f64> = values
                    .split_whitespace()
                    .filter_map(|v| v.parse().ok())
                    .collect();
                let ids: Vec<u64> = ids
                    .split_whitespace()
                    .filter_map(|v| v.parse().ok())
                    .collect();
                if let Some(ns) = values.get(value_index).or(values.last()) {
                    samples.push((ids, ns / 1e6));
                }
            }
            "samples" => {
                let kinds: Vec<&str> = line.split_whitespace().collect();
                if let Some(i) = kinds.iter().position(|k| k.ends_with("/nanoseconds")) {
                    value_index = i;
                }
            }
            "locations" => {
                let text = line.trim_start();
                let frame_text = match text.split_once(": ") {
                    Some((id, rest)) if id.chars().all(|c| c.is_ascii_digit()) => {
                        current = id.parse().ok();
                        // `0x4c2ded M=1 func file:line:col s=start`
                        rest.split_whitespace()
                            .skip(2)
                            .collect::<Vec<_>>()
                            .join(" ")
                    }
                    _ => text.to_string(),
                };
                let Some(id) = current else { continue };
                let mut parts = frame_text.split_whitespace();
                let (Some(func), Some(place)) = (parts.next(), parts.next()) else {
                    continue;
                };
                let start = parts
                    .find_map(|p| p.strip_prefix("s="))
                    .and_then(|s| s.parse::<u32>().ok())
                    .unwrap_or(0);
                let file = place.rsplitn(3, ':').nth(2).unwrap_or(place);
                let key = relative(root, file).map(|f| (f, go_name(func), start));
                locations.entry(id).or_default().push(key);
            }
            _ => {}
        }
    }
    samples
        .into_iter()
        .map(|(ids, ms)| {
            // Leaf first, and inlined frames innermost first: reversed, the stack is root first.
            let mut stack: Vec<Option<Key>> = ids
                .iter()
                .flat_map(|id| locations.get(id).cloned().unwrap_or_default())
                .collect();
            stack.reverse();
            (stack, ms)
        })
        .collect()
}

/// Go: `go test` with `-cpuprofile` (Go has no profiler to attach from outside), read with
/// `go tool pprof -raw`.
fn go(root: &Path, command: &[String], work: &Path) -> Result<(HashMap<Key, Acc>, f64)> {
    let program = Path::new(&command[0])
        .file_name()
        .map(|n| n.to_string_lossy().into_owned())
        .unwrap_or_default();
    if program != "go" || command.get(1).map(String::as_str) != Some("test") {
        bail!(
            "for Go, profile a benchmark or test: `rust-anything profile . -- go test -run XXX -bench . ./pkg` \
             (Go profiles only from inside the process; `go test` adds -cpuprofile)"
        );
    }
    let out = work.join("cpu.pprof");
    let status = crate::process::status(
        Command::new(&command[0])
            .stdout(std::io::stderr())
            .arg("test")
            .arg(format!("-cpuprofile={}", out.display()))
            .args(&command[2..])
            .current_dir(root),
        crate::process::TESTS,
    )
    .with_context(|| format!("running {}", command[0]))?;
    if !status.success() {
        bail!("the workload failed ({status})");
    }
    let raw = crate::process::output(
        Command::new("go")
            .args(["tool", "pprof", "-raw"])
            .arg(&out)
            .current_dir(root),
        crate::process::TESTS,
    )?;
    if !raw.status.success() {
        bail!(
            "go tool pprof failed: {}",
            String::from_utf8_lossy(&raw.stderr)
        );
    }
    let samples = parse_pprof_raw(root, &String::from_utf8_lossy(&raw.stdout));
    let total: f64 = samples.iter().map(|(_, ms)| ms).sum();
    let mut functions = HashMap::new();
    merge_samples(&samples, &mut functions);
    Ok((functions, total))
}

/// The line declaring method `name` in Java source: the nearest declaration at or above `near`.
fn java_declaration(text: &str, name: &str, near: u32) -> Option<u32> {
    let needle = format!("{name}(");
    let mut best = None;
    for (i, line) in text.lines().enumerate() {
        let n = u32::try_from(i + 1).unwrap_or(u32::MAX);
        if n > near.max(1) {
            break;
        }
        let Some(at) = line.find(&needle) else {
            continue;
        };
        let before = line[..at].trim_end();
        // A declaration names a type or modifier before the method: `static int pages(`.
        let declares = before
            .ends_with(|c: char| c.is_alphanumeric() || matches!(c, '>' | ']' | '_'))
            && !before.contains('=')
            && !before.contains("return")
            && !before.contains("new ")
            && !before.ends_with('.')
            && !line.trim_end().ends_with(';');
        if declares && at > 0 && line.as_bytes()[at - 1].is_ascii_whitespace() {
            best = Some(n);
        }
    }
    best
}

/// Java: Flight Recorder's execution samples, read with `jfr print --json`.
fn java(root: &Path, command: &[String], work: &Path) -> Result<(HashMap<Key, Acc>, f64)> {
    let recording = work.join("profile.jfr");
    let mut options = std::env::var("JAVA_TOOL_OPTIONS").unwrap_or_default();
    options.push_str(&format!(
        " -XX:StartFlightRecording=filename={},settings=profile -XX:FlightRecorderOptions=stackdepth=256",
        recording.display()
    ));
    let status = crate::process::status(
        Command::new(&command[0])
            .stdout(std::io::stderr())
            .args(&command[1..])
            .current_dir(root)
            .env("JAVA_TOOL_OPTIONS", options.trim()),
        crate::process::TESTS,
    )
    .with_context(|| format!("running {}", command[0]))?;
    if !status.success() {
        bail!("the workload failed ({status})");
    }
    let out = crate::process::output(
        Command::new("jfr")
            .args([
                "print",
                "--json",
                "--stack-depth",
                "256",
                "--events",
                "jdk.ExecutionSample",
            ])
            .arg(&recording),
        crate::process::TESTS,
    )
    .context("running jfr (it comes with the JDK)")?;
    if !out.status.success() {
        bail!("jfr print failed: {}", String::from_utf8_lossy(&out.stderr));
    }
    let json: Value = serde_json::from_slice(&out.stdout).context("jfr output")?;
    // Sources by class: `com/example/text/Strings` -> its file in the repository.
    let mut sources: HashMap<String, String> = HashMap::new();
    for entry in crate::walk::builder(root).build() {
        let Ok(entry) = entry else { continue };
        let path = entry.path();
        if path.extension().is_some_and(|e| e == "java")
            && let Ok(rel) = path.strip_prefix(root)
        {
            let rel = rel.to_string_lossy().replace('\\', "/");
            let text = std::fs::read_to_string(path).unwrap_or_default();
            let package = text
                .lines()
                .find_map(|l| {
                    l.trim()
                        .strip_prefix("package ")
                        .map(|p| p.trim_end_matches(';').trim().to_string())
                })
                .unwrap_or_default();
            let class = path
                .file_stem()
                .map(|s| s.to_string_lossy().into_owned())
                .unwrap_or_default();
            let fq = if package.is_empty() {
                class
            } else {
                format!("{}/{class}", package.replace('.', "/"))
            };
            sources.insert(fq, rel);
        }
    }
    const SAMPLE_MS: f64 = 10.0; // the `profile` settings sample Java threads every 10 ms
    let mut raw: Vec<Vec<Option<(String, String, u32)>>> = Vec::new();
    for event in json["recording"]["events"].as_array().into_iter().flatten() {
        let frames = event["values"]["stackTrace"]["frames"]
            .as_array()
            .cloned()
            .unwrap_or_default();
        let mut stack: Vec<Option<(String, String, u32)>> = frames
            .iter()
            .map(|f| {
                let class = f["method"]["type"]["name"].as_str()?;
                let outer = class.split('$').next().unwrap_or(class);
                let file = sources.get(outer)?;
                let name = f["method"]["name"].as_str()?.to_string();
                let line = u32::try_from(f["lineNumber"].as_i64().unwrap_or(0)).unwrap_or(0);
                Some((file.clone(), name, line))
            })
            .collect();
        stack.reverse();
        raw.push(stack);
    }
    // Key each method by its declaration line, found from the lowest sampled line.
    let mut lowest: HashMap<(String, String), u32> = HashMap::new();
    for (file, name, line) in raw.iter().flatten().flatten() {
        let e = lowest.entry((file.clone(), name.clone())).or_insert(*line);
        *e = (*e).min(*line);
    }
    let mut texts: HashMap<String, String> = HashMap::new();
    let mut key = |file: &str, name: &str| -> Key {
        if name.starts_with("lambda$") {
            return (file.to_string(), format!("(anonymous) {name}"), 0);
        }
        let text = texts
            .entry(file.to_string())
            .or_insert_with(|| std::fs::read_to_string(root.join(file)).unwrap_or_default());
        let near = lowest
            .get(&(file.to_string(), name.to_string()))
            .copied()
            .unwrap_or(0);
        let line = java_declaration(text, name, near).unwrap_or(near);
        (file.to_string(), name.to_string(), line)
    };
    let samples: Vec<(Vec<Option<Key>>, f64)> = raw
        .iter()
        .map(|stack| {
            let keys = stack
                .iter()
                .map(|f| f.as_ref().map(|(file, name, _)| key(file, name)))
                .collect();
            (keys, SAMPLE_MS)
        })
        .collect();
    let total = samples.len() as f64 * SAMPLE_MS;
    if samples.is_empty() {
        bail!("no samples were recorded: run the workload longer (Java is sampled every 10 ms)");
    }
    let mut functions = HashMap::new();
    merge_samples(&samples, &mut functions);
    Ok((functions, total))
}

// ---- Ruby: a call tracer --------------------------------------------------------------------

/// Loaded with `RUBYOPT=-r`: times every Ruby method call on the main thread (inclusive, self,
/// callers) and writes cProfile-like entries at exit. Tracing slows the program; the shares stay
/// comparable.
const RUBY_TRACER: &str = r#"
require "json"

module RustAnythingProfile
  STATS = Hash.new { |h, k| h[k] = { calls: 0, self: 0.0, total: 0.0, callers: Hash.new { |c, ck| c[ck] = [0, 0.0] } } }
  STACK = []
  ACTIVE = Hash.new(0)
  MAIN = Thread.current
  TRACE = TracePoint.new(:call, :return) do |tp|
    next unless Thread.current == MAIN
    now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    if tp.event == :call
      path = tp.path
      path = File.expand_path(path) unless path.start_with?("<")
      key = [path, tp.lineno, tp.method_id.to_s]
      ACTIVE[key] += 1
      STACK << [key, now, 0.0]
    elsif (frame = STACK.pop)
      key, start, child = frame
      elapsed = now - start
      ACTIVE[key] -= 1
      outer = ACTIVE[key].zero?
      s = STATS[key]
      s[:calls] += 1
      s[:self] += elapsed - child
      s[:total] += elapsed if outer
      if (parent = STACK.last)
        parent[2] += elapsed
        c = s[:callers][parent[0]]
        c[0] += 1
        c[1] += elapsed if outer
      end
    end
  end
  TRACE.enable

  at_exit do
    TRACE.disable
    out = STATS.map do |(file, line, name), s|
      { file: file, line: line, name: name, calls: s[:calls], self: s[:self] * 1000, total: s[:total] * 1000,
        callers: s[:callers].map { |(f, l, n), (calls, total)| { file: f, line: l, name: n, calls: calls, total: total * 1000 } } }
    end
    File.write(ENV.fetch("RUST_ANYTHING_PROFILE_OUT"), JSON.generate(out))
  end
end
"#;

/// Ruby: the tracer above, loaded into any Ruby command through `RUBYOPT`.
fn ruby(root: &Path, command: &[String], work: &Path) -> Result<(HashMap<Key, Acc>, f64)> {
    let tracer = work.join("rust_anything_profile.rb");
    std::fs::write(&tracer, RUBY_TRACER)?;
    let out = work.join("profile.json");
    let mut rubyopt = std::env::var("RUBYOPT").unwrap_or_default();
    rubyopt.push_str(&format!(" -r{}", tracer.display()));
    let start = std::time::Instant::now();
    let status = crate::process::status(
        Command::new(&command[0])
            .stdout(std::io::stderr())
            .args(&command[1..])
            .current_dir(root)
            .env("RUBYOPT", rubyopt.trim())
            .env("RUST_ANYTHING_PROFILE_OUT", &out),
        crate::process::TESTS,
    )
    .with_context(|| format!("running {}", command[0]))?;
    let total_ms = start.elapsed().as_secs_f64() * 1000.0;
    if !status.success() {
        bail!("the workload failed ({status})");
    }
    let entries: Vec<Value> = serde_json::from_str(
        &std::fs::read_to_string(&out)
            .context("the Ruby tracer wrote nothing: is the workload a Ruby process?")?,
    )?;
    Ok((ingest_entries(root, &entries).0, total_ms))
}

// ---- the report -----------------------------------------------------------------------------

/// The argument a caller passes per loop item: an arrow or for-of variable among `f`'s call
/// arguments in the caller's source. `None` when the source does not show one.
fn batch_param(root: &Path, caller: &Key, callee: &str, params: &[String]) -> Option<String> {
    let text = std::fs::read_to_string(root.join(&caller.0)).ok()?;
    let lines: Vec<&str> = text.lines().collect();
    let start = (caller.2 as usize).saturating_sub(1);
    let body = lines.get(start..(start + 80).min(lines.len()))?.join("\n");
    let mut loop_vars: Vec<String> = Vec::new();
    for (i, _) in body.match_indices("=>") {
        let before = body[..i].trim_end().trim_end_matches(')');
        let name: String = before
            .chars()
            .rev()
            .take_while(|c| c.is_alphanumeric() || *c == '_' || *c == '$')
            .collect::<String>()
            .chars()
            .rev()
            .collect();
        if !name.is_empty() {
            loop_vars.push(name);
        }
    }
    for word in ["const ", "let ", "var ", "for "] {
        for (i, _) in body.match_indices(word) {
            let rest = &body[i + word.len()..];
            let name: String = rest
                .chars()
                .take_while(|c| c.is_alphanumeric() || *c == '_')
                .collect();
            if rest[name.len()..].trim_start().starts_with("of ")
                || rest[name.len()..].trim_start().starts_with("in ")
            {
                loop_vars.push(name);
            }
        }
    }
    let (more, index_vars) = loop_variables(&body);
    loop_vars.extend(more);
    let call = body.find(&format!("{callee}("))?;
    let args_text = &body[call + callee.len() + 1..];
    let mut depth = 0;
    let mut args = vec![String::new()];
    for c in args_text.chars() {
        match c {
            '(' | '[' | '{' => depth += 1,
            ')' | ']' | '}' if depth == 0 => break,
            ')' | ']' | '}' => depth -= 1,
            ',' if depth == 0 => {
                args.push(String::new());
                continue;
            }
            _ => {}
        }
        args.last_mut()?.push(c);
    }
    // The item itself (`t`), or the item at the loop index (`texts[i]`).
    let per_item = |a: &str| {
        let a = a.trim();
        loop_vars.iter().any(|v| v == a)
            || a.strip_suffix(']')
                .and_then(|a| a.split_once('['))
                .is_some_and(|(list, i)| {
                    !list.is_empty() && index_vars.iter().any(|v| v == i.trim())
                })
    };
    // In nested loops the innermost one runs over the items: its variable is declared last.
    let declared_at = |a: &str| {
        let a = a.trim();
        let var = a
            .strip_suffix(']')
            .and_then(|a| a.split_once('['))
            .map_or(a, |(_, i)| i.trim());
        regex::Regex::new(&format!(r"\b{}\b", regex::escape(var)))
            .ok()
            .and_then(|re| re.find(&body).map(|m| m.start()))
            .unwrap_or(0)
    };
    let position = args
        .iter()
        .enumerate()
        .filter(|(_, a)| per_item(a))
        .max_by_key(|(_, a)| declared_at(a))?
        .0;
    params.get(position).cloned()
}

/// Loop variables in Go, Java and Ruby source: `(items, indexes)`. Items: `for _, t := range`,
/// `for (String t : texts)`, `|t|`; indexes: `for i := range`, `for (int i = 0;`.
fn loop_variables(body: &str) -> (Vec<String>, Vec<String>) {
    let ident = |s: &str| -> String {
        s.chars()
            .take_while(|c| c.is_alphanumeric() || *c == '_')
            .collect()
    };
    let last_ident = |s: &str| -> String {
        let s = s.trim_end();
        let rev: String = s
            .chars()
            .rev()
            .take_while(|c| c.is_alphanumeric() || *c == '_')
            .collect();
        rev.chars().rev().collect()
    };
    let (mut items, mut indexes) = (Vec::new(), Vec::new());
    for (i, _) in body.match_indices("for") {
        if body[..i]
            .chars()
            .last()
            .is_some_and(|c| c.is_alphanumeric() || c == '_')
        {
            continue;
        }
        let rest = &body[i + 3..];
        let line = rest.split(['{', '\n']).next().unwrap_or(rest);
        if let Some((vars, _)) = line.split_once(":= range") {
            // Go: `for i, t := range xs` (t is the item), `for i := range xs` (an index).
            let vars: Vec<String> = vars.split(',').map(|v| v.trim().to_string()).collect();
            match vars.as_slice() {
                [index, item] => {
                    items.push(item.clone());
                    indexes.push(index.clone());
                }
                [index] => indexes.push(index.clone()),
                _ => {}
            }
        } else if let Some(inner) = line.trim_start().strip_prefix('(') {
            // Java: `for (String t : texts)` or `for (int i = 0; ...)`.
            if let Some((decl, _)) = inner.split_once(" : ").filter(|(d, _)| !d.contains(';')) {
                items.push(last_ident(decl));
            } else if let Some((decl, _)) = inner.split_once('=') {
                indexes.push(last_ident(decl));
            }
        }
    }
    // Ruby blocks: `{ |t| ... }`, `do |t, i|`.
    for (i, _) in body.match_indices('|') {
        let before = body[..i].trim_end();
        if before.ends_with('{') || before.ends_with("do") {
            let first = ident(body[i + 1..].trim_start());
            if !first.is_empty() {
                items.push(first);
            }
        }
    }
    items.retain(|v| !v.is_empty() && v != "_");
    indexes.retain(|v| !v.is_empty() && v != "_");
    (items, indexes)
}

fn report(
    root: &Path,
    lang: Lang,
    command: &[String],
    functions: HashMap<Key, Acc>,
    unowned_ms: f64,
    total_ms: f64,
) -> Report {
    let analysis = crate::analyze::analyze_lang(root, lang).ok();
    let unit_of = |file: &str| -> Option<(String, bool)> {
        let a = analysis.as_ref()?;
        let u = a.units.iter().find(|u| u.files.iter().any(|f| f == file))?;
        let portable = !matches!(
            u.status,
            crate::analyze::Status::Keep | crate::analyze::Status::Blocked
        );
        Some((u.id.clone(), portable))
    };
    // Each hot file's exported functions, read and judged once, as `bridge` reads and judges
    // them.
    use_project_types(root, lang);
    let mut judged: HashMap<String, Vec<(crate::ts::exports::ExportedFn, Option<String>)>> =
        HashMap::new();
    for ((file, _, _), a) in &functions {
        if in_repo(file)
            && total_ms > 0.0
            && a.total_ms / total_ms >= 0.01
            && !judged.contains_key(file)
        {
            let exports = std::fs::read_to_string(root.join(file))
                .map(|text| exports_judged(root, lang, file, &text))
                .unwrap_or_default();
            judged.insert(file.clone(), exports);
        }
    }
    let exports_of = |file: &str,
                      name: &str,
                      line: u32|
     -> Option<(crate::ts::exports::ExportedFn, Option<String>)> {
        let exports = judged.get(file)?;
        exports
            .iter()
            .find(|(f, _)| f.name == name && f.line == line)
            .or_else(|| exports.iter().find(|(f, _)| f.name == name))
            .cloned()
    };
    let repo_self: f64 = functions
        .iter()
        .filter(|(k, _)| in_repo(&k.0))
        .map(|(_, a)| a.self_ms)
        .sum();
    let mut calls_in: HashMap<&str, Vec<SpawnCall>> = HashMap::new();
    let mut child_processes: Vec<ChildProcess> = functions
        .iter()
        .filter(|(_, a)| total_ms > 0.0 && a.spawn_ms / total_ms >= 0.01)
        .map(|((file, name, line), a)| {
            let calls = calls_in
                .entry(file.as_str())
                .or_insert_with(|| spawn_calls_in(root, lang, file));
            let (line, command) = spawn_site(calls, name, *line).unwrap_or((*line, None));
            ChildProcess {
                command,
                file: file.clone(),
                line,
                function: name.clone(),
                ms: a.spawn_ms,
                share: 100.0 * a.spawn_ms / total_ms,
            }
        })
        .collect();
    child_processes.sort_by(|a, b| b.ms.total_cmp(&a.ms));
    // Every wait counts once, at the frame that starts it, so the parts add up to the whole.
    let child_pct = if total_ms > 0.0 {
        (100.0 * (unowned_ms + functions.values().map(|a| a.spawn_ms).sum::<f64>()) / total_ms)
            .min(100.0)
    } else {
        0.0
    };
    let child_pct = if child_pct >= 1.0 { child_pct } else { 0.0 };
    let mut hot: Vec<Hot> = functions
        .into_iter()
        .filter(|((file, _, _), a)| in_repo(file) && total_ms > 0.0 && a.total_ms / total_ms >= 0.01)
        .map(|((file, name, line), a)| {
            let unit = unit_of(&file);
            let (export, js_only) = match exports_of(&file, &name, line) {
                Some((f, reason)) => (Some(f), reason),
                None => (None, None),
            };
            let mut callers: Vec<Caller> = a
                .callers
                .iter()
                .map(|((f, n, l), (ms, calls))| Caller { name: n.clone(), file: f.clone(), line: *l, ms: *ms, calls: *calls })
                .collect();
            callers.sort_by(|x, y| y.ms.total_cmp(&x.ms));
            callers.truncate(5);
            let ns_per_call = a.calls.filter(|c| *c > 0).map(|c| a.total_ms * 1e6 / c as f64);
            let total_pct = 100.0 * a.total_ms / total_ms;
            let top = callers.first();
            let small_per_call = ns_per_call.is_some_and(|ns| ns < 20_000.0) || (a.calls.is_none() && top.is_some_and(|c| c.ms >= 0.5 * a.total_ms));
            let params: Vec<String> = export.as_ref().map(|e| e.params.iter().map(|p| p.name.clone()).collect()).unwrap_or_default();
            let child_pct = (a.total_ms > 0.0 && a.waits_ms / a.total_ms >= 0.01)
                .then(|| (100.0 * a.waits_ms / a.total_ms).min(100.0));
            let mut batch_spec = None;
            let advice = match (&unit, &export) {
                _ if child_pct.is_some_and(|p| p >= 50.0) => format!(
                    "Hot, but {:.0}% of its time waits on child processes. {CHILD_ADVICE}",
                    child_pct.unwrap_or_default()
                ),
                (Some((_, true)), Some(_)) if js_only.is_some() => format!(
                    "Hot, but `bridge` keeps `{name}` in {}: {}.",
                    lang.name(),
                    js_only.as_deref().unwrap_or_default()
                ),
                (Some((u, true)), Some(_)) if small_per_call && top.is_some() => {
                    let c = top.expect("checked");
                    let batch = batch_param(root, &(c.file.clone(), c.name.clone(), c.line), &name, &params);
                    batch_spec = batch.as_ref().map(|p| format!("{name}:{p}"));
                    let calls = a.calls.map_or_else(String::new, |n| format!(", {n} calls"));
                    match batch {
                        Some(param) => format!(
                            "Called in a loop by `{}` ({}:{}{calls}): a call per item would cost more than the work. \
                             Batch it, keeping the items in Rust and using all cores: \
                             `rust-anything bridge {} {u} --batch {name}:{param}`, then prepare the items once in a \
                             `{}Index` in port.rs. Better still, port `{}` with it.",
                            c.name, c.file, c.line, root.display(), crate::codegen_batch::pascal(&name), c.name
                        ),
                        None => format!(
                            "Mostly called by `{}` ({}:{}{calls}): port `{}` together with `{name}`, so one call into Rust does the whole loop.",
                            c.name, c.file, c.line, c.name
                        ),
                    }
                }
                (Some((u, true)), Some(_)) => format!(
                    "Hot and exported by `{u}`: port it (`rust-anything bridge {} {u}`), then check `rust-anything bench`.",
                    root.display()
                ),
                (Some((u, true)), None) => format!("Hot inside `{u}`: port the exported function that calls it, with it."),
                (Some((u, false)), _) => format!("Hot, but `{u}` must stay in the source language (see `rust-anything analyze`)."),
                (None, _) => "Hot, outside the analyzed units.".into(),
            };
            Hot {
                portable: unit.as_ref().is_some_and(|(_, p)| *p),
                js_only,
                params,
                batch: batch_spec,
                child_pct,
                unit: unit.map(|(u, _)| u),
                exported: export.is_some(),
                name,
                file,
                line,
                self_ms: a.self_ms,
                total_ms: a.total_ms,
                total_pct,
                calls: a.calls,
                ns_per_call,
                callers,
                advice,
            }
        })
        .collect();
    hot.sort_by(|a, b| b.total_ms.total_cmp(&a.total_ms));
    let compiled = compiled_in(root, lang);
    for h in &mut hot {
        if let Some(advice) = compiled_advice(root, h, &compiled) {
            h.advice = advice;
        }
    }
    Report {
        schema: SCHEMA,
        language: lang,
        command: command.to_vec(),
        total_ms,
        repo_pct: if total_ms > 0.0 {
            (100.0 * repo_self / total_ms).min(100.0)
        } else {
            0.0
        },
        compiled,
        hot,
        child_pct,
        child_processes,
    }
}

/// Where the function `name` defined at `line` starts its child process: the call's line, and
/// the program when the call names it literally. A call in a callback counts for the function
/// around it, as the wait does. `None` when there is no such call, or several that start
/// different programs.
fn spawn_site(calls: &[SpawnCall], name: &str, line: u32) -> Option<(u32, Option<String>)> {
    // A file's top level: Python's `<module>`, Node's module function on line 1.
    let top = name == "<module>" || (line == 1 && name.starts_with("(anonymous)"));
    let own: Vec<&SpawnCall> = calls
        .iter()
        .filter(|c| {
            c.scopes.last() == Some(&line) || c.owner == Some(line) || (top && c.scopes.is_empty())
        })
        .collect();
    let sites = if own.is_empty() {
        calls.iter().filter(|c| c.scopes.contains(&line)).collect()
    } else {
        own
    };
    let first = sites.first()?;
    let same = sites
        .iter()
        .all(|s| s.program.is_some() && s.program == first.program);
    (sites.len() == 1 || same).then(|| (first.line, first.program.clone()))
}

/// The calls in a repository file that start a child process and wait for it.
fn spawn_calls_in(root: &Path, lang: Lang, file: &str) -> Vec<SpawnCall> {
    let inside = Path::new(file)
        .components()
        .all(|c| matches!(c, std::path::Component::Normal(_)));
    if !inside {
        return Vec::new(); // `/abs/bench.py`, `../bench.py`, `C:\\...`: not the repository's
    }
    let Ok(text) = std::fs::read_to_string(root.join(file)) else {
        return Vec::new();
    };
    match lang {
        Lang::Ts => js_spawn_calls(file, &text),
        Lang::Python => python_spawn_calls(&text),
        _ => Vec::new(),
    }
}

/// A call that starts a child process and waits for it.
#[derive(Debug, Clone, PartialEq)]
struct SpawnCall {
    /// First lines of the functions around the call, innermost last; empty at the top level.
    scopes: Vec<u32>,
    /// The function the profile charges the wait to: in JS the innermost named one (a callback
    /// passed as an argument has no name), else the innermost.
    owner: Option<u32>,
    line: u32,
    /// The program, when the call names it literally.
    program: Option<String>,
}

/// The program in a command line: `sleep` in `sleep 0.2`. `interpolated` when the text goes on
/// with a computed part (`ls ${dir}` gives `ls`, `${bin} run` and `git${x}` give nothing).
/// Leading `NAME=value` words are the child's environment, not the program, and are never shown.
fn first_word(command: &str, interpolated: bool) -> Option<String> {
    let mut rest = command.trim_start();
    loop {
        let word = rest.split_whitespace().next()?;
        let assignment = word.split_once('=').is_some_and(|(name, _)| {
            !name.is_empty() && name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
        });
        let after = rest[word.len()..].trim_start();
        if assignment {
            if word.contains(['"', '\'']) {
                return None; // `TOKEN='a b' deploy`: where the value ends is the shell's business
            }
            rest = after;
            continue;
        }
        // The word must end before any computed part, and be safe in a markdown table.
        let complete = !interpolated || rest.len() > word.len();
        let plain = !word.contains(['|', '`', '$', '{']) && !word.chars().any(char::is_control);
        return (complete && plain).then(|| word.to_string());
    }
}

/// The executable a call names on its own (`execFileSync(file, args)`, an argv list): the whole
/// text, spaces included, when it is safe in a markdown table.
fn whole_program(path: &str) -> Option<String> {
    (!path.trim().is_empty() && !path.contains(['|', '`']) && !path.chars().any(char::is_control))
        .then(|| path.to_string())
}

/// Node's blocking `child_process` calls in a JS/TS file, read with oxc.
fn js_spawn_calls(path: &str, source: &str) -> Vec<SpawnCall> {
    use oxc_ast::ast::{Argument, ArrowFunctionExpression, CallExpression, Expression, Function};
    use oxc_ast_visit::{Visit, walk};
    use oxc_syntax::scope::ScopeFlags;

    struct Calls {
        /// Byte offset of every newline, to turn offsets into lines without rescanning.
        newlines: Vec<usize>,
        /// First line of each enclosing function, and whether V8 gives it a name.
        scopes: Vec<(u32, bool)>,
        /// Where the functions passed straight to a call start: V8 shows them as anonymous.
        callbacks: HashSet<u32>,
        out: Vec<SpawnCall>,
    }
    impl Calls {
        fn line(&self, offset: u32) -> u32 {
            let before = self.newlines.partition_point(|&n| n < offset as usize);
            u32::try_from(before + 1).unwrap_or(u32::MAX)
        }
    }
    impl<'a> Visit<'a> for Calls {
        fn visit_function(&mut self, it: &Function<'a>, flags: ScopeFlags) {
            let named = it.id.is_some() || !self.callbacks.contains(&it.span.start);
            self.scopes.push((self.line(it.span.start), named));
            walk::walk_function(self, it, flags);
            self.scopes.pop();
        }
        fn visit_arrow_function_expression(&mut self, it: &ArrowFunctionExpression<'a>) {
            let named = !self.callbacks.contains(&it.span.start);
            self.scopes.push((self.line(it.span.start), named));
            walk::walk_arrow_function_expression(self, it);
            self.scopes.pop();
        }
        fn visit_call_expression(&mut self, it: &CallExpression<'a>) {
            let callee = match &it.callee {
                Expression::Identifier(id) => Some(id.name.as_str()),
                Expression::StaticMemberExpression(m) => Some(m.property.name.as_str()),
                _ => None,
            };
            for a in &it.arguments {
                match a {
                    Argument::ArrowFunctionExpression(f) => {
                        self.callbacks.insert(f.span.start);
                    }
                    Argument::FunctionExpression(f) if f.id.is_none() => {
                        self.callbacks.insert(f.span.start);
                    }
                    _ => {}
                }
            }
            if let Some(callee) = callee.filter(|n| NODE_SPAWNS.contains(n)) {
                // `execSync` takes a shell command line; the others an executable and its args.
                let shell = callee == "execSync";
                let program = match it.arguments.first() {
                    Some(Argument::StringLiteral(s)) => {
                        let text = s.value.display().to_string();
                        if shell {
                            first_word(&text, false)
                        } else {
                            whole_program(&text)
                        }
                    }
                    Some(Argument::TemplateLiteral(t)) => t.quasis.first().and_then(|q| {
                        let text = q
                            .value
                            .cooked
                            .as_ref()
                            .map_or_else(|| q.value.raw.to_string(), |c| c.display().to_string());
                        let interpolated = !t.expressions.is_empty();
                        if shell {
                            first_word(&text, interpolated)
                        } else if interpolated {
                            None
                        } else {
                            whole_program(&text)
                        }
                    }),
                    _ => None,
                };
                let owner = self
                    .scopes
                    .iter()
                    .rev()
                    .find(|(_, named)| *named)
                    .or(self.scopes.last())
                    .map(|(l, _)| *l);
                self.out.push(SpawnCall {
                    scopes: self.scopes.iter().map(|(l, _)| *l).collect(),
                    owner,
                    line: self.line(it.span.start),
                    program,
                });
            }
            walk::walk_call_expression(self, it);
        }
    }
    let source_type =
        oxc_span::SourceType::from_path(path).unwrap_or_else(|_| oxc_span::SourceType::ts());
    let allocator = oxc_allocator::Allocator::default();
    let ret = oxc_parser::Parser::new(&allocator, source, source_type).parse();
    let mut calls = Calls {
        newlines: source.match_indices('\n').map(|(i, _)| i).collect(),
        scopes: Vec::new(),
        callbacks: HashSet::new(),
        out: Vec::new(),
    };
    calls.visit_program(&ret.program);
    calls.out
}

/// Python's `subprocess` and `os.system` calls in a file, read with tree-sitter: through the
/// module under any name (`import subprocess as sp`), or a function imported from it.
fn python_spawn_calls(source: &str) -> Vec<SpawnCall> {
    use crate::lang::syntax;
    const SUBPROCESS: [&str; 7] = [
        "run",
        "call",
        "check_call",
        "check_output",
        "getoutput",
        "getstatusoutput",
        "Popen",
    ];
    let spawns = |module: &str, name: &str| {
        (module == "subprocess" && SUBPROCESS.contains(&name))
            || (module == "os" && name == "system")
    };
    let Some(tree) = syntax::parse(&tree_sitter_python::LANGUAGE.into(), source) else {
        return Vec::new();
    };
    let root = tree.root_node();
    let field = |n: tree_sitter::Node, f: &str| {
        n.child_by_field_name(f)
            .map_or("", |c| syntax::text(c, source))
    };
    let name_and_alias = |c: tree_sitter::Node| match c.kind() {
        "aliased_import" => (field(c, "name"), field(c, "alias")),
        _ => (syntax::text(c, source), syntax::text(c, source)),
    };
    let mut modules: HashMap<&str, &str> = HashMap::new();
    let mut imported: HashSet<&str> = HashSet::new();
    syntax::walk(root, |n| match n.kind() {
        "import_statement" => {
            for c in syntax::named_children(n) {
                let (module, alias) = name_and_alias(c);
                if matches!(module, "subprocess" | "os") {
                    modules.insert(alias, module);
                }
            }
        }
        "import_from_statement" => {
            let module = field(n, "module_name");
            let mut cursor = n.walk();
            for c in n.children_by_field_name("name", &mut cursor) {
                let (name, alias) = name_and_alias(c);
                if spawns(module, name) {
                    imported.insert(alias);
                }
            }
        }
        _ => {}
    });
    let mut out = Vec::new();
    syntax::walk(root, |n| {
        if n.kind() != "call" {
            return;
        }
        let Some(callee) = n.child_by_field_name("function") else {
            return;
        };
        let starts = match callee.kind() {
            "identifier" => imported.contains(syntax::text(callee, source)),
            "attribute" => modules
                .get(field(callee, "object"))
                .is_some_and(|m| spawns(m, field(callee, "attribute"))),
            _ => false,
        };
        if !starts {
            return;
        }
        // The functions around the call, by the line cProfile gives them: a decorated function
        // starts at its first decorator.
        let mut scopes = Vec::new();
        let mut up = n.parent();
        while let Some(p) = up {
            if matches!(
                p.kind(),
                "function_definition" | "class_definition" | "lambda"
            ) {
                let start = p
                    .parent()
                    .filter(|d| d.kind() == "decorated_definition")
                    .unwrap_or(p);
                scopes.push(syntax::line(start));
            }
            up = p.parent();
        }
        scopes.reverse();
        let program = n
            .child_by_field_name("arguments")
            .and_then(|args| {
                let all = syntax::named_children(args);
                all.iter()
                    .find(|a| a.kind() == "keyword_argument" && field(**a, "name") == "args")
                    .and_then(|a| a.child_by_field_name("value"))
                    .or_else(|| {
                        all.first()
                            .copied()
                            .filter(|a| a.kind() != "keyword_argument")
                    })
            })
            .and_then(|a| python_program(a, source));
        out.push(SpawnCall {
            owner: scopes.last().copied(),
            scopes,
            line: syntax::line(n),
            program,
        });
    });
    out
}

/// The program a Python argument names: `"sleep 0.2"`, `["git", "log"]`, `("make",)`.
fn python_program(arg: tree_sitter::Node, source: &str) -> Option<String> {
    use crate::lang::syntax;
    let string = |s: tree_sitter::Node| {
        let parts = syntax::named_children(s);
        let content: String = parts
            .iter()
            .take_while(|p| p.kind() != "interpolation")
            .filter(|p| p.kind() == "string_content")
            .map(|p| syntax::text(*p, source))
            .collect();
        (content, parts.iter().any(|p| p.kind() == "interpolation"))
    };
    match arg.kind() {
        // An argv list: its first item is the executable, spaces and all.
        "list" | "tuple" => {
            let first = *syntax::named_children(arg).first()?;
            let (content, interpolated) = (first.kind() == "string").then(|| string(first))?;
            if interpolated {
                None
            } else {
                whole_program(&content)
            }
        }
        "string" => {
            let (content, interpolated) = string(arg);
            first_word(&content, interpolated)
        }
        _ => None,
    }
}

pub fn markdown(r: &Report) -> String {
    use std::fmt::Write as _;
    let mut out = format!(
        "# Profile: `{}`\n\n{:.0} ms in all; {:.0}% of it in the repository's own code.\n\n\
         | Function | File | Total | Self | Calls | Unit |\n| --- | --- | ---: | ---: | ---: | --- |\n",
        r.command.join(" "),
        r.total_ms,
        r.repo_pct
    );
    for h in r.hot.iter().take(SHOWN) {
        let _ = writeln!(
            out,
            "| `{}` | `{}:{}` | {:.0}% ({:.0} ms) | {:.0} ms | {} | {} |",
            h.name,
            h.file,
            h.line,
            h.total_pct,
            h.total_ms,
            h.self_ms,
            h.calls.map_or_else(|| "-".into(), |c| c.to_string()),
            h.unit
                .as_deref()
                .map_or_else(|| "-".into(), |u| format!("`{u}`"))
        );
    }
    if let Some(summary) = r.child_summary() {
        let _ = write!(out, "\n## Child processes\n\n{summary} {CHILD_ADVICE}\n");
        if !r.child_processes.is_empty() {
            out.push_str("\n| Command | Called from | Time |\n| --- | --- | ---: |\n");
        }
        for c in &r.child_processes {
            let _ = writeln!(
                out,
                "| {} | `{}` (`{}:{}`) | {:.0}% ({:.0} ms) |",
                c.command
                    .as_deref()
                    .map_or_else(|| "-".into(), |c| format!("`{c}`")),
                c.function,
                c.file,
                c.line,
                c.share,
                c.ms
            );
        }
    }
    if !r.compiled.is_empty() {
        out.push_str(
            "\n## Already compiled\n\nThese run as machine code already; Rust is unlikely to make them \
             much faster:\n\n",
        );
        for c in &r.compiled {
            let _ = writeln!(out, "- `{}` (`{}:{}`): {}", c.name, c.file, c.line, c.by);
        }
    }
    out.push_str("\n## What to port\n\n");
    let mut seen = BTreeMap::new();
    for h in r.hot.iter().take(SHOWN).filter(|h| h.unit.is_some()) {
        if seen.insert(h.name.clone(), ()).is_some() {
            continue;
        }
        let _ = writeln!(
            out,
            "- **`{}`** ({:.0}%): {}",
            h.name, h.total_pct, h.advice
        );
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn compiled_code_changes_the_advice() {
        let root = std::env::temp_dir().join(format!("rust-anything-numba-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&root);
        std::fs::create_dir_all(root.join("sim")).unwrap();
        std::fs::write(root.join("pyproject.toml"), "[project]\nname = \"sim\"\n").unwrap();
        std::fs::write(
            root.join("sim/physics.py"),
            "from numba import njit\n\n@njit(cache=True)\ndef step(x, dt):\n    return x + dt\n\n\
             def run(xs):\n    out = []\n    for x in xs:\n        out.append(step(x, 0.1))\n    return out\n\n\
             def other(xs):\n    return [x * 2 for x in xs]\n",
        )
        .unwrap();
        let compiled = compiled_in(&root, Lang::Python);
        assert_eq!(compiled.len(), 1);
        assert_eq!(
            (compiled[0].name.as_str(), compiled[0].line, compiled[0].by),
            ("step", 4, "numba")
        );
        let hot = |name: &str, line: u32| Hot {
            name: name.into(),
            file: "sim/physics.py".into(),
            line,
            unit: None,
            exported: true,
            self_ms: 1.0,
            total_ms: 1.0,
            total_pct: 50.0,
            calls: None,
            ns_per_call: None,
            callers: Vec::new(),
            advice: "Port it.".into(),
            portable: true,
            js_only: None,
            params: vec!["x".into()],
            batch: None,
            child_pct: None,
        };
        let own = compiled_advice(&root, &hot("step", 3), &compiled).unwrap();
        assert!(
            own.starts_with("Already compiled by numba (`sim/physics.py:4`)"),
            "{own}"
        );
        let caller = compiled_advice(&root, &hot("run", 7), &compiled).unwrap();
        assert!(
            caller.starts_with("Port it. It calls `step` compiled by numba"),
            "{caller}"
        );
        assert!(compiled_advice(&root, &hot("other", 13), &compiled).is_none());
        assert!(compiled_in(&root, Lang::Ts).is_empty());
        std::fs::remove_dir_all(&root).unwrap();
    }

    #[test]
    fn functions_bridge_keeps_in_js_are_not_offered_for_porting() {
        let root =
            std::env::temp_dir().join(format!("rust-anything-js-only-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&root);
        std::fs::create_dir_all(&root).unwrap();
        std::fs::write(
            root.join("package.json"),
            r#"{"name":"demo","type":"module"}"#,
        )
        .unwrap();
        // A typed callback in TypeScript, and the same shape in JS typed by a sibling `.d.ts`.
        std::fs::write(
            root.join("lib.ts"),
            "export function sumBy<T>(items: T[], pick: (item: T) => number): number {\n  \
             let s = 0;\n  for (const it of items) s += pick(it);\n  return s;\n}\n\
             export function double(n: number): number { return n * 2; }\n",
        )
        .unwrap();
        std::fs::write(
            root.join("plug.js"),
            "export function each(items, visit) { for (const it of items) visit(it); }\n\
             export function half(n) { return n / 2; }\n",
        )
        .unwrap();
        std::fs::write(
            root.join("plug.d.ts"),
            "export declare function each<T>(items: T[], visit: (item: T) => void): void;\n\
             export declare function half(n: number): number;\n",
        )
        .unwrap();
        let acc = |total_ms: f64| Acc {
            self_ms: total_ms,
            total_ms,
            ..Default::default()
        };
        let functions: HashMap<Key, Acc> = [
            (("lib.ts".to_string(), "sumBy".to_string(), 1), acc(60.0)),
            (("plug.js".to_string(), "each".to_string(), 1), acc(20.0)),
            (("plug.js".to_string(), "half".to_string(), 2), acc(10.0)),
            (("lib.ts".to_string(), "double".to_string(), 6), acc(5.0)),
        ]
        .into_iter()
        .collect();
        let r = report(
            &root,
            Lang::Ts,
            &["node".into(), "bench.mjs".into()],
            functions,
            0.0,
            100.0,
        );
        std::fs::remove_dir_all(&root).unwrap();

        let hot = |name: &str| r.hot.iter().find(|h| h.name == name).unwrap();
        for name in ["sumBy", "each"] {
            let h = hot(name);
            assert_eq!(
                h.js_only.as_deref(),
                Some("takes or returns a function"),
                "{name}"
            );
            assert!(
                h.advice.starts_with(&format!("Hot, but `bridge` keeps `{name}` in TypeScript/JavaScript: takes or returns a function.")),
                "{}",
                h.advice
            );
        }
        for name in ["half", "double"] {
            let h = hot(name);
            assert_eq!(h.js_only, None, "{name}");
            assert!(h.advice.starts_with("Hot and exported by"), "{}", h.advice);
        }
        // `go` passes over the hottest functions, which `bridge` would keep in JS.
        assert_eq!(crate::go::pick(&r, None).unwrap().function, "half");
        let named = crate::go::pick(&r, Some("sumBy")).unwrap_err().to_string();
        assert!(named.contains("takes or returns a function"), "{named}");
    }

    #[test]
    fn a_python_generator_is_not_offered_for_porting() {
        let root =
            std::env::temp_dir().join(format!("rust-anything-py-js-only-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&root);
        std::fs::create_dir_all(root.join("demo")).unwrap();
        std::fs::write(
            root.join("pyproject.toml"),
            "[project]\nname = \"demo\"\nversion = \"0.1.0\"\n",
        )
        .unwrap();
        std::fs::write(root.join("demo/__init__.py"), "").unwrap();
        std::fs::write(
            root.join("demo/mod.py"),
            "def squares(xs):\n    for x in xs:\n        yield x * x\n\n\n\
             def collatz(n):\n    steps = 0\n    while n != 1:\n        n = n // 2 if n % 2 == 0 else 3 * n + 1\n        steps += 1\n    return steps\n",
        )
        .unwrap();
        let acc = |total_ms: f64| Acc {
            self_ms: total_ms,
            total_ms,
            ..Default::default()
        };
        let functions: HashMap<Key, Acc> = [
            (
                ("demo/mod.py".to_string(), "squares".to_string(), 1),
                acc(60.0),
            ),
            (
                ("demo/mod.py".to_string(), "collatz".to_string(), 6),
                acc(30.0),
            ),
        ]
        .into_iter()
        .collect();
        let r = report(
            &root,
            Lang::Python,
            &["python3".into(), "bench.py".into()],
            functions,
            0.0,
            100.0,
        );
        std::fs::remove_dir_all(&root).unwrap();

        let squares = r.hot.iter().find(|h| h.name == "squares").unwrap();
        assert!(
            squares
                .js_only
                .as_deref()
                .is_some_and(|r| r.starts_with("generator")),
            "{:?}",
            squares.js_only
        );
        assert!(
            squares
                .advice
                .starts_with("Hot, but `bridge` keeps `squares` in Python: generator"),
            "{}",
            squares.advice
        );
        assert_eq!(crate::go::pick(&r, None).unwrap().function, "collatz");
    }

    #[test]
    fn a_redefined_python_function_is_judged_by_the_definition_that_ran() {
        let root =
            std::env::temp_dir().join(format!("rust-anything-py-redef-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&root);
        std::fs::create_dir_all(root.join("demo")).unwrap();
        std::fs::write(
            root.join("pyproject.toml"),
            "[project]\nname = \"demo\"\nversion = \"0.1.0\"\n",
        )
        .unwrap();
        std::fs::write(root.join("demo/__init__.py"), "").unwrap();
        std::fs::write(
            root.join("demo/mod.py"),
            "def calc(xs):\n    for x in xs:\n        yield x * x\n\n\n\
             def calc(n):\n    steps = 0\n    while n != 1:\n        n = n // 2 if n % 2 == 0 else 3 * n + 1\n        steps += 1\n    return steps\n",
        )
        .unwrap();
        let functions: HashMap<Key, Acc> = [(
            ("demo/mod.py".to_string(), "calc".to_string(), 6),
            Acc {
                self_ms: 90.0,
                total_ms: 90.0,
                ..Default::default()
            },
        )]
        .into_iter()
        .collect();
        let r = report(
            &root,
            Lang::Python,
            &["python3".into(), "bench.py".into()],
            functions,
            0.0,
            100.0,
        );
        std::fs::remove_dir_all(&root).unwrap();

        let calc = r.hot.iter().find(|h| h.name == "calc").unwrap();
        assert_eq!(calc.js_only, None, "{}", calc.advice);
        assert_eq!(crate::go::pick(&r, None).unwrap().function, "calc");
    }

    #[test]
    fn frames_outside_user_code_are_dropped() {
        let root = Path::new("/repo");
        assert_eq!(relative(root, "node:internal/main"), None);
        assert_eq!(relative(root, "file:///repo/node_modules/x/index.js"), None);
        assert_eq!(relative(root, "/usr/lib/python3.13/json/__init__.py"), None);
        assert_eq!(
            relative(root, "file:///repo/src/fuzzy.ts").as_deref(),
            Some("src/fuzzy.ts")
        );
        // A workload script elsewhere is user code, shown with its full path.
        assert_eq!(
            relative(root, "/tmp/bench.ts").as_deref(),
            Some("/tmp/bench.ts")
        );
        assert!(in_repo("src/fuzzy.ts") && !in_repo("/tmp/bench.ts"));
    }

    #[test]
    fn a_relative_script_path_under_the_repository_is_kept() {
        let root = std::env::temp_dir().join(format!("ra-profile-rel-{}", std::process::id()));
        std::fs::create_dir_all(&root).unwrap();
        std::fs::write(root.join("bench.py"), "").unwrap();
        assert_eq!(relative(&root, "bench.py").as_deref(), Some("bench.py"));
        assert_eq!(relative(&root, "./bench.py").as_deref(), Some("bench.py"));
        assert_eq!(relative(&root, "missing.py"), None);
        assert_eq!(relative(&root, "<frozen runpy>"), None);
        assert_eq!(relative(&root, "~"), None);
        std::fs::remove_dir_all(&root).unwrap();
    }

    fn node(id: u64, name: &str, url: &str, line: i64, hits: u64, children: &[u64]) -> Value {
        json!({ "id": id, "hitCount": hits, "children": children,
                "callFrame": { "functionName": name, "url": url, "lineNumber": line - 1 } })
    }

    #[test]
    fn node_writes_profile_to_a_temp_path_with_spaces() {
        if !Command::new("node")
            .arg("--version")
            .status()
            .is_ok_and(|s| s.success())
        {
            return;
        }
        let dir = crate::app::temp_dir("profile with spaces").unwrap();
        let root = dir.canonicalize().unwrap();
        let command = ["node".into(), "-e".into(), "console.log('workload')".into()];
        let result = super::node(&root, &command, &root);
        std::fs::remove_dir_all(dir).unwrap();
        assert!(result.unwrap().2 > 0.0);
    }

    #[test]
    fn cpuprofile_times_and_callers() {
        let f = "file:///repo/src/search.ts";
        // (root) -> matched -> (anonymous) -> fuzzyScore -> fuzzyScore (recursion); 1 ms a sample.
        let profile = json!({
            "startTime": 0, "endTime": 10_000, "samples": vec![0; 10],
            "nodes": [
                node(1, "(root)", "", 0, 0, &[2]),
                node(2, "matched", f, 9, 1, &[3]),
                node(3, "", f, 10, 1, &[4]),
                node(4, "fuzzyScore", f, 2, 5, &[5]),
                node(5, "fuzzyScore", f, 2, 3, &[]),
            ],
        });
        let mut functions = HashMap::new();
        let ms = merge_cpuprofile(Path::new("/repo"), &profile, &mut functions, &mut 0.0);
        assert_eq!(ms, 10.0);
        let key = |n: &str, l: u32| ("src/search.ts".to_string(), n.to_string(), l);
        let fuzzy = &functions[&key("fuzzyScore", 2)];
        assert_eq!(
            (fuzzy.self_ms, fuzzy.total_ms),
            (8.0, 8.0),
            "recursion counts once"
        );
        // The caller is the named function, not the arrow callback in between.
        assert_eq!(
            fuzzy.callers.keys().collect::<Vec<_>>(),
            [&key("matched", 9)]
        );
        assert_eq!(functions[&key("matched", 9)].total_ms, 10.0);
        assert!(functions.contains_key(&key("(anonymous):10", 10)));
    }

    #[test]
    fn cpuprofile_blocking_child_processes_go_to_the_caller() {
        let f = "file:///repo/lib.js";
        let cp = "node:child_process";
        // main -> runAll -> execSync -> spawnSync (6 ms waiting); main -> crunch (3 ms);
        // main -> spawn (async: starting it is all the profile sees).
        let profile = json!({
            "startTime": 0, "endTime": 10_000, "samples": vec![0; 10],
            "nodes": [
                node(1, "(root)", "", 0, 0, &[2]),
                node(2, "main", f, 1, 0, &[3, 6, 7]),
                node(3, "runAll", f, 3, 0, &[4]),
                // runAll's `xs.map((x) => execSync(...))`: the wait is runAll's, as callers are.
                node(4, "", f, 4, 0, &[8]),
                node(8, "execSync", cp, 945, 0, &[5]),
                node(5, "spawnSync", cp, 823, 6, &[]),
                node(6, "crunch", f, 11, 3, &[]),
                node(7, "spawn", cp, 700, 1, &[]),
            ],
        });
        let mut functions = HashMap::new();
        merge_cpuprofile(Path::new("/repo"), &profile, &mut functions, &mut 0.0);
        let at = |n: &str, l: u32| &functions[&("lib.js".to_string(), n.to_string(), l)];
        assert_eq!(
            (at("runAll", 3).spawn_ms, at("runAll", 3).waits_ms),
            (6.0, 6.0)
        );
        assert_eq!((at("main", 1).spawn_ms, at("main", 1).waits_ms), (0.0, 6.0));
        assert_eq!(
            at("main", 1).total_ms,
            10.0,
            "the wait is still the caller's time"
        );
        assert_eq!(
            (at("crunch", 11).spawn_ms, at("crunch", 11).waits_ms),
            (0.0, 0.0)
        );
        let callback = at("(anonymous):4", 4);
        assert_eq!((callback.spawn_ms, callback.waits_ms), (0.0, 6.0));
    }

    #[test]
    fn python_waits_on_subprocess_go_to_the_repository_caller() {
        let root = Path::new("/repo");
        let sp = "/usr/lib64/python3.12/subprocess.py";
        let lib = "/repo/.venv/lib/python3.12/site-packages/gitlib/cmd.py";
        let entry = |file: &str, name: &str, line: u64, total: f64, callers: Value| {
            json!({"file": file, "line": line, "name": name, "calls": 1, "self": 0.0,
                   "total": total, "stdlib": file.starts_with("/usr/lib64/python3.12/"),
                   "callers": callers})
        };
        let caller = |file: &str, name: &str, line: u64, total: f64| json!({"file": file, "line": line, "name": name, "calls": 1, "total": total});
        let (bench, tool) = ("/repo/bench.py", "/repo/tool.py");
        let entries = [
            entry(bench, "<module>", 1, 200.0, json!([])),
            entry(
                tool,
                "run_all",
                5,
                90.0,
                json!([caller(bench, "<module>", 1, 90.0)]),
            ),
            entry(
                tool,
                "shell_all",
                12,
                10.0,
                json!([caller(bench, "<module>", 1, 10.0)]),
            ),
            entry(
                sp,
                "run",
                464,
                85.0,
                json!([caller(tool, "run_all", 5, 85.0)]),
            ),
            // Called from `subprocess.run`, not from user code: counted once, through `run`.
            entry(
                sp,
                "communicate",
                1090,
                84.0,
                json!([caller(sp, "run", 464, 84.0)]),
            ),
            entry(
                "~",
                "<built-in method posix.system>",
                0,
                9.0,
                json!([caller(tool, "shell_all", 12, 9.0)]),
            ),
            // Mutual recursion, a wait at the bottom: the wait is not fed around the cycle, and
            // only the function that starts it is known to wait.
            entry(
                tool,
                "even",
                20,
                40.0,
                json!([
                    caller(bench, "<module>", 1, 40.0),
                    caller(tool, "odd", 25, 40.0)
                ]),
            ),
            entry(
                tool,
                "odd",
                25,
                40.0,
                json!([caller(tool, "even", 20, 40.0)]),
            ),
            entry(
                sp,
                "check_output",
                400,
                2.0,
                json!([caller(tool, "even", 20, 2.0)]),
            ),
            // A library between user code and the wait: `fetch` -> `gitlib.execute` ->
            // `with Popen(...)`, which waits in `__exit__`.
            entry(
                tool,
                "fetch",
                30,
                30.0,
                json!([caller(bench, "<module>", 1, 30.0)]),
            ),
            entry(
                lib,
                "execute",
                10,
                30.0,
                json!([caller(tool, "fetch", 30, 30.0)]),
            ),
            entry(
                sp,
                "__exit__",
                1100,
                25.0,
                json!([caller(lib, "execute", 10, 25.0)]),
            ),
            // A user function recursing through a library (`ast.NodeVisitor`): its wait is its
            // own, once.
            entry(
                tool,
                "visit_Call",
                40,
                12.0,
                json!([
                    caller(bench, "<module>", 1, 12.0),
                    caller("/usr/lib64/python3.12/ast.py", "visit", 410, 12.0)
                ]),
            ),
            entry(
                "/usr/lib64/python3.12/ast.py",
                "visit",
                410,
                12.0,
                json!([caller(
                    "/usr/lib64/python3.12/ast.py",
                    "generic_visit",
                    420,
                    12.0
                )]),
            ),
            entry(
                "/usr/lib64/python3.12/ast.py",
                "generic_visit",
                420,
                12.0,
                json!([caller(tool, "visit_Call", 40, 12.0)]),
            ),
            entry(
                sp,
                "run",
                464,
                10.0,
                json!([caller(tool, "visit_Call", 40, 10.0)]),
            ),
            // A repository module that happens to be called `subprocess.py` is user code.
            entry(
                "/repo/pkg/subprocess.py",
                "run",
                3,
                20.0,
                json!([caller(bench, "<module>", 1, 20.0)]),
            ),
        ];
        let (functions, _) = ingest_entries(root, &entries);
        let at = |file: &str, n: &str, l: u32| {
            let a = &functions[&(file.to_string(), n.to_string(), l)];
            (a.spawn_ms, a.waits_ms)
        };
        assert_eq!(at("tool.py", "run_all", 5), (85.0, 85.0));
        assert_eq!(at("tool.py", "shell_all", 12), (9.0, 9.0));
        assert_eq!(at("tool.py", "even", 20), (2.0, 2.0));
        assert_eq!(at("tool.py", "odd", 25), (0.0, 0.0));
        assert_eq!(at("tool.py", "fetch", 30), (25.0, 25.0));
        assert_eq!(at("tool.py", "visit_Call", 40), (10.0, 10.0));
        assert_eq!(
            at("bench.py", "<module>", 1),
            (0.0, 85.0 + 9.0 + 2.0 + 25.0 + 10.0)
        );
        assert_eq!(at("pkg/subprocess.py", "run", 3), (0.0, 0.0));
        assert_eq!(
            functions[&("pkg/subprocess.py".into(), "run".into(), 3)].total_ms,
            20.0
        );
        assert!(
            !functions
                .keys()
                .any(|k| k.0.ends_with("cmd.py") || k.0.contains("lib64")),
            "{:?}",
            functions.keys()
        );
        // Each wait is started by one user function, so the parts add up.
        let started: f64 = functions.values().map(|a| a.spawn_ms).sum();
        assert_eq!(started, 85.0 + 9.0 + 2.0 + 25.0 + 10.0);
    }

    #[test]
    fn time_waiting_on_child_processes_is_reported_and_not_offered_for_porting() {
        let root =
            std::env::temp_dir().join(format!("rust-anything-children-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&root);
        std::fs::create_dir_all(&root).unwrap();
        std::fs::write(
            root.join("package.json"),
            r#"{"name":"demo","type":"module"}"#,
        )
        .unwrap();
        std::fs::write(
            root.join("lib.js"),
            "import { execSync } from \"node:child_process\";\n\n\
             export function runAll(n) {\n  let out = 0;\n  for (let i = 0; i < n; i++) {\n    \
             out += execSync(\"sleep 0.2\").length;\n  }\n  return out;\n}\n\n\
             export function crunch(n) {\n  let s = 0;\n  for (let i = 0; i < n; i++) s = (s + i * i) % 1000003;\n  return s;\n}\n",
        )
        .unwrap();
        let build = |wait: f64, cpu: f64| {
            let functions: HashMap<Key, Acc> = [
                (
                    ("lib.js".to_string(), "runAll".to_string(), 3),
                    Acc {
                        total_ms: wait,
                        spawn_ms: wait,
                        waits_ms: wait,
                        ..Default::default()
                    },
                ),
                (
                    ("lib.js".to_string(), "crunch".to_string(), 11),
                    Acc {
                        self_ms: cpu,
                        total_ms: cpu,
                        ..Default::default()
                    },
                ),
            ]
            .into_iter()
            .collect();
            report(
                &root,
                Lang::Ts,
                &["node".into(), "bench.mjs".into()],
                functions,
                0.0,
                100.0,
            )
        };
        let r = build(40.0, 55.0);
        let r_spawns = build(60.0, 30.0);
        let quiet = build(0.0, 90.0);
        assert_eq!(
            (r.child_pct, r_spawns.child_pct, quiet.child_pct),
            (40.0, 60.0, 0.0)
        );
        std::fs::remove_dir_all(&root).unwrap();

        let c = &r.child_processes[0];
        assert_eq!(
            (
                c.command.as_deref(),
                c.file.as_str(),
                c.line,
                c.function.as_str(),
                c.share
            ),
            (Some("sleep"), "lib.js", 6, "runAll", 40.0)
        );
        let run_all = r.hot.iter().find(|h| h.name == "runAll").unwrap();
        assert_eq!(run_all.child_pct, Some(100.0));
        assert!(run_all.advice.contains(CHILD_ADVICE), "{}", run_all.advice);
        assert_eq!(
            r.hot.iter().find(|h| h.name == "crunch").unwrap().child_pct,
            None
        );
        let md = markdown(&r);
        assert!(
            md.contains("## Child processes\n\n40% of the run waits on child processes (`sleep` from `lib.js:6`). Porting code will not speed this up"),
            "{md}"
        );
        assert!(
            md.contains("| `sleep` | `runAll` (`lib.js:6`) | 40% (40 ms) |"),
            "{md}"
        );
        let json = serde_json::to_value(&r).unwrap();
        assert_eq!(json["child_processes"][0]["command"], "sleep");
        // `go` passes over the waiting function; named, it says why.
        assert_eq!(crate::go::pick(&r, None).unwrap().function, "crunch");
        let named = crate::go::pick(&r, Some("runAll")).unwrap_err().to_string();
        assert!(
            named.contains("100% of its time waiting on child processes"),
            "{named}"
        );
        // When child processes take most of the run, `go` stops unless a function is named.
        let stop = crate::go::pick(&r_spawns, None).unwrap_err().to_string();
        assert!(
            stop.starts_with("60% of the run waits on child processes"),
            "{stop}"
        );
        assert!(stop.contains("--function"), "{stop}");
        assert_eq!(
            crate::go::pick(&r_spawns, Some("crunch")).unwrap().function,
            "crunch"
        );
        // Nothing waits: no section, no field.
        assert!(quiet.child_processes.is_empty());
        assert!(!markdown(&quiet).contains("Child processes"));
        assert!(
            serde_json::to_value(&quiet)
                .unwrap()
                .get("child_processes")
                .is_none()
        );
        assert!(
            serde_json::to_value(&quiet)
                .unwrap()
                .get("child_pct")
                .is_none()
        );
        assert!(
            serde_json::to_value(&quiet).unwrap()["hot"][0]
                .get("child_pct")
                .is_none()
        );
    }

    #[test]
    fn runtime_library_paths_are_matched_by_component() {
        let root = Path::new("/repo");
        assert_eq!(
            relative(root, "/repo/lib64/python3_tools/work.py").as_deref(),
            Some("lib64/python3_tools/work.py")
        );
        assert_eq!(
            relative(root, "/repo/lib/python3_tools/work.py").as_deref(),
            Some("lib/python3_tools/work.py")
        );
        for installed in [
            "/usr/lib64/python3.12/subprocess.py",
            "/usr/lib/python3.9/os.py",
            "/usr/lib/python3/dist-packages/yaml/__init__.py",
            "/usr/lib/python3/apt/package.py",
        ] {
            assert_eq!(relative(root, installed), None, "{installed}");
        }
    }

    #[test]
    fn cpuprofile_waits_with_no_user_frame_still_count_for_the_run() {
        let lib = "file:///repo/node_modules/timers-lib/index.js";
        let cp = "node:child_process";
        // A library's timer callback blocks in execSync for 7 ms; no user frame is on the stack.
        let profile = json!({
            "startTime": 0, "endTime": 10_000, "samples": vec![0; 10],
            "nodes": [
                node(1, "(root)", "", 0, 0, &[2, 6]),
                node(2, "tick", lib, 3, 0, &[3]),
                node(3, "execSync", cp, 945, 0, &[4]),
                node(4, "spawnSync", cp, 823, 7, &[]),
                node(6, "main", "file:///repo/app.js", 1, 0, &[7]),
                node(7, "execSync", cp, 945, 2, &[]),
            ],
        });
        let mut functions = HashMap::new();
        let mut unowned = 0.0;
        merge_cpuprofile(Path::new("/repo"), &profile, &mut functions, &mut unowned);
        assert_eq!(unowned, 7.0, "only the wait no user function owns");
        assert_eq!(
            functions[&("app.js".to_string(), "main".to_string(), 1)].spawn_ms,
            2.0
        );
        let r = report(
            Path::new("/nonexistent-rust-anything"),
            Lang::Ts,
            &["node".into()],
            functions,
            unowned,
            10.0,
        );
        assert_eq!(r.child_pct, 90.0);
    }

    #[test]
    fn python_waits_come_from_the_standard_library_only() {
        let root = Path::new("/repo");
        let sp = "/usr/lib/python3.12/subprocess.py";
        let fake = "/repo/.venv/lib/python3.12/site-packages/runner/subprocess.py";
        let lib = "/repo/.venv/lib/python3.12/site-packages/sched/jobs.py";
        let entry = |file: &str, name: &str, total: f64, stdlib: bool, callers: Value| {
            json!({"file": file, "line": 1, "name": name, "calls": 1, "self": 0.0,
                   "total": total, "stdlib": stdlib, "callers": callers})
        };
        let caller = |file: &str, name: &str, total: f64| json!({"file": file, "line": 1, "name": name, "calls": 1, "total": total});
        let entries = [
            entry("/repo/app.py", "crunch", 50.0, false, json!([])),
            // A dependency's own `subprocess.py` with a CPU-bound `run`.
            entry(
                fake,
                "run",
                45.0,
                false,
                json!([caller("/repo/app.py", "crunch", 45.0)]),
            ),
            // A library job with no user frame above it waits 20 ms.
            entry(lib, "job", 20.0, false, json!([])),
            entry(sp, "run", 20.0, true, json!([caller(lib, "job", 20.0)])),
        ];
        let (functions, unowned) = ingest_entries(root, &entries);
        let crunch = &functions[&("app.py".to_string(), "crunch".to_string(), 1)];
        assert_eq!((crunch.spawn_ms, crunch.waits_ms), (0.0, 0.0));
        assert_eq!(unowned, 20.0);
    }

    #[test]
    fn shared_helpers_and_recursion_do_not_confirm_a_wait() {
        let root = Path::new("/repo");
        let sp = "/usr/lib/python3.12/subprocess.py";
        let entry = |name: &str, total: f64, callers: Value| {
            json!({"file": "/repo/app.py", "line": 1, "name": name, "calls": 1, "self": 0.0,
                   "total": total, "stdlib": false, "callers": callers})
        };
        let caller = |name: &str, total: f64| json!({"file": "/repo/app.py", "line": 1, "name": name, "calls": 1, "total": total});
        let spawn = |total: f64, by: &str| {
            json!({"file": sp, "line": 506, "name": "run", "calls": 1, "self": 0.0,
                   "total": total, "stdlib": true, "callers": [caller(by, total)]})
        };
        let entries = [
            entry("<module>", 400.0, json!([])),
            // `parse` runs `helper` for 80 ms of CPU, `fetch` for 80 ms of waiting.
            entry("parse", 80.0, json!([caller("<module>", 80.0)])),
            entry("fetch", 80.0, json!([caller("<module>", 80.0)])),
            entry(
                "helper",
                160.0,
                json!([caller("parse", 80.0), caller("fetch", 80.0)]),
            ),
            spawn(80.0, "helper"),
        ];
        let (functions, _) = ingest_entries(root, &entries);
        let waits = |n: &str| functions[&("app.py".to_string(), n.to_string(), 1)].waits_ms;
        assert_eq!(waits("helper"), 80.0);
        assert_eq!(waits("parse"), 0.0, "no wait is confirmed under parse");

        // One call of `a` waits 60 ms; `a -> b` and `b -> a` only compute.
        let entries = [
            entry("<module>", 200.0, json!([])),
            entry(
                "a",
                100.0,
                json!([caller("<module>", 100.0), caller("b", 30.0)]),
            ),
            entry("b", 80.0, json!([caller("a", 80.0)])),
            spawn(60.0, "a"),
        ];
        let (functions, _) = ingest_entries(root, &entries);
        let waits = |n: &str| functions[&("app.py".to_string(), n.to_string(), 1)].waits_ms;
        assert_eq!((waits("a"), waits("b")), (60.0, 0.0));
    }

    #[test]
    fn go_looks_past_the_rows_the_report_shows() {
        let root = std::env::temp_dir().join(format!("rust-anything-rows-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&root);
        std::fs::create_dir_all(&root).unwrap();
        std::fs::write(
            root.join("package.json"),
            r#"{"name":"demo","type":"module"}"#,
        )
        .unwrap();
        std::fs::write(
            root.join("lib.js"),
            "export function parse(text) {\n  return text.split(\",\").length;\n}\n",
        )
        .unwrap();
        let mut functions: HashMap<Key, Acc> = (0..25)
            .map(|i| {
                (
                    ("lib.js".to_string(), format!("wait{i}"), 100 + i),
                    Acc {
                        total_ms: 40.0,
                        waits_ms: 40.0,
                        spawn_ms: if i == 0 { 40.0 } else { 0.0 },
                        ..Default::default()
                    },
                )
            })
            .collect();
        functions.insert(
            ("lib.js".to_string(), "parse".to_string(), 1),
            Acc {
                self_ms: 30.0,
                total_ms: 30.0,
                ..Default::default()
            },
        );
        let r = report(&root, Lang::Ts, &["node".into()], functions, 0.0, 100.0);
        std::fs::remove_dir_all(&root).unwrap();
        let json = serde_json::to_value(&r).unwrap();
        assert_eq!(json["hot"].as_array().unwrap().len(), 25);
        assert!(!markdown(&r).contains("`parse`"));
        assert_eq!(crate::go::pick(&r, None).unwrap().function, "parse");
    }

    #[test]
    fn executable_paths_stay_whole() {
        let js = "import { execFileSync, spawnSync, execSync } from \"node:child_process\";\n\
                  execFileSync(\"/opt/My Tools/worker\", []);\n\
                  spawnSync(\"ffmpeg\", [\"-i\", \"a b\"]);\n\
                  execSync(\"sleep 0.2\");\n\
                  execFileSync(`/opt/${dir}/worker`, []);\n";
        let programs: Vec<Option<String>> = js_spawn_calls("a.js", js)
            .into_iter()
            .map(|c| c.program)
            .collect();
        assert_eq!(
            programs,
            [
                Some("/opt/My Tools/worker".into()),
                Some("ffmpeg".into()),
                Some("sleep".into()),
                None
            ]
        );
        let py = "import subprocess\nsubprocess.run([\"/opt/My Tools/worker\", \"-x\"])\n\
                  subprocess.run(\"git status\", shell=True)\n";
        let programs: Vec<Option<String>> = python_spawn_calls(py)
            .into_iter()
            .map(|c| c.program)
            .collect();
        assert_eq!(
            programs,
            [Some("/opt/My Tools/worker".into()), Some("git".into())]
        );
    }

    #[test]
    fn a_callback_wait_and_a_direct_call_that_disagree_name_no_program() {
        let src = "import { execSync } from \"node:child_process\";\n\
                   export function sync(xs, cold) {\n\
                   \x20 if (cold) execSync(\"git status\");\n\
                   \x20 xs.forEach((x) => execSync(`sleep ${x}`));\n\
                   }\n\
                   export function outer() {\n\
                   \x20 function inner() { return execSync(\"git log\"); }\n\
                   \x20 execSync(\"make\");\n\
                   \x20 return inner();\n\
                   }\n";
        let calls = js_spawn_calls("a.js", src);
        assert_eq!(spawn_site(&calls, "sync", 2), None);
        assert_eq!(
            spawn_site(&calls, "outer", 6),
            Some((8, Some("make".into()))),
            "a named inner function owns its own wait"
        );
        assert_eq!(
            spawn_site(&calls, "inner", 7),
            Some((7, Some("git".into())))
        );
    }

    #[test]
    fn the_program_in_a_command_line() {
        assert_eq!(first_word("sleep 0.2", false).as_deref(), Some("sleep"));
        assert_eq!(
            first_word("TOKEN=abc LANG=C deploy --now", false).as_deref(),
            Some("deploy")
        );
        assert_eq!(
            first_word("TOKEN=abc", false),
            None,
            "never shows an assignment"
        );
        assert_eq!(first_word("TOKEN='abc def' deploy", false), None);
        assert_eq!(first_word("ls ", true).as_deref(), Some("ls"));
        assert_eq!(
            first_word("git", true),
            None,
            "`git${{x}}`: the program is computed"
        );
        assert_eq!(first_word("a|b c", false), None);
        assert_eq!(first_word("$HOME/bin/x run", false), None);
    }

    #[test]
    fn javascript_spawn_calls_and_their_programs() {
        let src = "import { execSync, execFileSync } from \"node:child_process\";\n\
                   import * as cp from \"node:child_process\";\n\
                   execSync(\"make\");\n\
                   export function runAll(xs) {\n\
                   \x20 // old: execSync(\"echo hi\")\n\
                   \x20 const sep = '}';\n\
                   \x20 return xs.map((x) =>\n\
                   \x20   execSync(`sleep ${x}`).length);\n\
                   }\n\
                   export function both() { return execSync(\"git status\") + cp.execSync(\"npm test\"); }\n\
                   export function many(\n\
                   \x20 opts: { cwd: string },\n\
                   ): void {\n\
                   \x20 execFileSync(\n\
                   \x20   \"ffmpeg\",\n\
                   \x20   [\"-i\", opts.cwd],\n\
                   \x20 );\n\
                   \x20 cp.spawnSync(`${opts.cwd}/bin`, []);\n\
                   }\n";
        let calls = js_spawn_calls("lib.ts", src);
        let summary: Vec<(Vec<u32>, u32, Option<&str>)> = calls
            .iter()
            .map(|c| (c.scopes.clone(), c.line, c.program.as_deref()))
            .collect();
        assert_eq!(
            summary,
            [
                (vec![], 3, Some("make")),
                (vec![4, 7], 8, Some("sleep")),
                (vec![10], 10, Some("git")),
                (vec![10], 10, Some("npm")),
                (vec![11], 14, Some("ffmpeg")),
                (vec![11], 18, None),
            ]
        );
    }

    #[test]
    fn python_spawn_calls_and_their_programs() {
        let src = "import os\nimport subprocess as sp\nfrom subprocess import check_output as co\n\n\
                   sp.run([\"make\"])\n\n\
                   def run_all(\n    n: int,\n) -> None:\n    \"\"\"e.g. sp.run([\"echo\"])\"\"\"\n    \
                   sp.run(\n        [\"git\", \"status\"],\n    )\n\n\n\
                   @cache\ndef shell():\n    os.system(\"\"\"sleep 0.1\"\"\")\n    co(args=(\"ls\", \"-l\"))\n    \
                   sp.check_call(f\"{tool} run\")\n    sp.Popen(f\"tar {flags}\")\n    \
                   other.run(\"nope\")\n    run(\"not imported\")\n";
        let calls = python_spawn_calls(src);
        let summary: Vec<(Vec<u32>, u32, Option<&str>)> = calls
            .iter()
            .map(|c| (c.scopes.clone(), c.line, c.program.as_deref()))
            .collect();
        assert_eq!(
            summary,
            [
                (vec![], 5, Some("make")),
                (vec![7], 11, Some("git")),
                (vec![16], 18, Some("sleep")),
                (vec![16], 19, Some("ls")),
                (vec![16], 20, None),
                (vec![16], 21, Some("tar")),
            ]
        );
    }

    #[test]
    fn the_site_of_a_functions_child_process() {
        let root = std::env::temp_dir().join(format!("rust-anything-sites-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&root);
        std::fs::create_dir_all(&root).unwrap();
        std::fs::write(
            root.join("tool.py"),
            "import os, subprocess\n\nsubprocess.run([\"make\"])\n\n\
             def same():\n    subprocess.run([\"git\", \"fetch\"])\n    subprocess.check_output([\"git\", \"log\"])\n\n\
             def mixed(x):\n    os.system(\"make\")\n    subprocess.run(x)\n\n\
             def other(p):\n    return p.check_output()\n",
        )
        .unwrap();
        std::fs::write(
            root.join("lib.js"),
            "import { execSync } from \"node:child_process\";\nexecSync(\"make\");\n\
             export function runAll(xs) {\n  return xs.map((x) => execSync(`sleep ${x}`));\n}\n",
        )
        .unwrap();
        let (py_calls, js_calls) = (
            spawn_calls_in(&root, Lang::Python, "tool.py"),
            spawn_calls_in(&root, Lang::Ts, "lib.js"),
        );
        let py = |name: &str, line| spawn_site(&py_calls, name, line);
        let js = |name: &str, line| spawn_site(&js_calls, name, line);
        assert!(spawn_calls_in(&root, Lang::Python, "/elsewhere/tool.py").is_empty());
        assert!(spawn_calls_in(&root, Lang::Python, "../tool.py").is_empty());
        let got = (
            py("<module>", 1),
            py("same", 5),
            py("mixed", 9),
            py("other", 13),
            js("(anonymous):1", 1),
            js("runAll", 3),
            js("(anonymous):4", 4),
        );
        std::fs::remove_dir_all(&root).unwrap();
        assert_eq!(
            got.0,
            Some((3, Some("make".into()))),
            "the top level of a script"
        );
        assert_eq!(
            got.1,
            Some((6, Some("git".into()))),
            "two calls, one program"
        );
        assert_eq!(
            got.2, None,
            "two calls, two programs: the function's line is shown"
        );
        assert_eq!(
            got.3, None,
            "`p.check_output()` is not `subprocess.check_output`"
        );
        assert_eq!(got.4, Some((2, Some("make".into()))));
        assert_eq!(
            got.5,
            Some((4, Some("sleep".into()))),
            "a call in a callback"
        );
        assert_eq!(got.6, Some((4, Some("sleep".into()))));
    }

    #[test]
    fn cpuprofile_recognizes_encoded_paths_in_the_repository() {
        let root = Path::new("/repo/work area");
        let profile = json!({
            "startTime": 0,
            "endTime": 10_000,
            "samples": [1],
            "nodes": [node(1, "hot", "file:///repo/work%20area/step%23one%25.mjs", 0, 1, &[])],
        });
        let mut functions = HashMap::new();
        merge_cpuprofile(root, &profile, &mut functions, &mut 0.0);
        assert!(
            functions.contains_key(&("step#one%.mjs".into(), "hot".into(), 0)),
            "{:?}",
            functions.keys()
        );
        assert_eq!(
            relative(root, "/repo/work area/step%20.mjs").as_deref(),
            Some("step%20.mjs")
        );
    }

    #[test]
    fn finds_the_argument_that_varies_per_item() {
        let root =
            std::env::temp_dir().join(format!("rust-anything-profile-{}", std::process::id()));
        std::fs::create_dir_all(&root).unwrap();
        std::fs::write(
            root.join("search.ts"),
            "export function matched(query: string, all: string[]) {\n  return all.map(key => ({ key, score: fuzzyScore(query, key) }))\n}\n",
        )
        .unwrap();
        std::fs::write(
            root.join("search.py"),
            "def best(q, keys):\n    for k in keys:\n        yield score(q, k)\n",
        )
        .unwrap();
        let params = ["query".to_string(), "text".to_string()];
        let caller = |file: &str, name: &str| (file.to_string(), name.to_string(), 1);
        assert_eq!(
            batch_param(
                &root,
                &caller("search.ts", "matched"),
                "fuzzyScore",
                &params
            )
            .as_deref(),
            Some("text")
        );
        assert_eq!(
            batch_param(&root, &caller("search.py", "best"), "score", &params).as_deref(),
            Some("text")
        );
        std::fs::write(
            root.join("nested.ts"),
            "function main() {\n  for (const query of queries) {\n    for (const text of items) {\n      if (score(query, text) > 0) hits++\n    }\n  }\n}\n",
        )
        .unwrap();
        assert_eq!(
            batch_param(&root, &caller("nested.ts", "main"), "score", &params).as_deref(),
            Some("text"),
            "the innermost loop runs over the items"
        );
        // `query` is the same for every item: no batch parameter to give.
        assert_eq!(
            batch_param(&root, &caller("search.ts", "matched"), "missing", &params),
            None
        );
        std::fs::remove_dir_all(&root).unwrap();
    }

    #[test]
    fn go_names_and_pprof() {
        assert_eq!(go_name("example.com/strkit/strkit.Slugify"), "Slugify");
        assert_eq!(go_name("testing.(*B).runN"), "runN");
        assert_eq!(
            go_name("example.com/x/pkg.Outer.func2"),
            "(anonymous) in Outer"
        );
        let raw = "Samples:\nsamples/count cpu/nanoseconds\n          2   20000000: 1 2 \n          1   10000000: 3 2 \n\
                   Locations\n     1: 0x1 M=1 golang.org/x/text.Map /home/u/go/pkg/mod/golang.org/x/text/map.go:568:0 s=528\n\
                   \x20            example.com/x/pkg.Slugify /repo/pkg/a.go:21:0 s=18\n\
                   \x20    2: 0x2 M=1 example.com/x/pkg.slugAll /repo/pkg/a_test.go:24:0 s=21\n\
                   \x20    3: 0x3 M=1 example.com/x/pkg.Slugify /repo/pkg/a.go:25:0 s=18\nMappings\n";
        let samples = parse_pprof_raw(Path::new("/repo"), raw);
        assert_eq!(samples.len(), 2);
        let key = |f: &str, n: &str, l: u32| Some((f.to_string(), n.to_string(), l));
        // Root first; the inlined Slugify sits between slugAll and strings.Map (not user code).
        assert_eq!(
            samples[0],
            (
                vec![
                    key("pkg/a_test.go", "slugAll", 21),
                    key("pkg/a.go", "Slugify", 18),
                    None
                ],
                20.0
            )
        );
        let mut functions = HashMap::new();
        merge_samples(&samples, &mut functions);
        let slug = &functions[&("pkg/a.go".to_string(), "Slugify".to_string(), 18)];
        assert_eq!((slug.total_ms, slug.self_ms), (30.0, 10.0));
        assert_eq!(slug.callers.values().map(|c| c.0).sum::<f64>(), 30.0);
    }

    #[test]
    fn java_declarations() {
        let src = "class Strings {\n    public static String slugify(String text, int max) {\n        \
                   String s = slugify(text, 0);\n        return s;\n    }\n}\n";
        assert_eq!(java_declaration(src, "slugify", 4), Some(2));
        assert_eq!(java_declaration(src, "slugify", 1), None);
    }

    #[test]
    fn loop_variables_in_go_java_ruby() {
        let (items, indexes) = loop_variables(
            "for _, t := range texts {\n\tSlugify(t, 30)\n}\nfor i := range xs {}\n\
             for (String w : words) {}\nfor (int j = 0; j < n; j++) {}\ntexts.map { |x| f(x) }\nxs.each do |y, k|\n",
        );
        assert_eq!(items, ["t", "w", "x", "y"]);
        assert_eq!(
            indexes,
            ["_", "i", "j"]
                .into_iter()
                .filter(|v| *v != "_")
                .collect::<Vec<_>>()
        );
    }
}