Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #39939

Re: Loop optimisation.

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Loop optimisation.
Date 2021-04-14 18:00 +0200
Organization PointedEars Software (PES)
Message-ID <2144986.iZASKD2KPV@PointedEars.de> (permalink)
References <s4snvd$cr0$1@dont-email.me> <86e3bc08-8877-45ce-9881-519d0a41b9c0n@googlegroups.com> <7904310.T7Z3S40VBb@PointedEars.de> <1110f942-cc14-45b2-966b-a4442461f170n@googlegroups.com> <9878253.nUPlyArG6x@PointedEars.de>

Show all headers | View raw


Thomas 'PointedEars' Lahn wrote:

> Michael Haufe (TNO) wrote: 
>> On Tuesday, April 13, 2021 at 7:20:42 PM UTC-5, Thomas 'PointedEars' Lahn
>> wrote:
>>> Michael Haufe (TNO) wrote:
>>> > Modern JavaScript engines use JIT compilation.
>>> AFAIK, they always did. What is new with “modern JavaScript engines”,
>>> IOW Google V8 (JavaScript), is only that the source code is compiled to
>>> native machine code instead of bytecode for a virtual machine.
>> 
>> Afraid not. They were all interpreters
>> 
>> - Firefox 3.1 was the first browser to introduce a JIT in version 3.1
>> called TraceMonkey [1] -
> 
> No, certainly not.  The wording “a new kind of Just-In-Time (JIT)
> compiler” does not imply that previous script engines interpreted source
> code.

This should clarify your misconception:

<https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Internals/Bytecodes>

See also: 

<https://softwareengineeringdaily.com/2018/10/03/javascript-and-the-inner-workings-of-your-browser/#:~:text=The%20JavaScript%20engine%20is%20the,MacOS%2C%20Windows%2C%20or%20Linux.>

| [00:03:39] JM: Mathias Bynens, you are a software engineer at Google
| working on developer advocacy on the V8 team. Welcome to Software
| Engineering Daily.
| 
| […]
| 
| Give me a high-level perspective for the steps that are happening as this
| webpage with some various forms of JavaScript opens up.
| 
| [00:04:26] MB: […]
| 
| So the first thing that happens is the JavaScript source code goes into
| the JavaScript [engine’s] parser, which then takes that code and turns
| into an object-based representation, which we call an abstract syntax tree
| or an AST. Armed with AST, we can do some transformations on the code, do
| some [inaudible 00:05:06] here and there, but mainly the reason to have
| this AST is so that we can feed it into the first phase of execution,
| which is the interpreter.
| 
| So our interpreter in V8, it’s called Ignition. This interpreter generates
                                                       ^^^^^^^^^^^^^^^^^^^^^
| bytecode, which then runs in the interpreter. At that point we’re actually
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Since this is actually *compilation* (conversion of human-readable source 
code to machine-readable code), that part of the “interpreter” is actually
technically a _compiler_.

And since the machine-readable code is not “interpreted“ by the CPU 
firmware, but by a software, the "machine" that executes/“interprets” it is 
called a *virtual machine*.

IOW: a JIT-compiler converts source code to bytecode to be 
executed/“interpreted” by a virtual machine.

| running the JavaScript already, which is great. It’s like all we have to
| do is sparse it and then generate some bytecode in the interpreter, which
| can happen pretty quickly. So that’s great for a code that only needs to
| run once. For example, a lot of code on the web is code that initializes
| the webpage and you don’t run that in a loop very open, right? You only
| run it once. You load the page. So all that magic can happen in the
| interpreter with the bytecode that we generate.
| 
| But that’s not the end of the story, because sometimes code gets repeated
| quite often or we see that the same code paths get hit over and over and
| over again, and that point it might make sense to start optimizing those
| specific functions, right?
| 
| That’s why while we’re running the bytecode in the interpreter, JavaScript
| engines – Well, V8 does this, but all JavaScript engines basically do
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| this. […].
`----

JIT-compilation of source code to bytecode to be executed by a virtual 
machine is nothing unusual for so-called "interpreted languages".  For 
example, the same happens with Perl and Python code since time immemorial.

Unfortunately, there is a lot of confusion about this (on the Nets).

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Loop optimisation. The Natural Philosopher <tnp@invalid.invalid> - 2021-04-10 18:41 +0100
  Re: Loop optimisation. John G Harris <niam@jghnorth.org.uk.invalid> - 2021-04-10 19:14 +0100
    Re: Loop optimisation. The Natural Philosopher <tnp@invalid.invalid> - 2021-04-11 11:33 +0100
      Re: Loop optimisation. John G Harris <niam@jghnorth.org.uk.invalid> - 2021-04-11 15:03 +0100
        Re: Loop optimisation. "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-04-11 07:38 -0700
          Re: Loop optimisation. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2021-04-11 13:36 -0700
          Re: Loop optimisation. John G Harris <niam@jghnorth.org.uk.invalid> - 2021-04-12 18:05 +0100
            Re: Loop optimisation. The Natural Philosopher <tnp@invalid.invalid> - 2021-04-12 23:16 +0100
              Re: Loop optimisation. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2021-04-13 05:19 -0700
                Re: Loop optimisation. "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-04-13 06:21 -0700
        Re: Loop optimisation. The Natural Philosopher <tnp@invalid.invalid> - 2021-04-12 01:36 +0100
          Re: Loop optimisation. John G Harris <niam@jghnorth.org.uk.invalid> - 2021-04-12 18:04 +0100
            Re: Loop optimisation. The Natural Philosopher <tnp@invalid.invalid> - 2021-04-12 23:09 +0100
              Re: Loop optimisation. John G Harris <niam@jghnorth.org.uk.invalid> - 2021-04-13 20:08 +0100
                Re: Loop optimisation. The Natural Philosopher <tnp@invalid.invalid> - 2021-04-13 21:41 +0100
                Re: Loop optimisation. John G Harris <niam@jghnorth.org.uk.invalid> - 2021-04-14 18:24 +0100
  Re: Loop optimisation. Kristjan Robam <he12457@hotmail.com> - 2021-04-10 13:36 -0700
    Re: Loop optimisation. The Natural Philosopher <tnp@invalid.invalid> - 2021-04-11 11:34 +0100
  Re: Loop optimisation. "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-04-10 22:22 -0700
    Re: Loop optimisation. The Natural Philosopher <tnp@invalid.invalid> - 2021-04-11 11:36 +0100
    Re: Loop optimisation. Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-04-14 02:20 +0200
      Re: Loop optimisation. "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-04-14 06:48 -0700
        Re: Loop optimisation. Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-04-14 17:31 +0200
          Re: Loop optimisation. Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-04-14 18:00 +0200
            Re: Loop optimisation. The Natural Philosopher <tnp@invalid.invalid> - 2021-04-14 19:25 +0100
            Re: Loop optimisation. Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-04-14 23:10 +0200
              Re: Loop optimisation. "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-04-17 09:45 -0700
            Re: Loop optimisation. "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-04-17 09:43 -0700
              Re: Loop optimisation. Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-04-17 21:45 +0200
                Re: Loop optimisation. Scott Sauyet <scott.sauyet@gmail.com> - 2021-04-18 19:06 -0700
          Re: Loop optimisation. "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-04-17 08:50 -0700

csiph-web