K
K
koshak2014-03-14 03:53:21
Classmates
koshak, 2014-03-14 03:53:21

Why is bytecode needed?

I have carefully read en.wikipedia.org/wiki/Bytecode.
I work with Python and C# and I understand why bytecode is needed (and why it is needed in other interpreted languages). Platform independence is really cool.
But why force the target machine to chew the bytecode through a virtual machine/JIT compiler? Why can't it be compiled once for the target architecture at the first launch / access or during installation / deployment (Ahead-Of-Time, in short)?
The reason why this stupid question came up is just as stupid and banal: after all, a program written in C will run faster than a program of similar functionality written in any interpreted language.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Starkov, 2014-03-14
@icelaba

A feature of many jit compilers is that they can optimize code on the fly using program execution statistics,
for example, hotpath optimization counts the number of hits in one or another part of the program, and generates machine code only for pieces of code where the program is actually often executed.
What it does: due to this, the jit optimizer can place pieces of frequently executed machine code very close to each other - so that they all fit in the processor cache, for example, and yes - the jit compiler sometimes overtakes precompiled machine code due to this.
There are still a lot of optimizations, for example, regarding languages ​​that support closures, as practice shows, most closures are used in code with the same environment variables, which allows you not to do a lot of work on saving the environment, etc. - but simply inline the closure - another thing is that it is impossible to understand at the compilation stage , but at the runtime, save the hash of the environment and if it does not change, then inline the code - it's easy.
There are a lot of similar optimizations that really help dynamic languages ​​​​work almost on a par with C on some tasks, a vivid example of luajit
And dynamic languages ​​\u200b\u200bare often impossible transfer the code to native in advance so that this code itself does not turn into a certain bytecode interpreter, here is a well written about it:
stackoverflow.com/questions/15626611/can-regular-j...
(by the way, c# is non-dynamic, so as far as I remember, there was some kind of tool for precompiling into native code for it - but I've been a hundred years now ;-) I haven't written in c#, so for sure I do not remember)

A
afiskon, 2014-03-15
@afiskon

The idea is the following. The program is taken, translated into a platform-independent bytecode. Then, under a virtual machine, this bytecode can be run unchanged under both x86 Windows and x64 Linux. That is, the program does not have to be compiled for each platform separately. In fact, I may not even know about the existence of some platforms on which someone will run my program. Perhaps these platforms do not even exist yet.
In addition, when the virtual machine is upgraded, the bytecode can be compiled into more efficient machine code. And actually, even on two processors with the same architecture, very different optimizations can be applied, so there are actually many more platforms than the number of architectures * the number of OSes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question