Tk Source Code

View Ticket
Login
2026-09-26
18:12 • Ticket [8da7af2f] slow widget creation if default font is not used status still Closed with 4 other changes artifact: a06f8f53 user: jan.nijtmans
17:56 • Ticket [8da7af2f]: 5 changes artifact: 2ab4f24d user: jan.nijtmans
17:54 • Ticket [c81e0cce] Named font late creation status still Open with 3 other changes artifact: ec774eee user: serhiy.storchaka
17:48
Fix [c81e0ccee2]: a named font was not used by widgets that referred to its name before it was created. Fonts cached under the name are now converted to the new named font. This also fixes a side effect of [8da7af2f8e]: an unused font cached under the name, kept until idle time, shadowed the new named font. Patch by Christian Werner, test by Harald Oehlmann. check-in: 9fc3317c user: serhiy.storchaka tags: named-font-late-creation
17:35 • Ticket [8da7af2f] slow widget creation if default font is not used status still Closed with 3 other changes artifact: 0e13925e user: serhiy.storchaka
17:30
Fix tests after [8da7af2f8e]: font-17.5 failed on macOS because the unused font of a deleted named font is now kept until idle time. Rename the duplicate test font-17.2 to font-17.1.1. closed check-in: 0e551c58 user: serhiy.storchaka tags: font-test-deferred-free
2026-09-25
19:28 • Closed ticket [8da7af2f]: slow widget creation if default font is not used plus 7 other changes artifact: 15e79ec4 user: nemethi
17:47
Fix for [8da7af2f8e]: slow widget creation if default font is not used. check-in: ad904e13 user: csaba tags: core-8-6-branch
16:48
Fix for [8da7af2f8e]: slow widget creation if default font is not used. check-in: 459cb7dc user: csaba tags: core-9-0-branch
16:39
Fix for [8da7af2f8e]: slow widget creation if default font is not used. check-in: 37a74c33 user: csaba tags: trunk, main
2026-09-18
16:07 • Ticket [8da7af2f] slow widget creation if default font is not used status still Open with 3 other changes artifact: ddc654ce user: serhiy.storchaka
16:02
Fix [8da7af2f8e]: slow widget creation if default font is not used

An unused font is now kept in the cache until idle time instead of being deleted immediately, since it is often needed again soon: the default font of a widget is released when the widget is created with another -font value, and loading it again for every widget is expensive. closed check-in: 722c9cb1 user: serhiy.storchaka tags: font-deferred-free

2024-06-24
06:02 • Ticket [8da7af2f] slow widget creation if default font is not used status still Open with 3 other changes artifact: 886f679e user: fvogel
2024-06-23
21:38 • Ticket [8da7af2f]: 3 changes artifact: 6d4f84a1 user: jan.nijtmans
08:41 • Ticket [8da7af2f]: 3 changes artifact: d17c2ec0 user: fvogel
08:19 • Open ticket [8da7af2f]. artifact: c169657b user: fvogel
08:16 • Closed ticket [a0dca39f]: Panic in font-47.1 plus 6 other changes artifact: 45e73bbb user: fvogel
2024-06-16
20:45 • Ticket [a0dca39f]: 4 changes artifact: 2a8f7415 user: fvogel
2024-01-27
13:50 • Closed ticket [8da7af2f]: slow widget creation if default font is not used plus 5 other changes artifact: 14a1926a user: fvogel
13:49
Fix [8da7af2f8e]: slow widget creation if default font is not used. check-in: ced5e641 user: fvogel tags: trunk, main
13:48
Fix [8da7af2f8e]: slow widget creation if default font is not used. check-in: f45960ae user: fvogel tags: core-8-6-branch
2024-01-23
20:18 • Ticket [8da7af2f] slow widget creation if default font is not used status still Open with 3 other changes artifact: 090b4ce6 user: anonymous
2024-01-22
21:49 • Ticket [8da7af2f]: 3 changes artifact: 0041dd24 user: fvogel
21:06 • Ticket [8da7af2f]: 3 changes artifact: 606e1975 user: fvogel
2024-01-21
21:19 • Ticket [8da7af2f]: 5 changes artifact: fdfdce6c user: fvogel
21:14
Fix [8da7af2f8e]: slow widget creation if default font is not used check-in: d7b0c443 user: fvogel tags: bug-8da7af2f8e
2024-01-14
20:21 • Ticket [8da7af2f] slow widget creation if default font is not used status still Open with 3 other changes artifact: 5aeeb5c0 user: anonymous
20:10 • Ticket [8da7af2f]: 3 changes artifact: 62c31a21 user: anonymous
2024-01-13
14:40 • Ticket [8da7af2f]: 3 changes artifact: ee1a0bab user: fvogel
14:31 • Ticket [8da7af2f]: 3 changes artifact: f36e8fe1 user: fvogel
2024-01-09
07:28 • Ticket [8da7af2f]: 3 changes artifact: bce17dde user: anonymous
2024-01-08
23:18 • Add attachment 8da7af2f8e - slow widget creation if default font is not used.tcl to ticket [8da7af2f] artifact: 85ffd0d5 user: fvogel
23:17 • Ticket [8da7af2f] slow widget creation if default font is not used status still Open with 3 other changes artifact: 03d2138a user: fvogel
2024-01-06
17:17 • New ticket [8da7af2f]. artifact: fbea336a user: anonymous

