Hot Fix Design AOT / JIT & dexopt and dex2oat (a)

 

Ali P7 mobile Internet architect, Advanced Video (Daily update) free Learning please click: https://space.bilibili.com/474380680
This article will start with the AOT / JIT & dexopt and dex2oat to introduce hot fix design:

A, AOT / JIT

A program compilation process may be iterative step, i.e. after the end of each of the results obtained in step a can be run independently, for example, and then outputs the first byte code configured AST, AST also can be explained intermediate state is performed. Due to the nature transcoding is compiled, so you can have multiple independent compiler for a language, each responsible for one step

AOT Compiler and JIT Compiler is for the compiled form to do classification:
AOT: Ahead Of Time, means that before the compiler is run, such as ordinary static compiler
JIT: Just In Time, refer compiled at run time, while running side compiler, such as java virtual machine used at run time JIT technology

JIT may know of some people, AOT term is relatively rare few, in fact, in addition to JIT, and the rest are AOT. JIT also a lot of explanation on the wiki detail than AOT, if by understanding the wiki, in general, is up from a form of distinction between the two concepts, that is not looking to be compiled in the "runtime"

However, these two concepts have ambiguity, the problem is that this "run-time" how to distinguish between, say, from the concept point of view, python is used JIT technology because:

... 
import a 
... 

When the execution to import a course is run, this time if only to find a.py, will work to compile and generate a.pyc, which is characteristic of JIT python, but generally speaking, think of python JIT is psyco, pypy and the like, do not think the dynamics of python itself belongs to the category of JIT, or that it's such a "form" of the JIT feature is not included in the scope. Other scripting languages, dynamic languages have a similar situation. I think there are a few specific reasons
was first recognized by the mainstream theory JIT compiler is part of add-ons for their compiled language, that is to say, even if the JIT removed, does not affect the operation of the language itself, such as java, if you turn off JIT can still explain the implementation and operation of the above import python when characteristics Although formally meet the JIT, but this mechanism is the provision of language itself, if removed, the language (mainstream realization) is not complete. Conversely, if the python source code directly using analytical executed, the bytecode compiler for the behavior can be seen as JIT, because do not do not affect the execution process to resolve
Secondly, this python compiler will not always be executed , because in general the results will generate bytecode pyc disk file exists, it is more like a java class file transfer source inertization process is carried out when needed
Finally, the JIT consume runtime resources, may cause Caton process, and the reason why the introduction of JIT java and other languages, because JIT can run faster after byte code compile, Caton period of time to remedy the back, so to speak from an engineering point of view, JIT almost tantamount to run optimization (although not so from concept and form), and the python import only Caton, the speed benefits of nothing
so, although the concept, the example above is indeed consistent with JIT, but generally do not think so departure angle problem, say python comes with or without JIT JIT properties are considered plausible

The reason to give this example because I think embodies the opposition and unified concept of AOT and JIT, is a form of opposition to "Run" as the dividing line, and unity is to say, in fact, all the sequence of instructions to be executed are is the need to re-compile the implementation of such import a, with respect to this whole process is certainly JIT, but relative to a.py this module (python process for the first time when it performs an import module) might be seen as AOT, if some people think do wrong, that change is more obvious example, run immediately if all import a python program are in the process open, before entering the implementation, in accordance with that concept, it is JIT, because the process has already begun running, but Why should not be seen as AOT compilation mode first and then executed, but the whole process is a batch of it?

With this question, consider a lot of information (including wiki) to another description of the JIT, JIT is interpreted at runtime execution language (such as byte code) compiled into machine instructions, in order to improve the operating speed. The view in front of an article also mentioned, there are a lot JIT compiler, such as java is so dry (we took the following java example), however, since the byte-code compiled into machine instructions can improve the speed, why must when carried out on the run, made AOT mode can run more smoothly than you, but also once compiled, N execution, why do have to make runs, JIT was supposed to improve the speed, but this is not it reducing the efficiency?

This view makes sense, in fact, there are some java AOT compiler, it can even java bytecode compiler source code directly into machine instructions executable files, Microsoft's original VJ ++ seems to be so engaging, and the sun beat a long time frame, sun also call for pure java (pure java, that is in accordance with the sun design concepts and standards to achieve java) slogan, are interested can go search about this period of history, very funny

On the other hand, sun jvm Although the use of a JIT compiler, but also provides client and server mode. In server mode, the virtual machine at the beginning of the implementation will first byte code as much as possible to compile and optimized degree as high as possible, so you can make the server during operation as little as possible Caton, as discussed above, this is actually the equivalent of a batch of AOT. Under client mode does not do so, mainly to minimize startup delays, improve the user experience

Incidentally, for the JIT bytecode compiled into machine instructions, wiki description ambiguous, sometimes with machine code, sometimes with native code, for example, we use a java virtual machine A language interpretation A word bytecode execution and compiled into bytecode own java byte code, which is the JIT, because a run on the jvm, the java bytecode seen as native code, and machine code is not necessarily the machine real machine, jvm is a machine

Because after JIT compilation time-consuming operation, then for some optimization points can not be hundred percent support, must make a trade-off between optimization and execution Caton code, AOT would not have this problem, in addition, AOT compilation can be done persisted to storage, and JIT generally run once will do it again every repetitive compilation

If we do not consider the time spent AOT itself (such as compiling once, N runs), does not consider the ease of use (AOT compilation may be several times), it is not considered, AOT JIT compiler can be completely replaced compilation, JIT it is completely unnecessary, and the actual situation of course is not the case, JIT still has its advantages and necessity, otherwise the crowd would not study it are fools

Driven static look at this issue, AOT compiler is static, but dynamic JIT compiler is running, the JIT has the advantage that it can not only see the static information (codes), but also to see the situation at runtime, this is the JIT The advantages. The next discussion of JIT is a narrow JIT, AOT JIT that can not handle the place of use, rather than the form of the above

About JIT advantages are given on a four-point wiki reasons, but interestingly, two of which have even admitted that it is not the only JIT can do, that at least in theory, to achieve (or in part) by AOT is possible, which is four:
. 1, the JIT compiler in real time to the optimal machine instruction according to the current hardware situation, for example if the cpu containing the FPU, parallel computing characteristic MMX, SSE2, or the Intel cpu, the same can be done a word Festival yards, running on different machines to maximize the use of hardware resources. And if AOT compiler is a program put out to different users, can only go to a minimum of characteristics compatible cpu, or the internal implementation of multiple versions
2, JIT can actually run according to the current state of the process, the bytecode compiler to optimize for the sequence of machine instructions. wiki considered static compilation can also be optimized in this area by analyzing the profile (may be a bit of trouble)
3, when the program needs to support dynamic linking, namely static compilation stage, may not know what code will be introduced to, and the program runs collaboration execution, this time can only rely on JIT
4, taking into garbage collection, JIT code that can be adjusted according to the actual process of memory, the cache can make fuller use, wiki considered static compiler can do, but do JIT it is easier to achieve

For the first article, JIT can indeed achieve this optimization, but can still achieve AOT, although AOT compiler implementation of a program to a different user can not do, but can compile byte code issue, and then do it again when the current machine users AOT
for the second, I think first of all state run most programs do not change frequently, such as the same program sometimes integer calculation majority, sometimes floating point calculations are mostly general program scenario is fixed; secondly for a particular scene you can AOT
for the third, really dynamic link full text of static optimization AOT can not do, but the articles mentioned above, when necessary, we can directly cut the dynamics of language, furthermore, when nothing is static compilation are imperceptible, such as C language do statically linked, at least know the header files, dynamic and not so strong
for the fourth, AOT is possible to achieve, although a lot of trouble. On the other hand, when there are static compilation instructions out of order to improve cache the results, and furthermore this also garbage collection algorithms, local program itself has a lot to write bad if the program itself, the effect of this adjustment is also more likely to limited

So I think, although these four are reasonable, but not exactly got a point. Again look at this problem, we can see that, in theory, can completely replace AOT JIT, because the state of a process is limited, AOT can predict all the possible scenarios and optimize the actual run-time status does not exceed the AOT forecasting, optimal use of the code can be executed, and JIT advantage here is that it can accurately know the runtime state, rather than predicted as AOT, lower cost, if a AOT optimized cost is too high, you should select the JIT. AOT is not can not do, but not feasible

JIT relevant information, compared to the wiki I recommend this paper: "Representation-based Just-in-time Specialization and the Psyco prototype for Python" by Armin Rigo, this paper is based on python and its plug-in library psyco example to JIT analysis, thesis topic in the word specialization can be described as the finishing touch, it pointed out that at least in a dynamically typed language, JIT is one of the key role specialization, and use of articles saying that the dynamic behavior of static, and these scenes are not feasible in AOT the reason is that it is difficult to find specialized direction, and an enumeration of all specialization is not feasible

A typical case of specialization, is also mentioned in the paper, consider a function f (x, y), then for the input x1 x, x2, x3 ..., we can specialize the function f1 (y) , f2 (y), f3 (y) ..., where fk (Y) corresponding to f (xk, y) in function, so that, for each fk can be individually optimized to do, irrespective of other functions, and Laid function list at least not slower than the original f (x, y) of the post. The only problem is, the value of x may be many, such as x is an int, then if AOT way to specialization, the need to compile more than 4.2 billion function, which is obviously unrealistic, but it is possible for the JIT scenes do optimization, because, although many values ​​of x, but in the course of a specific operation range is relatively small, even very small, which is in line with twenty-eight law

Thus, in the run-time function f we can do to monitor the statistics of each input x, and if found uneven distribution of these values, such as 123 x is the case of the majority, the dynamic characteristics of a f123 (y) , its highly optimized, and then modify the function f:

