4D v16.3

Overview

Home

 
4D v16.3
Overview

Overview  


 

 

You can compile your application, i.e., translate all of your methods into machine language. Compiling a database lets you check the consistency of the code and accelerate its execution, as well as making it possible to protect the code in its entirety. Compilation is an indispensable step between the development of databases using 4D and their deployment as stand-alone applications. 

The compilation process is entirely automatic; however, compilation requires greater rigor when writing 4D code. The Compiler section of the 4D Language Reference manual provides advice and specific information concerning programming with a view to compilation. Furthermore, keep in mind that the compiler will indicate any programming errors and situate them in their context.

The computer is a device where commands are written using only “0”s and “1”s. This binary language is called machine language. The heart of the machine, the microprocessor, understands only this language. A program written in any high-level computer language (C, C++, SQL, Java, BASIC, 4D, and so on) is first translated into machine language, so as to be understandable to the computer’s microprocessor.

There are two ways to do this:

  • The statements can be translated during execution; the program is then said to be interpreted.
  • The statements are translated as a whole before program execution; the program is then said to be compiled.

When a series of statements is executed using an interpreter, the process can be broken down as follows:

  • The program reads a statement in the program’s own language,
  • It translates the statement into machine language,
  • It executes the statement.

This cycle is executed for each of the statements in the program. The program that handles the execution of this kind of cycle is called the interpreter. For a database in the process of development, 4D methods are interpreted.

A compiled program is translated in its entirety prior to execution. This process results in a new file that contains a set of statements in machine language. This set is saved for repeated use—the translation is performed only once and the compiled version of the program is available for repeated execution.

This phase is completely independent from any use of the program. The program that handles the translation is called the compiler.

The compiler in 4D compiles the database methods, project methods, triggers, form methods and object methods in your database. If you do not have any of these elements in an application, the compiler will have nothing to compile.

When you have successfully completed compilation, the use of the compiled database is identical to that of the original one.

The first benefit of compilation is, of course, speed of execution. There are two further benefits directly linked to compilation:

  • Systematic code checking,
  • Database and component protection.

The increased speed is due to two characteristics of compiled code: direct code translation, once and for all, and direct access to variable and method addresses.

  • Direct and final code translation
    The code of the methods written in 4D will be translated once and for all using the compiler. The time required in interpreted mode to translate all the statements is saved whenever you use a compiled database. Here is a simple case that illustrates this point. Take the case of a loop containing a sequence of statements that is repeated 50 times:
     For($i;1;50)
      `Sequence of statements
     End for

    In an interpreted database, each statement in the sequence is translated 50 times. Using the compiler eliminates the translation phase for each statement. For every statement in the sequence, we save 50 translations.
  • Direct access to variable and method addresses
    In interpreted databases, variables are accessed through a name. Therefore, 4D must access the name in order to obtain the variable’s value.
    In the compiled code, the compiler attaches an address to each variable, writes the variable’s address directly in the code, and goes directly to that address whenever necessary.
    Notes:
    - Operations requiring disk access may not be affected because their speed of execution is limited by the rate of transmission between the computer and its peripherals (drive or hard disk).
    - Comments are not translated so they do not appear in the compiled code. Therefore, comments do not affect the execution time in compiled mode.

The compiler also operates as a syntax checker for your databases. It systematically checks your code and notes possible ambiguities, whereas 4D only does this when the method is executed.

Suppose that one of your methods contains a series of tests as well as a sequence of statements to be executed. It is unlikely that you would fully test for all cases if the number of tests was very large. Therefore, a syntax error in an untested case might not show up until an end user encounters the case.

This sort of problem is avoided when you use a compiled database. When you compile a database, the compiler scans the entire database and analyzes each statement. The compiler detects any abnormality and generates an error message or warning.

Once you have compiled your database, you can use the application builder to erase the interpreted code. In this case, access to the Design environment (except for records) is blocked. For more information about the application builder, refer to the Finalizing and deploying final applications chapter.

In a compiled database, the commands related to development are disabled. 

When a compiled component is installed into a host database, the shared project methods are accessible in the Explorer and can be called in methods of the host database but their contents do not appear in the preview area nor in the debugger. The other project methods of the component never appear. For more information about components, refer to the Developing and installing 4D components chapter.

The benefits are:

  • The database development cannot be modified, intentionally or by accident,
  • Your methods are now protected.

A compiler is integrated into 4D. Database compilation is carried out using the following dialog box:

It is also possible to launch compilation directly using the current settings via the Start Compilation command found in the Design menu and in the menu associated with the “Compiler” button of the tool bar.

Compilation is carried out in keeping with generic compilation options, set on the Compiler page of the Database Settings.

Once the database is compiled, it is still possible to switch from interpreted mode to compiled mode, and vice versa, at any time using the Restart Interpreted and Restart Compiled commands of the Run menu, without having to quit the 4D application — except when the interpreted code has been erased (see the previous section). 

The Open database dialog box also allows the choice of interpreted or compiled mode on startup of the database (see Open dialog box options). 

If you modify the structure of your database in interpreted mode, you must recompile it in order to have them taken into account in compiled mode. 

When you switch from one mode to the other, 4D closes the current mode and opens the new one. This amounts to exiting then reopening the application. Consequently, each time you change from one mode to another, 4D executes the two following database methods (if specified) in this order: On Exit database method -> On Startup database method

 
PROPERTIES 

Product: 4D
Theme: Compilation

 
HISTORY 

 
ARTICLE USAGE

4D Design Reference ( 4D v16)
4D Design Reference ( 4D v16.1)
4D Design Reference ( 4D v16.3)