Table of Contents

5.5 Just-In-Time-Compiler

As you may know, Gambas had a JIT (Just-In-Time) component based on LLVM (Low-Level-Virtual-Machine) - a library of programs for building highly optimized compilers, optimizers, and runtime environments. The old JIT-compiler no longer worked because newer versions of the LLVM are not backward compatible. Therefore, Benoît Minisini decided to write a new JIT-compiler from scratch which compiles Gambas code into C.

The new JIT-compiler is implemented using the gb.jit component in Gambas version 3.12.0. It is loaded automatically when required. It has a C part that implements the GambasToC_Code translator and a Gambas part that deals with the extraction of Gambas source code from executable archive files and the compilation of all translations in one large file.

This is how the Gambas JIT compiler works:

The first time a function marked with the FAST keyword is called, the JIT compiler is called.

If not already done, the JIT compiler translates all fast methods of the current project (or component) into a large C source file.

The C compiler is then called to create a shared library from this large C source file.

As long as the C standard does not change, the JIT compiler will work properly.

with the disadvantage that code generation is slower (and with gcc even slower) than the old JIT compiler.

5.5.1 Syntax

The syntax with FAST has not changed: Place the keyword FAST at the beginning of a Gambas class file for a class that is to provide all functions JIT-compiled instead of interpreted, or at the beginning of a function declaration so that only this function is jit-compiled.

The new keyword, UNSAFE, has been introduced, which is only used together with the keyword FAST. When a function is declared as “FAST UNSAFE”, it is jit-compiled, but all security checks are removed (null objects, out-of-array bounds, division by zero, …)! It allows you to speed up the code a little if you are sure it is declared correctly. Otherwise, you will get a memory and/or segmentation error.

5.5.2 Notes

Try to apply the JIT compiler only to certain methods that require a performance boost.

The greatest speed increases will occur with functions that use many low-level calculations and control procedures, such as functions that use many mathematical operations.

However, if the function mainly calls other libraries, you will not notice a large increase in speed.

You will not get any profit in string editing.

5.5.3 Debugging

These environment variables GB_NO_JIT, GB_JIT_DEBUG, GB_JIT_CC and GB_JIT_CFLAGS control the behavior of the JIT compiler in Gambas Version 3.12.0. The following applies

GB_NO_JIT: Set the value to 1 if you want to deactivate the JIT compiler completely.

GB_JIT_DEBUG: Set the value to 1 if you want to see the debugging messages from the JIT compiler.

GB_JIT_CC: Sets the name of the compiler to use. By default, gcc is used. But you can set clang or other C compilers instead - provided it uses the same options as gcc or the support for it is added in the JIT compiler gambas part.

GB_JIT_CFLAGS: Sets the JIT compiler to the flags used to compile the C code. The default value is -O3.

5.5.4 Software test

If you use a stable Gambas and the JIT compiler in your programs, then you are surely interested in the advantage this has for your programs. Tested program runtimes can be found at http://gambaswiki.org/wiki/doc/benchmark. The following adapted Gambas script achieved very different runtimes: