Further development of KOL. We reduce everything we can. Replacing System.pas and other system modules
And he is no longer what he was in the beginning.
Other people's destinies, becoming his destiny,
They take him away ...
(Rilke)
Already at the initial stage of writing the library, I had the idea to cut the System.pas module as much as possible. If someone is not in the know, then this unit is, as it were, automatically added to the uses section of any unit, and it contains a set of functions that are needed practically (in fact, theoretically) always. And in particular, it contains the code that is responsible for handling exceptions, for allocating memory in the heap - the so-called Memory Manager - Memory Manager, functions for working with variants, with dynamic arrays, dynamic strings, etc.
Having obtained all possible information on the bottom of the barrel, I discovered that it is quite possible to write and "substitute" my own system module compiled with the help of the Delphi compiler itself. So I did this by doing this work for Delphi 5, the version I was using then (in fact, I still mainly use Delphi 5 when creating projects on KOL, with rare exceptions - when I need, for example, to use in assembler inserts MMX commands).
When creating my own version of system.pas, if possible, I didn't just "disable" and replace the standard methods with my own, but made them optional. These features are turned off by default, but there is always the option to turn them back on. For example, the standard memory manager is enabled by calling the UseDelphiMemoryManager procedure, the ability to work with console I / O is enabled by calling UseInputOutput, etc. Including a standard memory manager instead of my primitive adapter (wrapper) to Windows API functions (GlobalAlloc, GlobalFree, GlobalRealloc), which takes literally tens of bytes of code instead of several kilobytes, usually there is no need - unless the program requires constant work with allocating and reallocating memory in heap, for example, when dealing with dynamic ANSI strings.
Now, in almost any KOL project, it is enough to add a directory in the project options in the search path, which contains the compiled alternative modules system.dcu and others like it, and the program is immediately reduced by 9-11 Kilobytes. For a gigantic size of 300-400 Kilobytes of a typical application made in Delphi, this would not be too much of an effect, but for a KOL program with a size of up to 40-60 Kilobytes, this is already a very significant gain. (Later, various authors made adaptations of my rework of System.pas for other Delphi versions: 3, 4, 6, 7).
|
Note: to replace system modules, it is not necessary to actually replace those modules in the system libraries. Moreover, this way you will not replace anything. The supplied replacement files should be unpacked into a separate directory, and in the project options specify the path to this directory - this is the replacement. |
Note: to replace system modules, it is not necessary to actually replace those modules in the system libraries. Moreover, this way you will not replace anything. The supplied replacement files should be unpacked into a separate directory, and in the project options specify the path to this directory - this is the replacement.
Already doing this job of shortening system modules, I came across writing code in inline assembly. The PC assembler was not a particular problem for me, although before that I had never had to deal with it. I had to fill in some gaps in my education, and after gaining some experience in translating Pascal code into assembler, I decided to make an alternative asm version of the code for almost all KOL functions, which could be reduced at least a little by this.
As a result of both improvements made - replacement of system modules, and translation of most of the code into assembler, the size of the minimum KOL project with one visual form stopped at 13.5 Kilobytes, and the minimum console application of the form
Program P1; {$ APPTYPE CONSOLE}
begin
end.
decreased to 6 kilobytes (if you insert a call to ShowMessage with the parameter 'Hello, world! "- a standard test for the size of the generated code - then the size of such a program turns out to be 6.5K).
Correction: since version 2.39, the size of the minimal KOL application with one form has been reduced to 12.5K, the minimum DLL using KOL - to 6K, the minimal console application using KOL - to 5.5K. The compilation was done in Delphi6. In recent versions, an application with an empty form takes only 11.5K.
In addition, ahead of the events, the Collapse project was recently completed, which reduces the code by about half, even compared to rewriting it into assembler, (although the effect becomes noticeable only for fairly large applications - from approximately 40 Kilobytes). Collapse uses the translation of a part of the Pascal code into the P-code of a virtual Collapse machine, which is emulated during program execution. For this, the P-code is converted into byte-code using a specially created P-compiler. To complete this fantastic project, it remains only to write your own Pascal compiler, which could turn almost any Pascal code into P-code.