| Ticket UUID: | 3396731 | ||
| Title: | inline string reverse | ||
| Type: | RFE | Created on: | 2011-08-23 06:59:42 |
| Submitter: | nijtmans | Assigned to: | dgp |
| Subsystem: | 44. UTF-8 Strings | Severity: | |
| Priority: | 5 Medium | Last modified: | 2011-08-29 14:27:46 |
| Status: | Closed | Closed by: | nijtmans |
| Resolution: | Fixed | Closed on: | 2011-08-23 07:19:36 |
| Version: | None | ||
| Description: | ||||
The TclStringObjReverse function (in tclStringObj.c) does a complicated job: If the object contains no unicode, the reverse is done in-place, but otherwise a Unicode string representation is formed and that is reversed in stead. Much simpler is: reverse the individual bytes from the UTF-8 characters first in-place, and then reverse the bytes of the whole string. The individual bytes of the UTF-8 representation will then be correct again, and this can always be done inline. | ||||
| User Comments: | ||||
nijtmans added on 2011-08-23 14:19:36:
Fixed in trunk [8ebe975003] dgp added on 2011-08-23 23:35:16:
Doesn't this change unnecessarily force the "string rep" generation of a pure Unicode value? Loss of "pure Unicode" status can influence what code paths are available down the line. Any impact measurement done? dgp added on 2011-08-24 00:02:32:
New branch revert-3396731 takes this patch back out. With two branches differing only in this matter, we can make comparisons and decide which to keep. nijtmans added on 2011-08-24 02:13:12:
>Doesn't this change unnecessarily >force the "string rep" generation of >a pure Unicode value? It will be difficult to write a test case which does not - in the end - generate a string rep anyway. In-line reversal only does a single memory allocation, and only when the value is shared, so it will win from whatever other method. dgp added on 2011-08-24 04:08:56:
I'm not worried about the speed of the operation,. I'm worried about the loss of "pure unicode" status which will change the optimizations available to subsequent operations on the value. dgp added on 2011-08-24 04:10:08:
FWIW, I tried to measure the two branches with tclbench and proved again only one thing. I'm completely incapable of getting any usable information out of tclbench! ;P:P:P:P:P dgp added on 2011-08-24 04:15:02:
003 ARRAY makeHash 500 50 40725.52 14465.44 That sure looks like the change was a huge performance hit. I used -repeat 8 on an idle machine; surely that's enough for such a huge dispaity to be real?! But I dont' believe it. Is a [reverse] operation even anywhere near this? So then what? dgp added on 2011-08-24 23:22:03:
Ok, appears my botched performance attempts were attempting to compare two build that did not agree on their -DPURIFY settings. D'oh!!! dgp added on 2011-08-24 23:24:29:
More successful attempt today better matches expectations. On almost all benchmarks, no significant difference. Only a few differences on the tests that measure the performance of the reverse operations that were changed: 555 STR reverse core, 10 c 1.29 1.32 1.00 556 STR reverse core, 10 uc 1.23 1.26 1.00 557 STR reverse core, 100 c 1.72 1.33 1.00 558 STR reverse core, 100 uc 1.67 1.24 1.00 559 STR reverse core, 400 c 2.86 1.29 1.00 560 STR reverse core, 400 uc 2.76 1.14 1.00 Unfortunately, the news is not good for the new patch. Will pursue and followup with an explanation when I have one. dgp added on 2011-08-24 23:25:56:
Column 1 = trunk Column 2 = revert branch Column 3 = 8.5 branch dgp added on 2011-08-25 00:08:55:
The degraded benchmarks test the speed of string reverse [string range $long 1 $length] The [string range] returns a pure unicode value, so this is exactly the case we would expect to slow down with the change, which forces a string rep generation that was not done before. dgp added on 2011-08-25 01:47:21:
Semi-side comment... I'm not seeing any way at the moment for an unshared value to reach TclStringObjReverse(). We could be optimizing a case that never happens. dgp added on 2011-08-25 01:52:28:
Ok, it's a bit weird. [string reverse $foo] always reaches TclStringObjReverse() with $foo shared. [::tcl::string::reverse $foo] can get there unshared. This feels somewhat like an unwanted side effect of ensembles. Bringing in dkf to comment on just that. dkf added on 2011-08-25 15:17:01:
That would most certainly count as an unwanted side effect. It seems to result from the changes to work with NRE (specifically, going from using Tcl_EvalObjv to Tcl_NREvalObj). nijtmans added on 2011-08-25 15:33:11:
>The degraded benchmarks test the speed of >string reverse [string range $long 1 $length] Thanks! That sounds logical. Of course we could ad a special case for this in TclStringObjReverse, but I guess that "string range" could also be speeded up by not generating the Unicode representation. Working on this........ ferrieux added on 2011-08-25 17:50:13:
Re the extra ref in ensembles. It seems to me that it is a necessary consequence of the absence of "frame surgery" when rewriting the command-and-args. See label "runResultingSubcommand" in NsEnsembleImplementationCmdNR: here we prepare a new objv[], we are not modifying in-place the framePtr->objv, hence the incrrefcount. Not sure it is NRE's fault :) msofer added on 2011-08-25 18:03:03:
If we find a way to fixt that and do "frame surgery", we should make sure to also use elsewhere - the [interp alias] implementation is a definite candidate nijtmans added on 2011-08-25 19:01:32:
Special case committed to trunk for Pure Unicode-situation. This should be faster in all situations. dkf added on 2011-08-25 19:28:10:
We should probably follow up on the NRE stuff as a separate issue. dgp added on 2011-08-25 19:48:17:
See 3397978 for continued examination of the value sharing issue. dgp added on 2011-08-25 19:50:41:
nijtmans, if you do pursue futher shimmering reform in tclStringObj.c, please make your commits to a feature branch first. Some of the most striking performance improvements in Tcl 8.6 have come from improved use of the "pure unicode" type, and I want the chance to confirm those improvements are not lost in continued reform. Thanks! dgp added on 2011-08-25 23:28:46:
Thank you for the update to the trunk. I have produced my own update http://core.tcl.tk/tcl/info/61d345ff2f which is the new tip of the revert-3396731 branch. dgp added on 2011-08-25 23:36:30:
I've also extended the "reverse core" benchmarks in tclbench so that it includes testing the reversal of strings of length 4000 and 40000 chars to get a more complete picture of scalability. Then looking at several variants I get this: Column 1 = "trunk before we started attempting these patches" Column 2 = "revert branch tip (my patch)" Column 3 = "current trunk (your patch)" Column 4 = "8.5 branch" 001 STR reverse core, 10 c 3.49 3.49 3.69 2.78 002 STR reverse core, 10 uc 3.67 3.69 3.37 2.92 003 STR reverse core, 100 c 3.67 3.67 5.31 2.89 004 STR reverse core, 100 uc 3.78 3.85 3.54 3.10 005 STR reverse core, 400 c 4.03 4.21 10.78 3.28 006 STR reverse core, 400 uc 4.17 4.38 4.03 3.67 007 STR reverse core, 4000 c 8.43 10.69 75.14 7.66 008 STR reverse core, 4000 uc 8.54 10.97 10.59 10.25 009 STR reverse core, 40000 c 58.18 76.50 724.64 54.33 010 STR reverse core, 40000 uc 55.26 76.70 76.08 75.76 At least for the code paths covered by the benchmarks, the current trunk is clearly worst and has some severe scaling problem. My patch is better than that, but still not as good as the trunk was before any of us started doing anything. dgp added on 2011-08-27 11:00:33:
Found the flaw in my loop hotspots that was slowing them down compared to the former trunk. Update tclbench results: 001 STR reverse core, 10 c 3.73 3.55 3.50 2.81 002 STR reverse core, 10 uc 3.36 3.73 3.68 2.93 003 STR reverse core, 100 c 5.35 3.71 3.64 2.91 004 STR reverse core, 100 uc 3.52 3.86 3.77 3.12 005 STR reverse core, 400 c 10.85 4.05 4.01 3.27 006 STR reverse core, 400 uc 4.08 4.24 4.17 3.67 007 STR reverse core, 4000 c 75.62 8.57 8.39 7.66 008 STR reverse core, 4000 uc 10.68 8.67 8.59 10.28 009 STR reverse core, 40000 c 730.58 61.96 54.48 54.01 010 STR reverse core, 40000 uc 77.10 55.44 55.57 76.43 This time: Column 1 = current trunk Column 2 = former trunk Column 3 = my patch Column 4 = 8.5 branch I'll be merging my patch into the trunk. It takes the core idea of the original feature request and implements it in ways that don't lose performance as measured by tclbench. dgp added on 2011-08-27 11:35:07:
Alternate patch merged to trunk. nijtmans added on 2011-08-29 14:27:46:
A few minor remarks...:
- ReverseUniChars contains the line
from += count - 1
but the "from" variable is not used any
more after that.
- ReverseBytes() should be static ;-)
- ReverseUniChars is only called once, so it
can be inlined. That allows us to separate the
inline case from tthe non-line case better , so
it saves an additional if().
- src = from + count -1: the substraction of 1
can be saved by changing the loop a little bit.
- The Tcl_Unichar ch declaration can be put
outside the loop.
Committed to trunk. Nice work, Don, and
thank you very much for the evaluation
and improvements!!!!
| ||||
