少点错误 2024年08月28日
On Interpters, Optimizing Compilers, and JIT
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文讨论了编译器、解释器和 JIT 在代码优化方面的差异。解释器只能逐行执行代码,而编译器可以分析整个代码并进行优化,例如删除无用代码。JIT 编译器则更强大,它可以在运行时根据实际数据进行优化,甚至可以根据输入数据推断逻辑前提并生成更有效的代码。

📕 解释器就像一个盲人,只能感知到眼前的指令,逐行执行。它无法像编译器那样全局分析代码,因此无法进行代码优化,例如删除无用代码。

💻 编译器可以读取整个源代码,并进行各种优化,例如删除无用代码、常量折叠、内联函数、循环展开和尾递归消除等。编译器就像一个推理引擎,它寻找与源代码功能等效的更高效的程序。

📷 JIT 编译器是更强大的优化工具,它可以在运行时根据实际数据进行优化。它可以分析程序的运行情况,识别“热点代码”,并对这些代码进行优化。JIT 还能够根据输入数据推断逻辑前提,并生成更有效的代码,甚至可以根据当前输入数据禁用一些不必要的代码逻辑。

📶 由于静态编译器无法访问运行时数据,因此无法像 JIT 那样进行基于运行时数据的优化。

Published on August 27, 2024 4:01 PM GMT

Summary: Describe at a high level how interpreters, static compilers, and JIT differ in their ability to optimize code.

Optimizing compilers are awesome.

Compilers and Interpreters

An interpreter is like a blind man. It can only feel the instruction right in front of it. It grabs the instruction and executes it. It sees a = Ackermann(4, 2) and evaluates it. But it turns out, a isn't ever used.

A compiler can read the entire source code, notice that a is never used, and just delete the entire a = Ackermann(4, 2) computation.

This is called dead code elimination. Other common optimization strategies include constant folding, inlining, loop unrolling, and tail call elimination.

In general, we can think of an optimizing compiler as a reasoning engine that searches for a better program that is functionally equivalent to the behavior your source code specifies.

A program can be better by running faster, using less memory, producing a smaller binary, or in the case of tail call elimination, works at all.

JIT compilers

Just In Time (JIT) compilers are even more awesome. We as programmers use profilers to check which part of the code we need to optimize. Why not have a program do this? Dynamically at runtime? Taking into account what data we are actually processing?

That's exactly what JITs do. In theory inlining calls, and unrolling loops, just make your program faster. But it trades off against your code size. It makes your binaries larger, and (I am somehow unsure about this point) your code might no longer fit into the CPU cache.

A JIT can run a lightweight profiler while the program is running and identify hot code (code that is executed very often). It then focuses its optimization ability on this hot code.

But it gets better. Imagine we have some numerical overflow detection in our code. If the numbers in our current input are so small that there is no chance of triggering an overflow, then in principle the JIT could notice this and disable the overflow detection logic.

In general, a JIT can monitor the inputs received. It then infers what logical predicates hold on these inputs. Now it can generate code that assumes that these predicates hold. This potentially allows the JIT to generate more efficient code, which also includes a check that in fact the current input does satisfy the predicates.

A static compiler simply can't perform this type of optimization.



Discuss

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

编译器 解释器 JIT 代码优化
相关文章