optimized entities-connection performances at startup

This commit is contained in:
Basile Burg 2014-12-09 04:54:51 +01:00
parent dce827ef65
commit 4e15bdcd15
12 changed files with 49 additions and 27 deletions

View File

@ -296,7 +296,7 @@
<ResourceBaseClass Value="Form"/>
<UnitName Value="ce_staticexplorer"/>
</Unit23>
<Unit24>
<Unit24>
<Filename Value="..\src\ce_staticmacro.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="ce_staticmacro"/>

View File

@ -6,8 +6,8 @@ uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Interfaces, Forms, lazcontrols, runtimetypeinfocontrols, ce_libman, ce_tools,
ce_dcd, ce_observer, ce_main, ce_writableComponent, ce_options, ce_symstring,
Interfaces, Forms, lazcontrols, runtimetypeinfocontrols, ce_observer, ce_libman,
ce_tools, ce_dcd, ce_main, ce_writableComponent, ce_options, ce_symstring,
ce_staticmacro;
{$R *.res}

View File

@ -55,10 +55,7 @@ begin
inherited;
Enabled := exeInSysPath('cdb');
if Enabled then
begin
EntitiesConnector.addObserver(self);
EntitiesConnector.endUpdate;
end;
end;
destructor TCECdbWidget.destroy;

View File

@ -90,7 +90,6 @@ begin
end;
//
EntitiesConnector.addObserver(self);
EntitiesConnector.endUpdate;
end;
destructor TCEEditorWidget.destroy;

View File

@ -152,7 +152,6 @@ begin
btnClearCat.OnClick := @actClearCurCatExecute;
//
EntitiesConnector.addObserver(self);
EntitiesConnector.endUpdate;
end;
destructor TCEMessagesWidget.destroy;

View File