Ticket UUID: 8da7af2f8e8975de3acd0b4f4d45bf1d69bc7197
Title: slow widget creation if default font is not used
Type: Bug Version: 8.6
Submitter: anonymous Created on: 2024-01-06 17:17:55
Subsystem: 44. Generic Fonts Assigned To: nobody
Priority: 5 Medium Severity: Important
Status: Closed Last Modified: 2026-09-26 18:12:25
Resolution: Fixed Closed By: jan.nijtmans
    Closed on: 2026-09-26 18:12:25
Description:
If all widgets are created with a -font parameter, widget creation is very slow.
This is slow:

grid [ ttk::entry .myEntry01 -font TkFixedFont -width 35 -textvariable myvariable ]
grid [ ttk::entry .myEntry02 -font TkFixedFont -width 35 -textvariable myvariable ]
grid [ ttk::entry .myEntry03 -font TkFixedFont -width 35 -textvariable myvariable ]
set myvariable "Hello World"

this is quick:

grid [ ttk::entry .myEntry00                   -width 35 -textvariable myvariable ]
grid [ ttk::entry .myEntry01 -font TkFixedFont -width 35 -textvariable myvariable ]
grid [ ttk::entry .myEntry02 -font TkFixedFont -width 35 -textvariable myvariable ]
grid [ ttk::entry .myEntry03 -font TkFixedFont -width 35 -textvariable myvariable ]
set myvariable "Hello World"

I think the problem manifests because:

* Tk instantiates every widget with the default font.
* It then sets the font according to the -font parameter.
* if the default font isn't used by any other widget, it's unloaded.

The next widget will go through the same process. Since the default font got unloaded during the instanciation of the former widget, it will be loaded from disk again.

I think, the relevant source is at
ttkWidget.c: TtkWidgetConstructorObjCmd(..)

[...]
/// I think Tk_InitOptions sets default font
    if (Tk_InitOptions(interp, recordPtr, optionTable, tkwin) != TCL_OK) {
	goto error;
    }

/// I think this then sets the other font, forgetting the default font
    if (Tk_SetOptions(interp, recordPtr, optionTable,
	    objc - 2, objv + 2, tkwin, &savedOptions, NULL) != TCL_OK) {
	Tk_RestoreSavedOptions(&savedOptions);
	goto error;
    } 
[...]
User Comments: jan.nijtmans added on 2026-09-26 18:12:25:

@serhiy, I didn't see your fix when I wrote previous comment ....


jan.nijtmans added on 2026-09-26 17:56:12:

On MacOS, I see:

==== font-17.5 Tk_FreeFont procedure: named font FAILED
==== Contents of test case:

# not (fontPtr->refCount == 0) font create xyz -underline 1 .t.f config -font xyz font delete xyz set x [font actual xyz -underline] destroy .t.f list [font actual xyz -underline] $x

---- Result was: 1 1 ---- Result should have been (exact matching): 0 1 ==== font-17.5 FAILED

