Coder Social home page Coder Social logo

isolate's Introduction

HertzScript Isolate

Enables parallel multitasking in V8 using HertzScript coroutines.

TODO: This is a draft, it must be improved.

Project Synopsis

HertzScript Isolate is a JavaScript M:N threading system implemented on top of the HertzScript programming environment, and allows programs to execute concurrently and/or in parallel depending on the quantity of available processors. The main motivation of HertzScript Isolate is to generate a standard multi-CPU parallel computing system and library for JavaScript. HertzScript (Hz) is the foundational platform for this project, and is a concurrent programming environment which implements userspace green threads; in other words, it can compile current standard JavaScript source code to be fully re-entrant, and is able to execute code with a higher degree of concurrency in run-times that do not support it. The current reference implementation of Hz (written in JavaScript) is a source-to-source/transpiler compiler based on Babel.

The primary goal of Hz Isolate is to be able to analyze JavaScript source code both statically and dynamically to facilitate a parallel task scheduler, provide automatic parallelization, and provide an API of primitives to existing run-times that do not support parallelism.

Use-Cases

Developers can create JavaScript applications which function similarly to those of Golang by linking the discrete components of their applications via channels, thereby allowing them to run concurrently and in parallel without any hassle or complicated interfaces. NodeJS and web browsers use a deferred work queue for concurrency by default, but it can often be insufficient for some systems as JavaScript is still executed in a single thread; we want to combine both operations by executing JavaScript in parallel with I/O throughput. Our main motivation is to allow existing code to be executed efficiently in FaaS (Function as a Service) environments or high intensity computation applications.

Some common use-cases of HertzScript Isolate are the following:

  • Improving the default capabilities of HertzScript by providing parallel execution, thus allowing a real M:N threading model.
  • Communicating Sequential Processes (like Golang).
  • Concurrent and parallel execution of JavaScript programs.
  • Calculating multiple Matrices across all available processors.
  • Video game engines, as mixing I/O with concurrent/parallel programming can simplify the design of video games.
  • Responsive (non-blocking) UX which runs independently of CPU-bound functions or underlying technology, as the event loop can never be blocked.

Enabling Functionalities

Dynamic Hooks

A feature of HertzScript which aids in parallel computing is that it dynamically implements multiple function call hooks at run-time; this property is very important and allows Hz to generate JavaScript source code that it is able to be executed, stopped, and paused/resumed arbitrarily. A hook is additional functionality that is applied statically to source code and/or applied dynamically to a program that is already executing; for example a debugger like GCC (static) or Counter Strike AMX-MOD (dynamic). Hz is able to hook into all function calls by using its compiler to statically detour each step (function call) of the source code into Hz at run-time, thus making them available for dynamic hooks and extensions. By utilizing the hook/detour mechanisms in Hz, we can begin to implement new abstract features such as better stack traces, dynamic analysis, tail-call optimization, reflective meta-programming techniques, and automatic parallelization.

V8 Isolates

Reference about the implementation in V8: https://stackoverflow.com/a/44359082

Latest improvements have enabled JavaScript to provide basic parallelism thanks to the V8 multiple-Isolate technique. V8 has an internal structure called Isolate which represents the context of executing JavaScript source code. In its current state, HertzScript effectively generates re-entrant code that can be used as green threads. For example, what the class Thread of CRuby (MRI) implements, but the implementation has some differences. The CRuby (MRI) implementation is very similar to CPython in many aspects but especially in regard to thread synchronization. Ruby and Python both use a common global mutex which prevents race conditions by blocking execution of their interpreters, as opposed to the Java Virtual Machine (JVM) which is designed to be non-blocking and atomic.

In the case of V8 we used to have similar limitations which have now been removed via the V8 multi-Isolate technique. Since version (TODO), V8 implemented support for multi-Isolate environments. This gives to us a better solution for parallel computing compared to Ruby and Python, but at the same time it gives us a very low level API which is difficult to use from the point of view of the user. HertzScript Isolate mixes HertzScript userspace green threads with the V8 multi-Isolate technique, allowing one JavaScript program to be executed by multiple processors simultaneously. An alternative to this is to make V8 completely atomic like the JVM, but this is not an option right now due to the current state of the art, so we provide an intermediate solution for JavaScript (and other languages) for achieving these capabilities.

isolate's People

Contributors

floofies avatar viferga avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

isolate's Issues

Elaborate a base strategy to implement a parallel scheduler.

TODO: This is a draft, it must be improved.

The main motivation of Hz/Isolate is to generate a standard multi-core parallel library for HertzScript. HertzScript implements green threads, in other words, in the nowadays JS implementation (09/15/19) it can compile existing code into a re-entrant version of it. Thanks to this capability, Hz is able to execute code concurrently in run-times that does not support it. Hz is a specification, but in order to illustrate this example, Hz current implementation (based on JavaScript) is a compiler (transpiler) based on Babel. Thanks to this Hz is able to transform all calls into a re-entrant version of it. This means, by means of a transpiler plugin Hz is able to hook into all your function calls, this mean, each step (function call) of your code is tracked (detoured) into Hz. So adding a compilation step to your code, with Hz we are able to stop the execution and resume it whenever we need.

This property is very important. It allows us to implement multiple hooks. A hook is an additional functionality applied to a code that already exists. For example, a debugger like GCC (dynamic) or Counter Strike AMX-MOD (static). After using Hz compiler, we can generate a JavaScript code that it is able to be executed, stopped, and resumed arbitrarily.

Thanks to this mechanism, we can begin to implement different behaviors. For example, Hz is focused on concurrent execution (green threads) but we can also hook functions in order to generate better stack traces, run-time code analysis or meta-programming techniques.

At the current state, Hz effectively generates re-entrant code that can be used as green threads. For example, what the class Thread of CRuby (MRI) implements, but the implementation has some differences. CRuby (MRI) implementation is very similar to CPython, in many aspects but specially at Threading level. Ruby and Python use a common global mutex that prevents race conditions from execution by blocking the whole Interpreter (as a difference to JVM that is designed to be atomic). In the case of V8 we used to have a similar limitation, latest improvements lead us to a basic way of parallelism (thanks to Multi-Isolate). V8 has an internal structure called Isolate, which in a few words, represents the context of the execution of your JS code. Since version (TODO), V8 implemented support for multi-isolate environments. This gives to us a better solution in terms of parallelism compared to Ruby green-threads, but at the same time it gives us a very low level API, difficult to use from the point of view of the user.

HzIsolate tries to mix both worlds, Hz green threads specification, with V8 Multi-Isolate implementation, allowing multiple tasks to be executed by different threads arbitrarily. This is a similar model that GoLang implements. It is a N:M stack-less/re-entrant design which allows to execute concurrent code in parallel, depending on the real OS threads.

Apart from current limitations, the final target of HzIsoleate is to be able to analyze the current code statically, and also, introduce a run-time, in order to execute existing code in a parallel way if it is possible, or provide an API of primitives to existing run-times that does not support parallelism as a standard library.

Talking about JavaScript, common use cases of Hz (without Isolate) are the following:

  • Real time tracking of a resource (telemetry, advanced debugging, control, meta-programming).
  • Concurrent execution of JavaScript code within a single thread execution:
    • Calculating two Matrix concurrently of a WebGL application (executing JavaScript operations concurrently).
    • Web Game Engines (mixing I/O with concurrent programming can simplify the design of Web Games).
    • Responsive (non-blocking) UX (independently of the user code or underlying technology (even with the Event Loop blocked, the application can continue)).

With the Isolate module (plugin), we are focused on improve default capabilities of Hz by providing parallel execution over concurrency, thus allowing a real N:M model and exploding the performance of the cores.

With this model we can create similar applications as GoLang, but not only mixing the asynchronous I/O but also, allow computed based applications to be executed in parallel. NodeJS (or browsers) use a thread pool as a deferred work. The benefit of this technique is that it allows to safely execute operations in parallel, but it is not enough. Our JavaScript code will be executed in a single thread. We want to combine both operations, execute JavaScript code in parallel and explode the I/O world. Our main motivation is to allow existing code to be executed efficiently in environments FaaS (Function as a Service) like, or high intensive computation applications.

An alternative to this application is to make V8 completely atomic, like JVM does. But this is not an option right now due to the current state of the art. So we provide an intermediate solution for JavaScript (and other languages) for achieving these capabilities.

Reference about the implementation in V8: https://stackoverflow.com/a/44359082

Review & Edit README.md

Some action items for the readme:

  • Add V8 Version which first provided multi-isolate.
  • Better clarify the relation of GCC/AMX-MOD to Hz (dynamic/static?)
  • Anything else like spelling/grammar.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.