Tcl Source Code

Check-in [61d345ff2f]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:3396731 Another rewrite of TclStringObjReverse() to make it adopt the nijtmans approach for reversing the objPtr->bytes rep without losing performance.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | revert-3396731
Files: files | file ages | folders
SHA1: 61d345ff2fd1c88c0a6c71864e4e7b49f070afe0
User & Date: dgp 2011-08-25 16:26:37
Context
2011-08-27
02:28
Repaired the lost performance in the copy loop hotspots. Now meets or beats the former trunk (and ... Closed-Leaf check-in: 34daf5b5b3 user: dgp tags: revert-3396731
2011-08-25
16:26
3396731 Another rewrite of TclStringObjReverse() to make it adopt the nijtmans approach for reversin... check-in: 61d345ff2f user: dgp tags: revert-3396731
2011-08-23
17:00
Revert the [string reverse] rewrite in a branch for more review. check-in: 7e909249a4 user: dgp tags: revert-3396731
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to generic/tclStringObj.c.

2648
2649
2650
2651
2652
2653
2654
2655














































2656
2657
2658
2659
2660
2661



2662

2663




2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723

2724
2725

2726

2727

2728
2729


2730

2731

2732

2733

2734

2735




2736


2737
2738
2739
2740





2741

2742

2743



2744
2745
2746
2747
2748





2749
2750
2751
2752
2753
2754
2755
 *	argument with modifications done in place.
 *
 * Side effects:
 *	May allocate a new Tcl_Obj.
 *
 *---------------------------------------------------------------------------
 */















































Tcl_Obj *
TclStringObjReverse(
    Tcl_Obj *objPtr)
{
    String *stringPtr;
    char *src = NULL, *dest = NULL;



    Tcl_UniChar *usrc = NULL, *udest = NULL;

    Tcl_Obj *resultPtr = NULL;





    SetStringFromAny(NULL, objPtr);
    stringPtr = GET_STRING(objPtr);

    if (stringPtr->hasUnicode == 0) {
	if (stringPtr->numChars == -1) {
	    TclNumUtfChars(stringPtr->numChars, objPtr->bytes, objPtr->length);
	}
	if (stringPtr->numChars <= 1) {
	    return objPtr;
	}
	if (stringPtr->numChars == objPtr->length) {
	    /*
	     * All one-byte chars. Reverse in objPtr->bytes.
	     */

	    if (Tcl_IsShared(objPtr)) {
		resultPtr = Tcl_NewObj();
		Tcl_SetObjLength(resultPtr, objPtr->length);
		dest = TclGetString(resultPtr);
		src = objPtr->bytes + objPtr->length - 1;
		while (src >= objPtr->bytes) {
		    *dest++ = *src--;
		}
		return resultPtr;
	    }

	    /*
	     * Unshared. Reverse objPtr->bytes in place.
	     */

	    dest = objPtr->bytes;
	    src = dest + objPtr->length - 1;
	    while (dest < src) {
		char tmp = *src;

		*src-- = *dest;
		*dest++ = tmp;
	    }
	    return objPtr;
	}
	FillUnicodeRep(objPtr);
	stringPtr = GET_STRING(objPtr);
    }
    if (stringPtr->numChars <= 1) {
	return objPtr;
    }

    /*
     * Reverse the Unicode rep.
     */

    if (Tcl_IsShared(objPtr)) {
	Tcl_UniChar ch = 0;

	/*
	 * Create a non-empty, pure unicode value, so we can coax
	 * Tcl_SetObjLength into growing the unicode rep buffer.
	 */


	resultPtr = Tcl_NewUnicodeObj(&ch, 1);
	Tcl_SetObjLength(resultPtr, stringPtr->numChars);

	udest = Tcl_GetUnicode(resultPtr);

	usrc = stringPtr->unicode + stringPtr->numChars - 1;

	while (usrc >= stringPtr->unicode) {
	    *udest++ = *usrc--;


	}

	return resultPtr;

    }



    /*

     * Unshared. Reverse objPtr->bytes in place.




     */



    udest = stringPtr->unicode;
    usrc = udest + stringPtr->numChars - 1;
    while (udest < usrc) {





	Tcl_UniChar tmp = *usrc;



	*usrc-- = *udest;



	*udest++ = tmp;
    }

    TclInvalidateStringRep(objPtr);
    stringPtr->allocated = 0;





    return objPtr;
}

/*
 *---------------------------------------------------------------------------
 *
 * FillUnicodeRep --








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>





|
>
>
>
|
>
|
>
>
>
>




|
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
|
|
|
|

>
|
|
>
|
>
|
>
|
<
>
>
|
>
|
>
|
>

>
|
>
|
>
>
>
>
|
>
>

<
<
|
>
>
>
>
>
|
>

>
|
>
>
>
|
|

|
|
>
>
>
>
>







2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722










2723







2724




























2725


2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739

2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760


2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
 *	argument with modifications done in place.
 *
 * Side effects:
 *	May allocate a new Tcl_Obj.
 *
 *---------------------------------------------------------------------------
 */

void
ReverseBytes(
    unsigned char *to,		/* Copy bytes into here... */
    unsigned char *from,	/* ...from here... */
    int count)		/* Until this many are copied, */
				/* reversing as you go. */
{
    if (to == from) {
	/* Reversing in place */
	from += count - 1;
	while (to < from) {
	    unsigned char c = *from;
	    *from-- = *to;
	    *to++ = c;
	}
    }  else {
	from += count - 1;
	while (count--) {
	    *to++ = *from--;
	}
    }
}

void
ReverseUniChars(
    Tcl_UniChar *to,		/* Copy Tcl_UniChars into here... */
    Tcl_UniChar *from,		/* ...from here... */
    unsigned int count)		/* Until this many are copied, */
				/* reversing as you go. */
{
    if (to == from) {
	/* Reversing in place */
	from += count - 1;
	while (to < from) {
	    Tcl_UniChar c = *from;
	    *from-- = *to;
	    *to++ = c;
	}
    }  else {
	from += count - 1;
	while (count--) {
	    *to++ = *from--;
	}
    }
}

Tcl_Obj *
TclStringObjReverse(
    Tcl_Obj *objPtr)
{
    String *stringPtr;

    if (TclIsPureByteArray(objPtr)) {
	int numBytes;
	unsigned char *from = Tcl_GetByteArrayFromObj(objPtr, &numBytes);

	if (Tcl_IsShared(objPtr)) {
	    objPtr = Tcl_NewByteArrayObj(NULL, numBytes);
	}
	ReverseBytes(Tcl_GetByteArrayFromObj(objPtr, NULL), from, numBytes);
	return objPtr;
    }

    SetStringFromAny(NULL, objPtr);
    stringPtr = GET_STRING(objPtr);

    if (stringPtr->hasUnicode) {










	Tcl_UniChar *from = Tcl_GetUnicode(objPtr);




































	if (Tcl_IsShared(objPtr)) {


	    /*
	     * Create a non-empty, pure unicode value, so we can coax
	     * Tcl_SetObjLength into growing the unicode rep buffer.
	     */

	    Tcl_UniChar ch = 0;
	    objPtr = Tcl_NewUnicodeObj(&ch, 1);
	    Tcl_SetObjLength(objPtr, stringPtr->numChars);
	}
	ReverseUniChars(Tcl_GetUnicode(objPtr), from, stringPtr->numChars);
    }

    if (objPtr->bytes) {
	int numChars = stringPtr->numChars;

	int numBytes = objPtr->length;
	char *to, *from = objPtr->bytes;

	if (Tcl_IsShared(objPtr)) {
	    objPtr = Tcl_NewObj();
	    Tcl_SetObjLength(objPtr, numBytes);
	}
	to = objPtr->bytes;

	if (numChars < numBytes) {
	    /*
	     * Either numChars == -1 and we don't know how many chars are
	     * represented by objPtr->bytes and we need Pass 1 just in case,
	     * or numChars >= 0 and we know we have fewer chars than bytes,
	     * so we know there's a multibyte character needing Pass 1.
	     *
	     * Pass 1. Reverse the bytes of each multi-byte character.
	     */
	    int charCount = 0;
	    int bytesLeft = numBytes;



	    while (bytesLeft) {
		/*
		 * NOTE: We know that the from buffer is NUL-terminated.
		 * It's part of the contract for objPtr->bytes values.
		 * Thus, we can skip calling Tcl_UtfCharComplete() here.
		 */
		Tcl_UniChar ch = 0;
		int bytesInChar = Tcl_UtfToUniChar(from, &ch);

		ReverseBytes((unsigned char *)to, (unsigned char *)from,
			bytesInChar);
		to += bytesInChar;
		from += bytesInChar;
		bytesLeft -= bytesInChar;
		charCount++;
	    }

	    from = to = objPtr->bytes;
	    stringPtr->numChars = charCount;
	}
	/* Pass 2. Reverse all the bytes. */
	ReverseBytes((unsigned char *)to, (unsigned char *)from, numBytes);
    }

    return objPtr;
}

/*
 *---------------------------------------------------------------------------
 *
 * FillUnicodeRep --

Changes to tests/string.test.

1619
1620
1621
1622
1623
1624
1625








1626
1627
1628
1629
1630
1631
1632
    string reverse $x$y
} \udead\ubeef
test string-24.12 {string reverse command - corner case} {
    set x \ubeef
    set y \udead
    string is ascii [string reverse $x$y]
} 0









test string-25.1 {string is list} {
    string is list {a b c}
} 1
test string-25.2 {string is list} {
    string is list "a \{b c"
} 0







>
>
>
>
>
>
>
>







1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
    string reverse $x$y
} \udead\ubeef
test string-24.12 {string reverse command - corner case} {
    set x \ubeef
    set y \udead
    string is ascii [string reverse $x$y]
} 0
test string-24.13 {string reverse command - pure bytearray} {
    binary scan [string reverse [binary format H* 010203]] H* x
    set x
} 030201
test string-24.14 {string reverse command - pure bytearray} {
    binary scan [tcl::string::reverse [binary format H* 010203]] H* x
    set x
} 030201

test string-25.1 {string is list} {
    string is list {a b c}
} 1
test string-25.2 {string is list} {
    string is list "a \{b c"
} 0