热点代码是怎么变成机器码的?这篇用 PrintCompilation 把一个方法的「解释 → C1 → C2」过程和每一档的速度逐批量出来(分层编译的阈值、去优化、内联、OSR 各有后面的专门篇目)。
批 1:调用 100 次,平均 300.0 ns/次 31 121 3 Main::work (31 bytes) 批 2:调用 100 次,平均 265.4 ns/次 批 3:调用 100 次,平均 57.1 ns/次 批 4:调用 100 次,平均 57.9 ns/次 批 5:调用 100 次,平均 57.5 ns/次 批 6:调用 100 次,平均 49.6 ns/次 批 7:调用 100 次,平均 55.8 ns/次 批 8:调用 100 次,平均 47.9 ns/次 32 130 4 Main::work (31 bytes) 批 9:调用 20000 次,平均 43.4 ns/次 32 121 3 Main::work (31 bytes) made not entrant 批 10:调用 20000 次,平均 21.7 ns/次 批 11:调用 20000 次,平均 19.2 ns/次 批 12:调用 20000 次,平均 17.9 ns/次 批 13:调用 20000 次,平均 12.7 ns/次 批 14:调用 20000 次,平均 8.7 ns/次
方式 总耗时中位数(ms) 平均每次(ns) -Xint(纯解释器) 554 184.7 -XX:TieredStopAtLevel=1(只 C1) 31 10.3 默认(分层编译 C1 + C2) 23 7.7 -XX:-TieredCompilation(只 C2) 24 8.0