@ -16,18 +16,26 @@ type
private
fObservers: TObjectList;
fSubjects: TObjectList;
fUpdating: boolean;
fUpdatesCount: Integer;
procedure tryUpdate;
procedure updateEntities;
function getIsUpdating: boolean;
public
constructor create;
destructor destroy; override;
//
// forces the update, fixes begin/add pair error or if immediate update is needed.
procedure forceUpdate;
// entities will be added in bulk, must be followed by an enUpdate().
procedure beginUpdate;
// entities has ben added in bulk
procedure endUpdate;
// add/remove entities, update is automatic
procedure addObserver(anObserver: TObject);
procedure addSubject(aSubject: TObject);
procedure removeObserver(anObserver: TObject);
procedure removeSubject(aSubject: TObject);
// should be tested before forceUpdate()
property isUpdating: boolean read getIsUpdating;
end;
(**
@ -71,6 +79,9 @@ var
implementation
uses
LCLProc;
{$REGION TCEEntitiesConnector --------------------------------------------------}
constructor TCEEntitiesConnector.create;
begin
@ -85,11 +96,33 @@ begin
inherited;
end;
function TCEEntitiesConnector.getIsUpdating: boolean;
begin
exit(fUpdatesCount > 0);
end;
procedure TCEEntitiesConnector.tryUpdate;
begin
{$IFDEF DEBUG}
if fUpdatesCount > 0 then
DebugLn('saved uselless update in TCEEntitiesConnector')
else
DebugLn('efficient update in TCEEntitiesConnector');
{$ENDIF}
if fUpdatesCount <= 0 then
updateEntities;
end;
procedure TCEEntitiesConnector.forceUpdate;
begin
updateEntities;
end;
procedure TCEEntitiesConnector.updateEntities;
var
i,j: Integer;
begin
fUpdating := false;
fUpdatesCount := 0;
for i := 0 to fSubjects.Count-1 do
begin
if not (fSubjects[i] is ICESubject) then
@ -104,20 +137,21 @@ end;
procedure TCEEntitiesConnector.beginUpdate;
begin
fUpdating := true;
fUpdatesCount += 1;
end;
procedure TCEEntitiesConnector.endUpdate;
begin
updateEntities;
fUpdatesCount -= 1;
tryUpdate;
end;
procedure TCEEntitiesConnector.addObserver(anObserver: TObject);
begin
if fObservers.IndexOf(anObserver) <> -1 then
exit;
fUpdating := true;
fObservers.Add(anObserver);
tryUpdate;
end;
procedure TCEEntitiesConnector.addSubject(aSubject: TObject);
@ -126,25 +160,25 @@ begin
exit;
if fSubjects.IndexOf(aSubject) <> -1 then
exit;
fUpdating := true;
fSubjects.Add(aSubject);
tryUpdate;
end;
procedure TCEEntitiesConnector.removeObserver(anObserver: TObject);
var
i: Integer;
begin
fUpdating := true;
fObservers.Remove(anObserver);
for i := 0 to fSubjects.Count-1 do
if fSubjects[i] <> nil then
(fSubjects[i] as ICESubject).removeObserver(anObserver);
tryUpdate;
end;
procedure TCEEntitiesConnector.removeSubject(aSubject: TObject);
begin
fUpdating := true;
fSubjects.Remove(aSubject);
tryUpdate;
end;
{$ENDREGION}
@ -153,13 +187,11 @@ constructor TCECustomSubject.create;
begin
fObservers := TObjectList.create(false);
EntitiesConnector.addSubject(Self);
EntitiesConnector.endUpdate;
end;
destructor TCECustomSubject.destroy;
begin
EntitiesConnector.removeSubject(Self);
EntitiesConnector.endUpdate;
fObservers.Free;
Inherited;
end;
@ -200,6 +232,7 @@ end;
initialization
EntitiesConnector := TCEEntitiesConnector.create;
EntitiesConnector.beginUpdate;
finalization
EntitiesConnector.Free;
EntitiesConnector := nil;

View File

@ -53,7 +53,6 @@ begin
Grid.OnEditorFilter := @GridFilter;
//
EntitiesConnector.addObserver(self);
EntitiesConnector.endUpdate;
end;
destructor TCEProjectConfigurationWidget.destroy;

View File

@ -83,7 +83,6 @@ begin
Tree.PopupMenu := contextMenu;
//
EntitiesConnector.addObserver(self);
EntitiesConnector.endUpdate;
end;
destructor TCEProjectInspectWidget.destroy;

View File

@ -92,7 +92,6 @@ begin
fReplaceMru:= TMruList.Create;
//
EntitiesConnector.addObserver(self);
EntitiesConnector.endUpdate;
end;
destructor TCESearchWidget.Destroy;

View File

@ -86,7 +86,7 @@ type
implementation
{$R *.lfm}
uses ce_libman, ce_symstring;
uses LCLProc, ce_libman, ce_symstring;
{$REGION Standard Comp/Obj------------------------------------------------------}
constructor TCEStaticExplorerWidget.create(aOwner: TComponent);
@ -138,7 +138,6 @@ begin
Tree.PopupMenu := contextMenu;
//
EntitiesConnector.addObserver(self);
EntitiesConnector.endUpdate;
end;
destructor TCEStaticExplorerWidget.destroy;
@ -513,7 +512,7 @@ begin
if ndCat = nil then
begin
{$IFDEF DEBUG}
writeln(memb.Items[i].GetPath('kind').AsString);
DebugLn(memb.Items[i].GetPath('kind').AsString);
{$ENDIF}
continue;
end;

View File

@ -89,7 +89,6 @@ begin
updateCompletor;
//
EntitiesConnector.addObserver(Self);
EntitiesConnector.endUpdate;
end;
destructor TCEStaticEditorMacro.destroy;

View File

@ -128,7 +128,6 @@ begin
PopupMenu := contextMenu;
EntitiesConnector.addObserver(self);
EntitiesConnector.endUpdate;
end;
destructor TCEWidget.destroy;