Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: a064c6601d4c17d23610f49b5d48b7c56918995e8ec8d8bb04ffef342f5eb7c7
Ticket: 7da6c2d04c219e8ed4896b9d4e9eb88e586447f2
fileevent poor performance due to shimmering
User & Date: ralfixx on 2026-05-26 17:54:55
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/x-markdown"
  4. comment changed 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>
    
  5. foundin changed to: "9.0.3"
  6. is_private changed to: "0"
  7. login: "ralfixx"
  8. priority changed to: "5 Medium"
  9. private_contact changed to: "1d3c95db24e116878d6afed7cf890a89a0effe62"
  10. resolution changed to: "None"
  11. severity changed to: "Important"
  12. status changed to: "Open"
  13. submitter changed to: "ralfixx"
  14. subsystem changed to: "24. Channel Commands"
  15. title changed to: "fileevent poor performance due to shimmering"
  16. type changed to: "RFE"