Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cd777ea

Browse files
committed
Issue #17615: Comparing two Unicode strings now uses wmemcmp() when possible
wmemcmp() is twice faster than a dummy loop (342 usec vs 744 usec) on Fedora 18/x86_64, GCC 4.7.2.
1 parent 9fc5981 commit cd777ea

5 files changed

Lines changed: 30 additions & 5 deletions

File tree

Objects/unicodeobject.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10304,8 +10304,19 @@ unicode_compare(PyObject *str1, PyObject *str2)
1030410304
COMPARE(Py_UCS2, Py_UCS1);
1030510305
break;
1030610306
case PyUnicode_2BYTE_KIND:
10307+
{
10308+
#if defined(HAVE_WMEMCMP) && SIZEOF_WCHAR_T == 2
10309+
int cmp = wmemcmp((wchar_t *)data1, (wchar_t *)data2, len);
10310+
/* normalize result of wmemcmp() into the range [-1; 1] */
10311+
if (cmp < 0)
10312+
return -1;
10313+
if (cmp > 0)
10314+
return 1;
10315+
#else
1030710316
COMPARE(Py_UCS2, Py_UCS2);
10317+
#endif
1030810318
break;
10319+
}
1030910320
case PyUnicode_4BYTE_KIND:
1031010321
COMPARE(Py_UCS2, Py_UCS4);
1031110322
break;
@@ -10324,8 +10335,19 @@ unicode_compare(PyObject *str1, PyObject *str2)
1032410335
COMPARE(Py_UCS4, Py_UCS2);
1032510336
break;
1032610337
case PyUnicode_4BYTE_KIND:
10338+
{
10339+
#if defined(HAVE_WMEMCMP) && SIZEOF_WCHAR_T == 4
10340+
int cmp = wmemcmp((wchar_t *)data1, (wchar_t *)data2, len);
10341+
/* normalize result of wmemcmp() into the range [-1; 1] */
10342+
if (cmp < 0)
10343+
return -1;
10344+
if (cmp > 0)
10345+
return 1;
10346+
#else
1032710347
COMPARE(Py_UCS4, Py_UCS4);
10348+
#endif
1032810349
break;
10350+
}
1032910351
default:
1033010352
assert(0);
1033110353
}

PC/pyconfig.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,9 @@ Py_NO_ENABLE_SHARED to find out. Also support MS_NO_COREDLL for b/w compat */
645645
#define HAVE_WCSXFRM 1
646646
#endif
647647

648+
/* Define to 1 if you have the `wmemcmp' function. */
649+
#define HAVE_WMEMCMP 1
650+
648651
/* Define if the zlib library has inflateCopy */
649652
#define HAVE_ZLIB_COPY 1
650653

configure

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10273,7 +10273,7 @@ for ac_func in alarm accept4 setitimer getitimer bind_textdomain_codeset chown \
1027310273
sigtimedwait sigwait sigwaitinfo snprintf strftime strlcpy symlinkat sync \
1027410274
sysconf tcgetpgrp tcsetpgrp tempnam timegm times tmpfile tmpnam tmpnam_r \
1027510275
truncate uname unlinkat unsetenv utimensat utimes waitid waitpid wait3 wait4 \
10276-
wcscoll wcsftime wcsxfrm writev _getpty
10276+
wcscoll wcsftime wcsxfrm wmemcmp writev _getpty
1027710277
do :
1027810278
as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
1027910279
ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2816,7 +2816,7 @@ AC_CHECK_FUNCS(alarm accept4 setitimer getitimer bind_textdomain_codeset chown \
28162816
sigtimedwait sigwait sigwaitinfo snprintf strftime strlcpy symlinkat sync \
28172817
sysconf tcgetpgrp tcsetpgrp tempnam timegm times tmpfile tmpnam tmpnam_r \
28182818
truncate uname unlinkat unsetenv utimensat utimes waitid waitpid wait3 wait4 \
2819-
wcscoll wcsftime wcsxfrm writev _getpty)
2819+
wcscoll wcsftime wcsxfrm wmemcmp writev _getpty)
28202820

28212821
AC_CHECK_DECL(dirfd,
28222822
AC_DEFINE(HAVE_DIRFD, 1,

pyconfig.h.in

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,9 @@
11181118
/* Define to 1 if you have the `wcsxfrm' function. */
11191119
#undef HAVE_WCSXFRM
11201120

1121+
/* Define to 1 if you have the `wmemcmp' function. */
1122+
#undef HAVE_WMEMCMP
1123+
11211124
/* Define if tzset() actually switches the local timezone in a meaningful way.
11221125
*/
11231126
#undef HAVE_WORKING_TZSET
@@ -1190,9 +1193,6 @@
11901193
/* Define if setpgrp() must be called as setpgrp(0, 0). */
11911194
#undef SETPGRP_HAVE_ARG
11921195

1193-
/* Define this to be extension of shared libraries (including the dot!). */
1194-
#undef SHLIB_EXT
1195-
11961196
/* Define if i>>j for signed int i does not extend the sign bit when i < 0 */
11971197
#undef SIGNED_RIGHT_SHIFT_ZERO_FILLS
11981198

0 commit comments

Comments
 (0)