| Ticket UUID: | ff8a1e55a2b8714d23909013ee4e9c52f06db919 | |||
| Title: | Filling a never-mapped text widget is CPU hungry | |||
| Type: | Bug | Version: | 8.5.18 | |
| Submitter: | beric | Created on: | 2015-11-09 08:48:35 | |
| Subsystem: | 18. [text] | Assigned To: | fvogel | |
| Priority: | 6 | Severity: | Important | |
| Status: | Closed | Last Modified: | 2015-12-13 20:04:49 | |
| Resolution: | Fixed | Closed By: | fvogel | |
| Closed on: | 2015-12-13 20:04:49 | |||
| Description: |
Determining the height in lines of the text widget must be started only when the widget is "realized" (i.e. mapped the first time). In fact, before the widget is realized, its width is 1 pixel. With the default -wrap mode "char", it takes a lot of CPU to determine its height, which is not relevant. Non-realized text widget are typically inside notebook widgets. | |||
| User Comments: |
fvogel added on 2015-12-13 20:04:49:
Fix merged in core-8-5-branch and trunk, thank you very much again. danckaert added on 2015-12-08 09:11:51: I first copied this from another place where Tk_IsMapped() is checked (around line 4059). But in fact this situation never happens: see just above and below line 5072, which is the only place where dInfoPtr->maxX and dInfoPtr->maxY are set. fvogel added on 2015-12-07 21:42:45: Thanks again. I have reverted your initial patch and commited the second one to branch bug-ff8a1e55a2.
The test suite is happy with this second patch.
Just a question: in your original patch you tested whether:
dInfoPtr->maxX <= dInfoPtr->x || dInfoPtr->maxY <= dInfoPtr->y
and in such a case the line metrics calculation was also postponed.
This sounded a good idea to me, why is this not part of the patch anymore?
danckaert added on 2015-12-07 14:32:51: The drawback of my earlier patch is that it will keep firing [after 1] events for all unmapped text widgets forever. This might become a problem when there are many unmapped text widgets (for example in notebook panes). Attached is a different patch to achieve the same. It simply discards the async updates for unmapped widgets. I think this is correct because TkTextRelayoutWindow() will schedule a new async update cycle after mapping the widget anyway. fvogel added on 2015-12-06 10:03:22: > . unmapped case: 10 s This value is in fact due to the granularity of the timing measurement (see proc timeit in my comment below). In fact it is much much quicker, and this is consistent with the expectation from what the patch is doing. Besides, no test case of the test suite shows regression with this patch. fvogel added on 2015-12-05 13:35:09: BTW, timings for me:
- Without the patch:
. mapped case: 71 s
. unmapped case: > 12 h (didn't let it run more than one night!)
- With the patch:
. mapped case: 71 s (unchanged, of course)
. unmapped case: 10 s
Great!!!
fvogel added on 2015-12-05 13:32:43: Many thanks for the patch! Now available in branch bug-ff8a1e55a2 for testing and review. danckaert added on 2015-12-03 10:35:41: Maybe we can postpone the line metrics calculations when the text widget is not yet mapped. See attached patch. fvogel added on 2015-11-09 21:49:59: > Is there a way to optimize the calculation? Well, I don't think so. Quoting part of the discussion we have right know on TCLCORE list regarding TIP #438: [We could] "profile the text widget code to see if there is a way to speed up the calculations. This would have limited benefit since the operation is fundamentally O(n)-ish anyway." See TIP #438 and the discussion around this on TCLCORE. beric added on 2015-11-09 15:25:30: I didn't know this debug command. Also note that the problem is due to the small width of the text widget, not the fact that it has never been mapped: if the user resize the window, this will be slow too. While disabling the calculation process when the text has never been mapped would solve this specific problem, it would be good to find a real fix. Sometimes I also had to kill wish process because the console was stuck in displaying very long result (big list for example). Is the a way to optimize the calculation? (I ask without knowing how is the code now) fvogel added on 2015-11-09 14:49:40: In fact it is time-able, using a little trick:
package require Tk
destroy .t
set str [string repeat "Unmapped text widget - " 100]
set str [string repeat "$str\n" 500]
proc timeit {{oldvalue -1}} {
global tk_textInvalidateLine starttime
if {$oldvalue < 0} {
set starttime [clock seconds]
set tk_textInvalidateLine {}
}
set newvalue [llength $tk_textInvalidateLine]
if {$newvalue > $oldvalue} {
# timing granularity = 10 seconds is good enough
after 10000 [list timeit $newvalue]
} else {
set lasted [expr {[clock seconds] - $starttime}]
puts "The end! --> Duration: $lasted seconds"
}
}
pack [text .t] ; # Mapped text widget
.t debug on
update
.t insert end $str
timeit
destroy .t
text .t ; # Unmapped text widget
.t debug on
.t insert end $str
timeit
Results:
Mapped widget: 50 seconds
Unmapped widget: more than 35 minutes (still not finished yet)
beric added on 2015-11-09 11:38:16: The difference is not time-able, but as you know, the line metrics are calculated asynchronously ;-) This is this process which takes all CPU. Inside a whish console, just run the first part of your test, an check the processor state. Mine is at 25% (one of my core) since 5 minutes now. fvogel added on 2015-11-09 11:14:33: I fail to show a significant difference between populating time in a text widget that was never mapped and a mapped one.
My test script is:
package require Tk
destroy .t
set str [string repeat "Unmapped text widget - " 100]
set str [string repeat "$str\n" 5000]
text .t ; # Unmapped text widget
time {.t insert end $str}
destroy .t
pack [text .t] ; # Mapped text widget
update
time {.t insert end $str}
For me this outputs (unmapped case, then mapped case):
35692 microseconds per iteration
37240 microseconds per iteration
Can you provide more details and a script demonstrating the issue?
| |||
Attachments:
- text-unmapped-2.patch [download] added by danckaert on 2015-12-07 14:27:30. [details]
- text-unmapped.patch [download] added by danckaert on 2015-12-03 10:34:53. [details]
