Recent

Author Topic: Optimization  (Read 16523 times)

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Optimization
« Reply #15 on: July 18, 2011, 08:05:06 pm »
Maybe would be good to have CheckBox in Tools->Options...->CodeTools->Class Completion->Property Completin (GroupBox): [ - ] Pass parameter as const
CheckBox can be off by default.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Shebuka

  • Sr. Member
  • ****
  • Posts: 429
Re: Optimization
« Reply #16 on: July 19, 2011, 10:15:20 am »
Regarding const, I always use it when I'm sure the parameters shouldn't be modified and I pass big thing (array / records). That way the compiler will pass it by reference so there's no copy penalty. The case Martin showed rather seldom happens IMHO.
Also i was thinking so, but yesterday before read this discussion, i wrote this procedure:

Code: [Select]
procedure TFrm_Search.ManageExpand(const AExpand: Boolean);
begin
  if AExpand then
  begin
    GB_Search.Height := 200;
    FAdvancedSearch := True;
    Edt_SearchLimit.Enabled := True;
    Edt_Nome.Enabled := True;
  end
  else
  begin
    GB_Search.Height := 100;
    FAdvancedSearch := False;
    Edt_SearchLimit.Enabled := False;
    Edt_Nome.Enabled := False;
  end
end;
That was intended to be used in this way:
Code: [Select]
procedure TFrm_Search.FormShow(Sender: TObject);
begin
  Edt_SearchLimit.Caption := '';
  Edt_Nome.Caption := '';
  ManageExpand(False);
end;

procedure TFrm_Search.Lbl_ExpandSearchBoxClick(Sender: TObject);
begin
  if FAdvancedSearch then
    ManageExpand(False)
  else
    ManageExpand(True);
end;

This was Ok until i'v optimized the second procedure as follow: (btw is it really optimize it?)
Code: [Select]
procedure TFrm_Search.Lbl_ExpandSearchBoxClick(Sender: TObject);
begin
  ManageExpand(not FAdvancedSearch);
end;
and this is exactly the example Martin_fr give to us... so pay attention.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12180
  • Debugger - SynEdit - and more
    • wiki
Re: Optimization
« Reply #17 on: July 19, 2011, 10:39:46 am »
This code will still work.
You do not (currently) break the promise of not to modify the local var.

Boolean is passed by value. And it is highly unlikely that a platform will ever exist, for which this will not be the case. (e.g. a platform where "sizeof(boolean) > sizeof(pointer)")


Besides:
not FAdvancedSearch
is an expression, you are passing the result of the expression, not the variable.
So even if bool was passed by ref, it would still work.

But you do have a point, it is easy to get to a point where you break the promise not to chane the parameter.
« Last Edit: July 19, 2011, 10:43:19 am by Martin_fr »

Ask

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 687
Re: Optimization
« Reply #18 on: July 19, 2011, 03:52:47 pm »
Regarding const, I always use it when I'm sure the parameters shouldn't be modified and I pass big thing (array / records). That way the compiler will pass it by reference so there's no copy penalty. The case Martin showed rather seldom happens IMHO.

As soon as FPC 2.6.0 if out, I shall start using "constref" for this.

 

TinyPortal © 2005-2018