Recent

Author Topic: [SOLVED] Improved CPU cache usage at packages/fcl-image/src/pixtools.pp  (Read 842 times)

lagprogramming

  • Sr. Member
  • ****
  • Posts: 407
packages/fcl-image/src/pixtools.pp has:
Code: Pascal  [Select][+][-]
  1. procedure FillRectangleColor (Canv:TFPCustomCanvas; x1,y1, x2,y2:integer; const color:TFPColor);
  2. var x,y : integer;
  3. begin
  4.   SortRect (x1,y1, x2,y2);
  5.   with Canv do
  6.     begin
  7.     for x := x1 to x2 do
  8.       for y := y1 to y2 do
  9.         DrawPixel(x,y,color);
  10.     end;
  11. end;
Code: Pascal  [Select][+][-]
  1. procedure FillRectangleImage (Canv:TFPCustomCanvas; x1,y1, x2,y2:integer; const Image:TFPCustomImage);
  2. var x,y : integer;
  3. begin
  4.   with image do
  5.     for x := x1 to x2 do
  6.       for y := y1 to y2 do
  7.         Canv.DrawPixel(x,y, colors[x mod width, y mod height]);
  8. end;
Code: Pascal  [Select][+][-]
  1. procedure FillRectangleImageRel (Canv:TFPCustomCanvas; x1,y1, x2,y2:integer; const Image:TFPCustomImage);
  2. var x,y : integer;
  3. begin
  4.   with image do
  5.     for x := x1 to x2 do
  6.       for y := y1 to y2 do
  7.         Canv.DrawPixel(x,y, colors[(x-x1) mod width, (y-y1) mod height]);
  8. end;

The following patch improves CPU cache usage by switching the x and y loops.
Code: Pascal  [Select][+][-]
  1. diff --git a/packages/fcl-image/src/pixtools.pp b/packages/fcl-image/src/pixtools.pp
  2. index b8baccb5bb..da0fbfe700 100644
  3. --- a/packages/fcl-image/src/pixtools.pp
  4. +++ b/packages/fcl-image/src/pixtools.pp
  5. @@ -73,8 +73,8 @@ procedure FillRectangleColor (Canv:TFPCustomCanvas; x1,y1, x2,y2:integer; const
  6.    SortRect (x1,y1, x2,y2);
  7.    with Canv do
  8.      begin
  9. -    for x := x1 to x2 do
  10. -      for y := y1 to y2 do
  11. +    for y := y1 to y2 do
  12. +      for x := x1 to x2 do
  13.          DrawPixel(x,y,color);
  14.      end;
  15.  end;
  16. @@ -555,8 +555,8 @@ procedure FillRectangleImage (Canv:TFPCustomCanvas; x1,y1, x2,y2:integer; const
  17.  var x,y : integer;
  18.  begin
  19.    with image do
  20. -    for x := x1 to x2 do
  21. -      for y := y1 to y2 do
  22. +    for y := y1 to y2 do
  23. +      for x := x1 to x2 do
  24.          Canv.DrawPixel(x,y, colors[x mod width, y mod height]);
  25.  end;
  26.  
  27. @@ -564,8 +564,8 @@ procedure FillRectangleImageRel (Canv:TFPCustomCanvas; x1,y1, x2,y2:integer; con
  28.  var x,y : integer;
  29.  begin
  30.    with image do
  31. -    for x := x1 to x2 do
  32. -      for y := y1 to y2 do
  33. +    for y := y1 to y2 do
  34. +      for x := x1 to x2 do
  35.          Canv.DrawPixel(x,y, colors[(x-x1) mod width, (y-y1) mod height]);
  36.  end;
« Last Edit: April 14, 2023, 05:55:24 pm by lagprogramming »


 

TinyPortal © 2005-2018