Recent

Author Topic: AI's advice how to optimize GTK2 TextOut  (Read 1218 times)

AlexTP

  • Hero Member
  • *****
  • Posts: 2669
    • UVviewsoft
AI's advice how to optimize GTK2 TextOut
« on: December 29, 2025, 11:52:03 pm »
My question:
Lazarus IDE project from GitLab, https://gitlab.com/freepascal.org/lazarus/lazarus . Analyze the GTK2 widgetset code from folder "lcl/interfaces/gtk2". Interesting part is method ExtTextOut from unit gtk2int, its code is in the file "lcl\interfaces\gtk2\gtk2winapi.inc". This method ExtTextOut is very slow, ie if I call it 200 times per one frame rendering, it spends lot of time. Find out how can I make it faster? Maybe some Pango objects can be cached better? If so, tell me how.

Grok's answer:
https://grok.com/c/80e6b290-418a-4fd2-96ee-979218a494fb?rid=cbf687cf-8884-4a44-b134-fd17dd7c3484

Somebody can add caching of Pango objects? I cannot.
« Last Edit: December 30, 2025, 05:59:49 pm by AlexTP »

Thaddy

  • Hero Member
  • *****
  • Posts: 18707
  • To Europe: simply sell USA bonds: dollar collapses
Re: AI's advice how to optimize GTK2 TextOut
« Reply #1 on: December 30, 2025, 03:47:43 pm »
Block the frame from paint updating until done? You know what I mean.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

ALLIGATOR

  • Sr. Member
  • ****
  • Posts: 334
  • I use FPC [main] 💪🐯💪
Re: AI's advice how to optimize GTK2 TextOut
« Reply #2 on: December 30, 2025, 05:24:37 pm »
As far as I know, this person also faced performance limitations. And as a result, he does it like this

UPD:
https://github.com/AlexanderBagel/FWHexView/blob/c3e06b3dc42a494d7683ab8bef694bb89c6c1957/src/FWHexView.Common.pas#L367
« Last Edit: December 30, 2025, 05:26:42 pm by ALLIGATOR »
I may seem rude - please don't take it personally

AlexTP

  • Hero Member
  • *****
  • Posts: 2669
    • UVviewsoft
Re: AI's advice how to optimize GTK2 TextOut
« Reply #3 on: January 01, 2026, 09:08:38 am »
Thanks @ALLIGATOR. I contact this person now and will use his code. License for this part was changed to 'MPL2 or LGPL' by kind permission of this person.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4660
  • I like bugs.
Re: AI's advice how to optimize GTK2 TextOut
« Reply #4 on: January 01, 2026, 10:44:29 am »
The AI is quite impressive! After studying the code for few seconds it already knows it better than I do.
Did somebody try the "Pango Caching Improvements" it suggested? How much they affect?
I guess a similar optimization could be used for LCL-GTK3. That would be even more important for the future.
« Last Edit: January 01, 2026, 10:47:12 am by JuhaManninen »
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

ALLIGATOR

  • Sr. Member
  • ****
  • Posts: 334
  • I use FPC [main] 💪🐯💪
Re: AI's advice how to optimize GTK2 TextOut
« Reply #5 on: January 01, 2026, 12:43:23 pm »
Just for fun, to understand what this is all about, I tried this code:

Code: Pascal  [Select][+][-]
  1. program app;
  2.  
  3. uses
  4.   Interfaces, LCLIntf, LCLType, SysUtils, Classes, Graphics, DateUtils;
  5.  
  6. var
  7.   B: TBitmap;
  8.   i: Integer;
  9.   t: TDateTime;
  10.   c: HDC;
  11.   s: String = '0123456789';
  12.  
  13. begin
  14.   B := TBitmap.Create;
  15.   B.PixelFormat := pf24bit;
  16.   B.SetSize(100, 100);
  17.  
  18.   c := B.Canvas.Handle;
  19.  
  20.   t:=Now;
  21.   for i:=100*1000 downto 0 do
  22.     ExtTextOut(c, 0, 0, 0, nil, PChar(s), Length(s), nil);
  23.   WriteLn(MilliSecondsBetween(Now, t), 'ms');
  24.  
  25.   B.Free;
  26.  
  27.   {$IFDEF WINDOWS}ReadLn;{$ENDIF}
  28. end.

Result:
Windows: 500ms
Linux GTK2: 900ms

Am I testing correctly? Is this the difference in performance we are talking about, or is it greater?
I may seem rude - please don't take it personally

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4660
  • I like bugs.
Re: AI's advice how to optimize GTK2 TextOut
« Reply #6 on: January 01, 2026, 02:43:36 pm »
Am I testing correctly? Is this the difference in performance we are talking about, or is it greater?
What does @AlexTP say?
I personally did not realize the code is slow until he mentioned it.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

AlexTP

  • Hero Member
  • *****
  • Posts: 2669
    • UVviewsoft
Re: AI's advice how to optimize GTK2 TextOut
« Reply #7 on: January 01, 2026, 03:00:12 pm »
@Juha, @Alligator
GTK2 ExtTextOut is slower of coz, than Win32 API. Seems you are testing ok, but I thouhgt that the ratio between gtk2/win32 must be bigger than 900/500, my looking shows it (ratio) is like 2...5. your demo did not pass Dx (inter-char deltas) non-nil value to ExtTextOut.
« Last Edit: January 01, 2026, 03:02:09 pm by AlexTP »

ALLIGATOR

  • Sr. Member
  • ****
  • Posts: 334
  • I use FPC [main] 💪🐯💪
Re: AI's advice how to optimize GTK2 TextOut
« Reply #8 on: January 01, 2026, 04:59:38 pm »
With this code
Windows: 500 ms
Linux GTK2: 14000 ms

Code: Pascal  [Select][+][-]
  1. program app;
  2.  
  3. uses
  4.   Interfaces, LCLIntf, LCLType, SysUtils, Classes, Graphics, DateUtils;
  5.  
  6. const
  7.   cs = '0123456789';
  8.  
  9. var
  10.   B: TBitmap;
  11.   i: Integer;
  12.   t: TDateTime;
  13.   s: String = cs;
  14.   dx: array [0..Length(cs)] of Integer;
  15.  
  16.  
  17. begin
  18.   B := TBitmap.Create;
  19.   B.SetSize(100, 100);
  20.  
  21.   for i:=Low(dx) to High(dx) do
  22.     dx[i] := 1;
  23.  
  24.   t:=Now;
  25.   for i:=0 to 100*1000 do
  26.     ExtTextOut(B.Canvas.Handle, 0, 0, 0, nil, PChar(s), Length(s), dx);
  27.   WriteLn(MilliSecondsBetween(Now, t), 'ms');
  28.  
  29.   B.Free;
  30.  
  31.   {$IFDEF WINDOWS}ReadLn;{$ENDIF}
  32. end.
  33.  
I may seem rude - please don't take it personally

zeljko

  • Hero Member
  • *****
  • Posts: 1823
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: AI's advice how to optimize GTK2 TextOut
« Reply #9 on: January 01, 2026, 08:48:36 pm »
The AI is quite impressive! After studying the code for few seconds it already knows it better than I do.
Did somebody try the "Pango Caching Improvements" it suggested? How much they affect?
I guess a similar optimization could be used for LCL-GTK3. That would be even more important for the future.

It advised two times to use Qt5 and Qt6 :), and I did not see any code for "Pango Caching Improvements" :)

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4660
  • I like bugs.
Re: AI's advice how to optimize GTK2 TextOut
« Reply #10 on: January 01, 2026, 09:24:16 pm »
It advised two times to use Qt5 and Qt6 :),
That is a good advice. :)

Quote
and I did not see any code for "Pango Caching Improvements" :)
There is a code snippet although I did not test it.
Code: Pascal  [Select][+][-]
  1. // Add fields to device context or a global cache map
  2. if not Assigned(CachedLayout) or (FontChanged) then
  3. begin
  4.   if Assigned(CachedLayout) then g_object_unref(CachedLayout);
  5.   CachedLayout := pango_layout_new(PangoContext);
  6.   CachedFontDesc := pango_font_description_copy(CurrentFontDesc);
  7.   pango_layout_set_font_description(CachedLayout, CachedFontDesc);
  8. end;
  9. pango_layout_set_text(CachedLayout, PChar(UTF8Str), Count);
  10. // Then use for extents and rendering
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

zeljko

  • Hero Member
  • *****
  • Posts: 1823
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: AI's advice how to optimize GTK2 TextOut
« Reply #11 on: January 01, 2026, 09:50:46 pm »
That does not smell good, but let someone provide patch which prooves massive improvement.

 

TinyPortal © 2005-2018