My guess is that this failure is caused by this bugfix, so I took the freedom to change the test expectation. Please verify if this is correct.


serhiy.storchaka added on 2026-09-26 17:35:43:

After the merge, font-17.5 fails on macOS CI (Aqua only): after font delete xyz and destroying the widget that used it, font actual xyz -underline returns 1 instead of 0, because the unused font of the deleted named font is now kept in the cache until idle time.

Proposed fix in branch font-test-deferred-free: update idletasks in font-17.5. It also renames the duplicate test font-17.2 added by the fix to font-17.1.1. The same is needed in core-9-0-branch and core-8-6-branch.

The change also has a side effect: if a font name was used before font create with that name (e.g. by font actual), the unused font is still in the cache and shadows the new named font (font actual and new widgets get the old font). This is fixed by the proposed fix for [c81e0ccee2], which also converts such fonts.


nemethi (claiming to be Csaba Nemethi) added on 2026-09-25 19:28:43:

Many thanks, Serhiy, for this elegant alternative solution! Merged to main, core-9-0-branch, and core-8-6-branch by commits [37a74c33], [459cb7dc], and [ad904e13]. Closing the ticket.


serhiy.storchaka added on 2026-09-18 16:07:28:

An alternative solution on branch font-deferred-free ([722c9cb1d4]): an unused font is kept in the cache until idle time instead of being deleted immediately, so the default font released during widget creation is not unloaded. No reference is leaked, and no fonts are left in the cache when the application is deleted (checked with a PURIFY build). With the script from the ticket, 2000 entries take 6 ms instead of 3 s on Linux.


fvogel added on 2024-06-24 06:02:24:

I understand the idea. Note however that:

  1. We have the problem reported in this ticket for all options having the OPTION_NEEDS_FREEING flag, not just -font. It is only much more apparent with -font because the -font option processing takes time. That said, perhaps we could deal with -font only since the other 6 OPTION_NEEDS_FREEING options processing is so quicker.
  1. The default font might be different for different types of widgets. In principle we should not only cache a reference to TkDefaultFont but to several default fonts. For instance the Tk button default font is TkDefaultFont but the ttk::entry default font is TkTextFont.


jan.nijtmans added on 2024-06-23 21:38:40:

How about adding a "font" field to the TkDisplay struct, and initialize it with the default font. When the TkDisplay is destructed, destroy the default font too. Then there is always at least one reference to the default font.

