Optimise Lazarus for smart linking
Original Reporter info from Mantis: Flo
-
Reporter name: Florian Köberle
Original Reporter info from Mantis: Flo
- Reporter name: Florian Köberle
Description:
The result of this feature would be smaller executeables.
How to do it:
Remove all orders which need any Lazarus class from the initialization and finalization area. Replace all variables in the interface part with a function which returns the same class.
The order of the function is to check if it is its first call. In the Case of the first call this function does the work of the initialization, e. g. set a variable of that class in the implementation part and use it later for return value.
Finally we a way to remove the object when the application terminate. The function will simply add the created object to a list of TObject. Only this removing of the items in this list have to be executed in the finalization part(of an other unit).
Hint: It could be that more than one function need to check if it is already loaded and do the loading if it isn't.
Lets take the Unit Clipbrd as example.
Unit now it looks like this:
***************************
procedure InternalInit;
var
AClipboardType: TClipboardType;
begin
OnLoadGraphicFromClipboardFormat:=@LoadGraphicFromClipboardFormat;
OnSaveGraphicToClipboardFormat:=@SaveGraphicToClipboardFormat;
for AClipboardType:=Low(TClipboardType) to High(TClipboardType) do
FClipboards[AClipboardType]:=nil;
end;
procedure InternalFinal;
begin
FreeAllClipboards;
end;
initialization
InternalInit;
finalization
InternalFinal;
**************************
and
**************************
function Clipboard(ClipboardType: TClipboardType): TClipboard;
begin
if not Assigned(FClipboards[ClipboardType]) then
FClipboards[ClipboardType] := TClipboard.Create(ClipboardType);
Result := FClipboards[ClipboardType];
end;
function Clipboard: TClipboard;
begin
Result:=Clipboard(ctClipboard);
end;
**************************
First we move the initalisation Code into the Clipboard function:
New Code:
**************************
function Clipboard(ClipboardType: TClipboardType): TClipboard;
AClipboardType: TClipboardType;
begin
if not ClipboadLoaded then // a Boolean in the implemenation part; default: false
begin
// This must be done somewhere else:
// OnLoadGraphicFromClipboardFormat:=@LoadGraphicFromClipboardFormat;
// OnSaveGraphicToClipboardFormat:=@SaveGraphicToClipboardFormat;
for AClipboardType:=Low(TClipboardType) to High(TClipboardType) do
FClipboards[AClipboardType]:=nil;
end;
if not Assigned(FClipboards[ClipboardType]) then
begin
FClipboards[ClipboardType] := TClipboard.Create(ClipboardType);
// FreeByTerminate take care that the object will be removed in the end:
FreeByTerminateList.Add(FClipboards[ClipboardType]);
end;
Result := FClipboards[ClipboardType];
end;
function Clipboard: TClipboard;
begin
Result:=Clipboard(ctClipboard);
end;
***************************
I would help you doing that but I don't know how to get a member of the developer team.
Mantis conversion info:
- Mantis ID: 1057
- Target version: post 1.0