Difference between revisions of "KOL"

From Lazarus wiki
m (Example)
(Introduction)
Line 20: Line 20:
 
<br>
 
<br>
 
This has several '''advantages''', some of which are: <br>
 
This has several '''advantages''', some of which are: <br>
* The size of your binary is very tiny, on average 10-80% smaller than a VCL/LCL binary, but a factor 100 is not unusual.
+
* The size of your binary is very tiny, on average 80% smaller than a VCL/LCL binary, but a factor 100 or more is not unusual.
 
* KOL binaries (both executables as dll's) have much much faster load times and feel snappier.
 
* KOL binaries (both executables as dll's) have much much faster load times and feel snappier.
 
* KOL is very easy to extend once you understand its concepts. It is a fully object oriented framework
 
* KOL is very easy to extend once you understand its concepts. It is a fully object oriented framework

Revision as of 15:21, 8 September 2014

Introduction

kolmck.png

The Key Objects Library - a.k.a. KOL - is an object oriented framework created by Vladimir Kladov (http://kolmck.net) in 2000.
It is the successor to the XCL library by the same author.
The current release as of March 2014 is 3.22.
New versions appear with Irregular intervals, but the library is currently maintained.

The main reason for creating this library is explained through the dissatisfaction of Vladimir and others towards ever increasing codebloat.
Nobody seemed to care anymore about *efficient* coding.
According to KOL users there a strong correlation between Moore's law and degrading code optimizing efficiency.

The framework centers around these basic concepts:

  • It uses - some say old school - pascal objects, not classes.
  • Most of the widgets are basically one and the same object. Polymorphism is conceptually through constructing functions.
  • Because of the latter you can chain commands, most of them are functions that return a pointer to Self.
  • It ignores the VCL/LCL, in fact it attempts to replace them.
  • If you can optimize for speed or size, do it! Large parts of KOL are available in both optimized Basm or pure Pascal equivalents.


This has several advantages, some of which are:

  • The size of your binary is very tiny, on average 80% smaller than a VCL/LCL binary, but a factor 100 or more is not unusual.
  • KOL binaries (both executables as dll's) have much much faster load times and feel snappier.
  • KOL is very easy to extend once you understand its concepts. It is a fully object oriented framework
  • The core is generally much faster than Free Pascal or Delphi code. It favors the stack, not the heap.
  • It is very feature rich. In many cases you won't miss the LCL/VCL, in some cases it surpasses them.


It also has very obvious disadvantages, some of which are:

  • KOL is a framework. It doesn't mix very well with classes and it can't be sensibly mixed with LCL/VCL code because you will loose the size advantage.
  • If you are not familiar with the old Object Pascal paradigm from Turbo Pascal the learning curve can be steep.
  • You will miss some functionality that has become common place, like RTTI, but most of these features are often codebloat and the reason why KOL exists in the first place.
  • Because you are often working on the same object with multiple appearances, it may be confusing that some methods or properties that you can see in the code editor do not apply.
  • Some people do not like command chaining. If not used properly it can become unreadable and, not least, hard to debug.
  • Because the size of the binaries, it is extremely popular in malware circles. You will run into the odd false positive when compiled with D5-D7. In that case please contact your anti-malware software producer.


Requirements

  • Free Pascal compiler 2.6.4 or later for Win32
  • Free Pascal compiler 2.7.1 or later for Win64
  • KOL 3.22 or later for win32
  • The Mirror Classes Kit a.k.a. MCK for those who want to develop using a visual designer like Lazarus or Delphi.
  • Release preview of KOL 3.22-64 for KOL win32/64
  • See the KOL CE page for win-ce development and requirements.
  • Not required, but recommended is a copy of FPC's RTL and/or Delphi's RTL sources

Supported targets

  • All 32-bit Windows: from Windows 95 to Windows 8.1.
  • All 64-bit x86_64 from Windows 2000 to Windows 8.1.
  • Windows CE based PocketPC and Smartphones. More information on the KOL CE page

Example

To show what KOL really means, here's a simple example. A form, a button and some action.
You will notice that the code resembles that of a LCL/VCL application and basically:
It does exactly the same!
The main difference is the use of objects and pointer to objects instead of classes and the use of constructing functions instead of constructors.

Here's the main program file:

program koldemo;
{This line makes us FPC and Delphi compatible}
{$IFDEF FPC}{$MODE DELPHI}{$H+}{$APPTYPE GUI}{$ENDIF}
uses
 Kol,
 koldemo1 in 'koldemo1.pas';

begin
 NewForm1( Form1, nil);
 Run(Form1.form);
end.


And here is the main unit, again, you will notice it is very similar to LC/VCL code:

unit koldemo1;

interface
uses
  Windows, Messages, Kol;
const
 cps:KolString = 'Kol project in ' + {$IFDEF FPC}'Freepascal '{$ELSE}'Delphi '{$ENDIF} +
    {$IFDEF WIN64}'64 bits '{$ELSE}'32 bits '{$ENDIF} +
    {$IFDEF UNICODE_CTRLS}'Unicode'{$ELSE}'Ansi'{$ENDIF};
type

PForm1=^TForm1;
TForm1=object(Tobj)
  Form, Button:pControl;{Note both widgets are of the same type}
public
  {The message handler for our Button}
  procedure ButtonClick(Sender:PObj); {The message handler for our Button}
end;

procedure NewForm1( var Result: PForm1; AParent: PControl );

var
  Form1:pForm1;

implementation

procedure NewForm1( var Result: PForm1; AParent: PControl );
begin
  New(Result,Create);
  with Result^ do
  begin
    {The appearance of a widget depends on its creating function}
    Form := NewForm(AParent,cps);
    Button := NewButton(Form,'&Click Me');
    {Attach the MessageHandler}
    Button.OnClick := ButtonClick;
    { The applet variable is the Application object
      Assigning the main for to it assigns the main message loop to the form.}
    Applet:=Form;
    Form.Add2AutoFree(Result);
  end;
end;

procedure TForm1.ButtonClick(Sender: PObj);
begin
   MsgOk('Ouch!');
end;

end.


So what's so special about it?
Well, for one, compile this and look at the executable size.
A similar VCL program with all optimizations on, form + Button + ShowMessage compiled with Delphi 7 is 393 Kilobytes and it gets worse for progressive newer versions of Delphi.
Compiled with Delphi this unoptimized KOL version renders an executable of just 23 Kilobytes or 23040 bytes with Delphi 7. That is almost 20 times smaller than the same VCL application!
Compiled with PPC386 2.7.1 it renders an executable of 48128 bytes. Compiled like:

ppc386 -Mdelphi -CX -XX -Xs -Os koldemo.dpr 

Compiled with PPCX64 2.7.1 for Windows it renders 65536 bytes (is this by accident?). Compiled like:

ppcx64 -Mdelphi -Rintel -CX -XX -Xs -Os koldemo.dpr


If you would like to compare that to Lazarus LCL or Delphi VCL/FireMonkey XE2 and later series of 64 bit frameworks, go ahead ;) You will be surprised.