Artifacts Associated With Ticket 7da6c2d04c
Ticket change [a064c6601d] (rid 253939) by ralfixx on 2026-05-26 17:54:55:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/x-markdown"
- comment initialized to:
When a fileevent is set up to include the data read from the channel, the performance is dramatically reduced due to shimmering from list to string in Tcl_FileEventObjCmd(). Script: set up a pipeline, and on each read in the fileevent, reset the fileevent script to include the new data. I have intentionally limited the read to 65536 bytes at one time in order to show the problem. <pre> proc read_channel {fd data} { append data [read $fd 65536] # puts stderr "[string length $data]" if {[eof $fd]} { set now [clock milliseconds] puts stderr "read [string length $data] bytes in [expr {$now-$::start}]ms" catch {close $fd} set ::forever 1 return } fileevent $fd readable [list read_channel $fd $data] } set fd [open "|dd if=/dev/zero bs=65536 count=200" rb] fconfigure $fd -blocking 0 set start [clock milliseconds] fileevent $fd readable [list read_channel $fd ""] vwait forever </pre> Tcl_FileEventObjCmd() is checking whether to remove the fileevent by issuing a TclGetString() on the fileevent script and checking whether the returned string is '\\0' <pre> generic/tclIO.c #9285 /* * If we are supposed to delete a stored script, do so. */ if (*(TclGetString(objv[3])) == '\0') { DeleteScriptRecord(interp, chanPtr, mask); return TCL_OK; } </pre> This causes the fileevent script to shimmer into a string each time the filevent is reset. Proposed patch: I think it is safe to assume that fileevent scripts should be valid lists, so we could check the list length instead. If I do so, the perfomance is greatly enhanced (20x). Original tcl9.0.3 <code> read 13107200 bytes in 10245ms </code> With proposed patch applied: <code> read 13107200 bytes in 529ms </code> - foundin initialized to: "9.0.3"
- is_private initialized to: "0"
- login: "ralfixx"
- priority initialized to: "5 Medium"
- private_contact initialized to: "1d3c95db24e116878d6afed7cf890a89a0effe62"
- resolution initialized to: "None"
- severity initialized to: "Important"
- status initialized to: "Open"
- submitter initialized to: "ralfixx"
- subsystem initialized to: "24. Channel Commands"
- title initialized to: "fileevent poor performance due to shimmering"
- type initialized to: "RFE"
Add attachment "diff" [1dfd84f438] (rid 253941) by ralfixx on 2026-05-26 17:57:28.
Ticket change [32f216cf10] (rid 253942) by dgp on 2026-05-26 20:05:57:
- icomment:
Be very cautious. I do not think all of Tcl's callback mechanisms are consistent, but some if not most of them allow for a callback *script* and not only a callback command or command prefix. When any script is allowed, it is not at all safe to assume a list value. Make changes based on how you see the code to be written, not on assumptions about "the way it must be". (FWIW, given the fantasy to imagine starting Tcl over, limiting all callbacks to command prefixes would have been a better idea.)
- login: "dgp"
- mimetype: "text/plain"
- icomment:
Ticket change [5210e020a9] (rid 253943) by dgp on 2026-05-26 20:09:50:
- icomment:
Also worth noting that string rep generation is not what most people mean by "shimmering". Usually the unwanted effects that get disparaged as shimmering have to do with the loss of an internal rep. Generating a string does not (have to) provoke internal rep. loss. The stork can have two legs on the ground.
- login: "dgp"
- mimetype: "text/plain"
- icomment:
Ticket change [d8dcf0ac49] (rid 253949) by emiliano on 2026-05-27 00:45:03:
- icomment:
Note that, on trunk, the check uses the new Tcl_IsEmpty() function introduced with TIP#711, which doesn't generate a string representation if the provided object has a list internal representation
- login: "emiliano"
- mimetype: "text/plain"
- icomment:
Ticket change [d9a31f4839] (rid 253958) by ralfixx on 2026-05-27 08:43:37:
- icomment:
Emiliano, that is great news, even better solution than to fix it in just one place. Donal, thanks for the reminder that "there might be dragons" :-) My proposed patch tried "list" first, and if that failed for any reason, the "old" GetString was used. Reasoning was to revert the penalty: if a ("proper":-) list is used as callback, have the quick path. If a non-list is used, use the string rep. Since the script is "eval"d, I assumed it needed to be a proper list anyway (eg. <pre> fileevent $fd readable "some-func $fd {" </pre> fails with <pre> missing close-brace while executing "foo file3 {" </pre> ) - login: "ralfixx"
- mimetype: "text/x-markdown"
- icomment:
Ticket change [4dd45e54f7] (rid 253959) by apnadkarni on 2026-05-27 08:53:27:
- icomment:
@ralfixx, and thanks for taking the trouble to track this down, I haven't had time to follow up on c.l.t.
- login: "apnadkarni"
- mimetype: "text/plain"
- icomment:
Ticket change [544a013fa0] (rid 253965) by oehhar on 2026-05-27 10:17:41:
- icomment:
Maybe TIP 711 "Tcl_IsEmpty()" may be used to avoid shimmering...
- login: "oehhar"
- mimetype: "text/x-fossil-wiki"
- username: "oehhar"
- icomment:
Ticket change [b6f30ca7fd] (rid 253966) by oehhar on 2026-05-27 10:21:33:
- icomment:
Sorry, Jan has replaced all the Tcl_GetString() == \0 by Tcl_IsEmpty in commit [c26a56dd82]. Thanks, Harald
- login: "oehhar"
- mimetype: "text/x-fossil-wiki"
- username: "oehhar"
- icomment:
Ticket change [9afc3d4e11] (rid 253973) by jan.nijtmans on 2026-05-27 10:39:48:
- assignee changed to: "jan.nijtmans"
- closedate changed to: "2461187.9443148379"
- closer changed to: "jan.nijtmans"
- icomment:
What can be done in 9.0 (which doesn't have Tcl_IsEmpty) is [ab9c069c58ac773e|this]. It's harmless, because it only operates when there is no string representation. The disadvantage of using Tcl_ListObjLength() is that it might shimmer a non-list type to a list, that's what we don't want.
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- resolution changed to: "Fixed"
- status changed to: "Closed"