func f(x, y): 
    if x == 123: 
        return f123(y) 
    ... //f的正常流程 

So just need a specialized function, can bring to enhance the efficiency of the operation, which is of special advantage of JIT

For many programs, doing the monitoring of such values ​​may be specialized and cost is not high, because it is not a function of the range of input values ​​are presented for each state of imbalance, or is not so obvious, but the above example, x and y not necessarily a variable, it can be the type, so that there is great significance for dynamically typed languages

Recall that the templates can be used to achieve a duck type in C ++, in essence, to achieve the type of static through code replacement, C ++ this way, although efficient, but the channel is the full text of the analysis by the static compilation, is the AOT compiler, if the change to be slightly stronger dynamics of language, irrelevant. In the dynamic type, if there is a function of k parameters, there are n possible types, AOT is a need to expand the function as n ^ k th specialized instances, n and k are not operable slightly larger, and besides, is itself dynamic type, n ranges do not necessarily know at compile time

For this scenario, JIT can be selectively specialized statistical way, the greater feasibility and practical significance, because, when programmers write programs using dynamic types, such as writing a function:

func f(x, y): 
    return x + y 

Theoretically, this function can accept any type of x and y, as long as x and y can add together, but specific to a certain program, the service of the function is generally fixed, or do string concatenation, or is adding value, rarely speak and write a function that takes a different type of operation can not beat the eight-pole, but also programmers deliberately so designed, like said earlier ambiguity as C ++ templates, see no fundamental to this demand, so the input parameter type of the function, in line with twenty-eight law. So for the above codes, it is assumed in most cases x and y are integers, the specialized performed (assuming that the pseudo-code is not considered integer overflow):

func f(x, y): 
    if not (x instanceof int and y instanceof int): 
        //有一个不是整数,走原有流程 
        return x + y 
    //整数加法的特化流程 
    internal_code: 
        int ix = get_internal_int(x) 
        int iy = get_internal_int(y) 
        int iresult 
        asm: 
            push ... //当前状态压栈 
            mov eax, ix 
            mov ebx, iy 
            add eax, ebx 
            mov iresult, eax 
            pop ... //状态出栈 
        return build_int_object(iresult)

Of course, this is just an example, if only for an addition, this number overkill, but if f is more complex logic, optimization is quite clear

Also can reverse thinking about, AOT is difficult to achieve specialization reason is unable to consider all the circumstances, but we do not need to consider all the circumstances, in fact, twenty-eight law itself is another type used in twenty-eight law, specific to the type int a vast majority are using to type int program accounted for the vast majority in all programs, at least in a limited field is so, so had 2k kinds int relevant specialization, so that for each function only situation is quite acceptable (a lot lower than the actual number of cases 2k, because if many parameters are assumed to be int, syntax errors, do not assume that a) if to do a little better, compiler options can also be made by the user AOT's time to specify which type of specialization, so that more perfect

In addition to dynamic type, the other dynamics may be similar discussions on the articles only take an example, not go into details:

for i in range(n): 
    print(i) 
转换为: 
if not (range is builtins.range and print is builtins.print): 
    for i in range(n): 
        print(i) 
else: 
    internal_code: 
        long tmp = get_internal_long(n) 
        long i 
        //这里应该用汇编,仅表个意思 
        for (i = 0; i < tmp; ++ i): 
            print_long(i) 

Need to be saved when the program starts in builtins inside the default function for detecting the current operating environment has not been altered user, thus taking into account the efficiency and dynamic, like the above, here JIT or AOT can be achieved.

Two, dexopt difference with dex2oat

It developed from the application layer have a general understanding on the need to master one principle is, the specific differences available in the following figure outlines (Picture from the network).

 

 
19956127-b3d84776d9e1ddc6.png
 

 

 

You can clearly see the difference between dexopt and dex2oat by the figure, the former for the Dalvik virtual machine, which is a virtual machine for Art.

 

dexopt dex is a file verification and optimization of the operations, it became odex file optimization results dex files, documents and dex files like this, just use some optimization operation code (such as optimizing virtual call instructions, etc.).

dex2oat is AOT dex files compiled ahead of the operation, which requires a dex file, and then compile it, the result is a native ELF executable file that can be executed directly to the local processor.

Besides can also be seen in FIG Dalvik virtual machine has a JIT compiler, that is to say which is also capable of running hot java byte code compiled to execute code, so that the virtual machine still Art difference. Dex2oat Art virtual machine is ahead of all dex bytecode compiler, and the Dalvik virtual machine only the bytecode compiler to use hot heuristic detection is the most frequently performed.
Reference: https://www.jianshu.com/p/26a82119da49
https://blog.csdn.net/xtlisk/article/details/39099199
Ali P7 mobile Internet architect, Advanced Video (Daily update) free learning please click: https://space.bilibili.com/474380680

Guess you like

Origin www.cnblogs.com/Android-Alvin/p/11963062.html