(I could imagine that each display might have its own default font, that's why this probably is the best place)


fvogel added on 2024-06-23 08:19:29:

Sorry, I have to reopen this because my "fix" is wrong. Please see [a0dca39fd1] for its consequences.

After careful analysis, the cacheing mechanism of the TkFont pointer in the Tcl_Obj internalRep.twoPtrValue.ptr1 (see Tk_AllocFontFromObj and Tk_GetFontFromObj) does after all seem to be totally correct to me.

As a consequence I had to revert [f45960ae] in [921bf608].

What's happening is exactly what the OP described in the first place:

* Tk creates every widget with the default font (through Tk_InitOptions).
* It then sets the font according to the -font parameter (through Tk_SetOptions).
* If the default font isn't used by any other widget, it's freed (through Tk_FreeSavedOptions).

Then, creating many widgets with a -font option leads to a lot of allocation and freeing of the default font. This takes time. We have this Tk_InitOptions --> Tk_SetOptions --> Tk_FreeSavedOptions pattern everywhere in Tk (see for instance ConfigureEntry in tkEntry.c, for regular (not ttk) entry widgets). I don't readily see how we can avoid the problem.


fvogel added on 2024-01-27 13:50:34:
Thank you for the feedback, and for the report in the first place. Fix is now merged.

anonymous added on 2024-01-23 20:18:11:
The fix works for me also. Thanks!

fvogel added on 2024-01-22 21:49:11:

The two new failures in the test suite come from the interaction between config.test (config-4.47 and config-4.48) and font.test (font-17.1 and font-18.1). All use the {Courier 12} font.

When running only font.test no previous use of {Courier 12} happened --> the testfont counts are OK.

When config.test is run before font.test, the testfont counts takes into account the use of {Courier 12} in config.test --> the counts in font.test are no longer the expected ones.

My fix [d7b0c443] reveals the issue with the test suite hygiene, which is now fixed by [07f1820b46].


fvogel added on 2024-01-22 21:06:44:
There are two new failures in the test suite: font-17.1 and font-18.1.

When running the font.test file only they do not fail. They fail when the full test suite is run. This indicates bad test suite hygiene rather than a problem with the fix. I'll investigate.

fvogel added on 2024-01-21 21:19:46:

I have traced this performance problem to be due to a bug in the font cache handling in Tk.

Please see branch bug-8da7af2f8e. Timings are now the same for me with or without .myEntry00 (checked on Linux). Could you please confirm whether you see the issue as fixed for you too?


anonymous added on 2024-01-14 20:21:51:
.. ok sorry, just realized that removing the textvariable gets rid of the long teardown time and the problem becomes more clear:

#!/usr/bin/wish

ttk::entry .myEntry00
for {set i 0} {$i < 2000} {incr i} {
    ttk::entry .myEntry$i -font TkFixedFont
}
exit


time with myEntry00:  0.219s
time  w/o myEntry00: 26.091s

anonymous added on 2024-01-14 20:10:04:
Indeed gridding isn't necessary to reproduce, timings are very similar.

If I run this script in linux 

#!/usr/bin/wish

set myvariable "Hello World"
grid [ ttk::entry .myEntry00 -width 35 -textvariable myvariable ]

for {set i 0} {$i < 2000} {incr i} {
    grid [ ttk::entry .myEntry$i -font TkFixedFont -width 35 -textvariable myvariable ]
}
exit


.. with time like so:

time ./script.tcl

I get:
 - 11s when I create myEntry00 first (with the standard font).
 - 36s when I comment out creation of myEntry00

The problem is more severe than the timings suggest: in both cases 10s are spent tearing down the window. Accounting for that, the difference in widget creation is roughly 25s to 1s.

For me, the effect is clearly visible without timing:
If I don't put the exit at the end of the script, such that I actually see the contents of the window, the quick versions comes up almost immediately. The slow version takes felt 20s before anything is shown.

fvogel added on 2024-01-13 14:40:10:
> That's a difference of ~7%.

Sorry, it's more than that. Still, I can't reproduce the original numbers I saw.

fvogel added on 2024-01-13 14:31:47:
Hmmm... I'm no longer seeing a performance difference of 3x. I can't reproduce the timings I pasted below.

With 5000 widgets (see my attached test script), on my hardware running the current core-8-6-branch on Windows 11, proc test1_slow executes in 4 seconds and proc test2_quick in 2.8 seconds. That's a difference of ~7%.

You (the OP) says "If all widgets are created with a -font parameter, widget creation is very slow." Could you please better characterize what you mean by "very slow"? What are your numbers?

Finally, gridding should not be needed to show the difference if it really lies in the Tk_InitOptions/Tk_SetOptions calls, and the -width and -textvariable options neither. This means the test script should be simplifiable.

fvogel added on 2024-01-08 23:17:43:

Profiling of proc test1_slow (see the attached script) on Windows shows that 72 % of the time is indeed spent in TtkWidgetConstructorObjCmd().

This time is spent in three main contributors in TtkWidgetConstructorObjCmd():

  • Tk_MakeWindowExist() line 423 (41%)
  • Tk_InitOptions() line 401 (21%)
  • SizeChanged() line 422 (7%)

When profiling proc test2_quick, 69% of the time is spent in TtkWidgetConstructorObjCmd(), with 54 % in Tk_MakeWindowExist(). Tk_InitOptions() indeed has negligible time (1%) spent in it.

Therefore, while Tk_MakeWindowExist() is the first contributor to performance, one could perhaps look at Tk_InitOptions().

Here are my timings:

% destroy {*}[winfo children .]
% time {test1_slow $nw}
10992699 microseconds per iteration
% destroy {*}[winfo children .]
% time {test2_quick $nw}
3585076 microseconds per iteration

The slowdown of proc test1_slow compared to proc test2_quick is of a factor of 3.


Attachments: