Recent

Author Topic: [SOLVED] Improved CPU cache usage at procedure TFPCustomImage.Assign  (Read 842 times)

lagprogramming

  • Sr. Member
  • ****
  • Posts: 407
packages/fcl-image/src/fpimage.inc has procedure TFPCustomImage.Assign(Source: TPersistent);
Code: Pascal  [Select][+][-]
  1. Procedure TFPCustomImage.Assign(Source: TPersistent);
  2.  
  3. Var
  4.   Src : TFPCustomImage;
  5.   X,Y : Integer;
  6.  
  7. begin
  8.   If Source is TFPCustomImage then
  9.     begin
  10.     Src:=TFPCustomImage(Source);
  11.     // Copy extra info
  12.     FExtra.Assign(Src.Fextra);
  13.     // Copy palette if needed.
  14.     SetSize(0,0); { avoid side-effects in descendant classes }
  15.     UsePalette:=Src.UsePalette;
  16.     If UsePalette then
  17.       begin
  18.       Palette.Count:=0;
  19.       Palette.Merge(Src.Palette);
  20.       end;
  21.     // Copy image.
  22.     SetSize(Src.Width,Src.height);
  23.     If UsePalette then
  24.       For x:=0 to Src.Width-1 do
  25.         For y:=0 to src.Height-1 do
  26.           pixels[X,Y]:=src.pixels[X,Y]
  27.     else
  28.       For x:=0 to Src.Width-1 do
  29.         For y:=0 to src.Height-1 do
  30.           self[X,Y]:=src[X,Y];
  31.     end
  32.   else
  33.     Inherited Assign(Source);
  34. end;

The following patch improves CPU cache usage by switching the x and y loops. In addition, some cosmetics: two empty lines have been removed, "Var" has been changed to "var" and "[X,Y]" has been changed to "[x,y]".
Code: Pascal  [Select][+][-]
  1. diff --git a/packages/fcl-image/src/fpimage.inc b/packages/fcl-image/src/fpimage.inc
  2. index 3e2075900d..54be93074c 100644
  3. --- a/packages/fcl-image/src/fpimage.inc
  4. +++ b/packages/fcl-image/src/fpimage.inc
  5. @@ -460,11 +460,9 @@ procedure TFPCustomImage.CheckIndex (x,y:integer);
  6.  end;
  7.  
  8.  Procedure TFPCustomImage.Assign(Source: TPersistent);
  9. -
  10. -Var
  11. +var
  12.    Src : TFPCustomImage;
  13. -  X,Y : Integer;
  14. -
  15. +  x,y : Integer;
  16.  begin
  17.    If Source is TFPCustomImage then
  18.      begin
  19. @@ -482,13 +480,13 @@ procedure TFPCustomImage.CheckIndex (x,y:integer);
  20.      // Copy image.
  21.      SetSize(Src.Width,Src.height);
  22.      If UsePalette then
  23. -      For x:=0 to Src.Width-1 do
  24. -        For y:=0 to src.Height-1 do
  25. -          pixels[X,Y]:=src.pixels[X,Y]
  26. +      For y:=0 to src.Height-1 do
  27. +        For x:=0 to Src.Width-1 do
  28. +          pixels[x,y]:=src.pixels[x,y]
  29.      else
  30. -      For x:=0 to Src.Width-1 do
  31. -        For y:=0 to src.Height-1 do
  32. -          self[X,Y]:=src[X,Y];
  33. +      For y:=0 to src.Height-1 do
  34. +        For x:=0 to Src.Width-1 do
  35. +          self[x,y]:=src[x,y];
  36.      end
  37.    else
  38.      Inherited Assign(Source);
  39.  
« Last Edit: April 23, 2023, 07:44:15 pm by lagprogramming »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12326
  • FPC developer.
Re: Improved CPU cache usage at procedure TFPCustomImage.Assign
« Reply #1 on: April 23, 2023, 03:55:01 pm »
Fixed.

 

TinyPortal © 2005-2018