Forum > FPC development

[SOLVED] Improved CPU cache usage at procedure TFPCustomImage.Assign

(1/1)

lagprogramming:
packages/fcl-image/src/fpimage.inc has procedure TFPCustomImage.Assign(Source: TPersistent);

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Procedure TFPCustomImage.Assign(Source: TPersistent); Var  Src : TFPCustomImage;  X,Y : Integer; begin  If Source is TFPCustomImage then    begin    Src:=TFPCustomImage(Source);    // Copy extra info    FExtra.Assign(Src.Fextra);    // Copy palette if needed.    SetSize(0,0); { avoid side-effects in descendant classes }    UsePalette:=Src.UsePalette;    If UsePalette then      begin      Palette.Count:=0;      Palette.Merge(Src.Palette);      end;    // Copy image.    SetSize(Src.Width,Src.height);    If UsePalette then      For x:=0 to Src.Width-1 do        For y:=0 to src.Height-1 do          pixels[X,Y]:=src.pixels[X,Y]    else      For x:=0 to Src.Width-1 do        For y:=0 to src.Height-1 do          self[X,Y]:=src[X,Y];    end  else    Inherited Assign(Source);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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---diff --git a/packages/fcl-image/src/fpimage.inc b/packages/fcl-image/src/fpimage.incindex 3e2075900d..54be93074c 100644--- a/packages/fcl-image/src/fpimage.inc+++ b/packages/fcl-image/src/fpimage.inc@@ -460,11 +460,9 @@ procedure TFPCustomImage.CheckIndex (x,y:integer); end;  Procedure TFPCustomImage.Assign(Source: TPersistent);--Var+var   Src : TFPCustomImage;-  X,Y : Integer;-+  x,y : Integer; begin   If Source is TFPCustomImage then     begin@@ -482,13 +480,13 @@ procedure TFPCustomImage.CheckIndex (x,y:integer);     // Copy image.     SetSize(Src.Width,Src.height);     If UsePalette then-      For x:=0 to Src.Width-1 do-        For y:=0 to src.Height-1 do-          pixels[X,Y]:=src.pixels[X,Y]+      For y:=0 to src.Height-1 do+        For x:=0 to Src.Width-1 do+          pixels[x,y]:=src.pixels[x,y]     else-      For x:=0 to Src.Width-1 do-        For y:=0 to src.Height-1 do-          self[X,Y]:=src[X,Y];+      For y:=0 to src.Height-1 do+        For x:=0 to Src.Width-1 do+          self[x,y]:=src[x,y];     end   else     Inherited Assign(Source); 

marcov:
Fixed.

Navigation

[0] Message Index

Go to full version