Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2011-08-29
14:27 • Ticket [3396731fff] inline string reverse status still Closed with 4 other c... artifact: 08f331996f user: nijtmans
07:25
[3396731] inline string reverse: minor further improvements check-in: 69c582084d user: jan.nijtmans tags: trunk
2011-08-27
11:35 • Ticket [3396731fff] inline string reverse status still Closed with 5 other c... artifact: 7b2f0de628 user: dgp
11:00 • Ticket [3396731fff]: 4 changes artifact: c52984736f user: dgp
2011-08-25
23:36 • Ticket [3396731fff]: 4 changes artifact: 9d7bf4026d user: dgp
23:28 • Ticket [3396731fff]: 4 changes artifact: 9eae69815b user: dgp
19:50 • Ticket [3396731fff]: 4 changes artifact: 188710f64d user: dgp
19:48 • Ticket [3396731fff]: 5 changes artifact: 6838bad6fa user: dgp
19:28 • Ticket [3396731fff]: 1 change artifact: 6606725e41 user: dkf
19:28 • Ticket [3396731fff]: 4 changes artifact: b0a3983d0c user: dkf
19:01 • Ticket [3396731fff]: 4 changes artifact: ddc6a1eaeb user: nijtmans
18:03 • Ticket [3396731fff]: 4 changes artifact: 8d974b46ac user: msofer
17:50 • Ticket [3396731fff]: 4 changes artifact: b782fb61e0 user: ferrieux
15:33 • Ticket [3396731fff]: 4 changes artifact: 7586cd18c2 user: nijtmans
15:17 • Ticket [3396731fff]: 4 changes artifact: 397ab81ab3 user: dkf
01:52 • Ticket [3396731fff]: 5 changes artifact: 9877fc8cee user: dgp
01:47 • Ticket [3396731fff]: 4 changes artifact: 7772c06c7c user: dgp
00:08 • Ticket [3396731fff]: 4 changes artifact: 5c41486eb9 user: dgp
2011-08-24
23:25 • Ticket [3396731fff]: 4 changes artifact: 1dc858a1ff user: dgp
23:24 • Ticket [3396731fff]: 4 changes artifact: ccdad0231b user: dgp
23:22 • Ticket [3396731fff]: 4 changes artifact: 7fae403b39 user: dgp
04:15 • Ticket [3396731fff]: 4 changes artifact: d2ded28fe1 user: dgp
04:10 • Ticket [3396731fff]: 4 changes artifact: 0795aafc53 user: dgp
04:08 • Ticket [3396731fff]: 4 changes artifact: 194ef3fb3a user: dgp
02:13 • Ticket [3396731fff]: 4 changes artifact: 45059c190e user: nijtmans
00:02 • Ticket [3396731fff]: 4 changes artifact: 5ef31056ff user: dgp
2011-08-23
23:35 • Ticket [3396731fff]: 4 changes artifact: 076360037f user: dgp
14:19 • Closed ticket [3396731fff]. artifact: a2b2343165 user: nijtmans
06:59 • New ticket [3396731fff]. artifact: 0b853849d2 user: nijtmans

inline string reverse

Closed / Fixed3396731fffffffffffffffffffffffffffffffffCreated 15 years agoUpdated 15 years ago
Descriptionplain2011-08-23 06:59:42
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.
nijtmansplain2011-08-23 14:19:36
Fixed in trunk [8ebe975003]
dgpplain2011-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?
dgpplain2011-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.
nijtmansplain2011-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.
dgpplain2011-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.
dgpplain2011-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
dgpplain2011-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?
dgpplain2011-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!!!
dgpplain2011-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.
dgpplain2011-08-24 23:25:56
Column 1 = trunk
Column 2 = revert branch
Column 3 = 8.5 branch
dgpplain2011-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.
dgpplain2011-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.
dgpplain2011-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.
dkfplain2011-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).
nijtmansplain2011-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........
ferrieuxplain2011-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 :)
msoferplain2011-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
nijtmansplain2011-08-25 19:01:32
Special case committed to trunk for Pure Unicode-situation. This should
be faster in all situations.
dkfplain2011-08-25 19:28:10
We should probably follow up on the NRE stuff as a separate issue.
dkf Ticket [3396731fff]: 1 change [History diff]2011-08-25 19:28:29
dgpplain2011-08-25 19:48:17
See 3397978 for continued examination
of the value sharing issue.
dgpplain2011-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!
dgpplain2011-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.
dgpplain2011-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.
dgpplain2011-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.
dgpplain2011-08-27 11:35:07
Alternate patch merged to trunk.
nijtmansplain2011-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!!!!
Log in (anonymous is fine) to add a comment.