Skip to content

Commit fdc8ff3

Browse files
committed
Make the tables extern instead of using a non-inline function
3.7x faster for a strripos benchmark which does a tight loop of tolower().
1 parent 4a5c7a3 commit fdc8ff3

File tree

2 files changed

+20
-28
lines changed

2 files changed

+20
-28
lines changed

Zend/zend_operators.c

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static _locale_t current_locale = NULL;
5454

5555
#define TYPE_PAIR(t1,t2) (((t1) << 4) | (t2))
5656

57-
static const unsigned char tolower_map[256] = {
57+
const unsigned char zend_tolower_map[256] = {
5858
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
5959
0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,
6060
0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,
@@ -73,7 +73,7 @@ static const unsigned char tolower_map[256] = {
7373
0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
7474
};
7575

76-
static const unsigned char toupper_map[256] = {
76+
const unsigned char zend_toupper_map[256] = {
7777
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
7878
0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,
7979
0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,
@@ -92,8 +92,6 @@ static const unsigned char toupper_map[256] = {
9292
0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
9393
};
9494

95-
#define i_zend_tolower_ascii(c) (tolower_map[(unsigned char)(c)])
96-
#define i_zend_toupper_ascii(c) (toupper_map[(unsigned char)(c)])
9795

9896
/**
9997
* Functions using locale lowercase:
@@ -2706,7 +2704,7 @@ static zend_always_inline void zend_str_tolower_impl(char *dest, const char *str
27062704
}
27072705
#endif
27082706
while (p < end) {
2709-
*q++ = i_zend_tolower_ascii(*p++);
2707+
*q++ = zend_tolower_ascii(*p++);
27102708
}
27112709
}
27122710
/* }}} */
@@ -2734,23 +2732,11 @@ static zend_always_inline void zend_str_toupper_impl(char *dest, const char *str
27342732
}
27352733
#endif
27362734
while (p < end) {
2737-
*q++ = i_zend_toupper_ascii(*p++);
2735+
*q++ = zend_toupper_ascii(*p++);
27382736
}
27392737
}
27402738
/* }}} */
27412739

2742-
ZEND_API int ZEND_FASTCALL zend_tolower_ascii(int c) /* {{{ */
2743-
{
2744-
return i_zend_tolower_ascii(c);
2745-
}
2746-
/* }}} */
2747-
2748-
ZEND_API int ZEND_FASTCALL zend_toupper_ascii(int c) /* {{{ */
2749-
{
2750-
return i_zend_toupper_ascii(c);
2751-
}
2752-
/* }}} */
2753-
27542740
ZEND_API char* ZEND_FASTCALL zend_str_tolower_copy(char *dest, const char *source, size_t length) /* {{{ */
27552741
{
27562742
zend_str_tolower_impl(dest, source, length);
@@ -2798,7 +2784,7 @@ ZEND_API char* ZEND_FASTCALL zend_str_tolower_dup_ex(const char *source, size_t
27982784
const unsigned char *end = p + length;
27992785

28002786
while (p < end) {
2801-
if (*p != i_zend_tolower_ascii(*p)) {
2787+
if (*p != zend_tolower_ascii(*p)) {
28022788
char *res = (char*)emalloc(length + 1);
28032789
unsigned char *r;
28042790

@@ -2822,7 +2808,7 @@ ZEND_API char* ZEND_FASTCALL zend_str_toupper_dup_ex(const char *source, size_t
28222808
const unsigned char *end = p + length;
28232809

28242810
while (p < end) {
2825-
if (*p != i_zend_toupper_ascii(*p)) {
2811+
if (*p != zend_toupper_ascii(*p)) {
28262812
char *res = (char*)emalloc(length + 1);
28272813
unsigned char *r;
28282814

@@ -2876,13 +2862,13 @@ ZEND_API zend_string* ZEND_FASTCALL zend_string_tolower_ex(zend_string *str, boo
28762862
#endif
28772863

28782864
while (p < end) {
2879-
if (*p != i_zend_tolower_ascii(*p)) {
2865+
if (*p != zend_tolower_ascii(*p)) {
28802866
zend_string *res = zend_string_alloc(length, persistent);
28812867
memcpy(ZSTR_VAL(res), ZSTR_VAL(str), p - (unsigned char*) ZSTR_VAL(str));
28822868

28832869
unsigned char *q = p + (ZSTR_VAL(res) - ZSTR_VAL(str));
28842870
while (p < end) {
2885-
*q++ = i_zend_tolower_ascii(*p++);
2871+
*q++ = zend_tolower_ascii(*p++);
28862872
}
28872873
ZSTR_VAL(res)[length] = '\0';
28882874
return res;
@@ -2930,13 +2916,13 @@ ZEND_API zend_string* ZEND_FASTCALL zend_string_toupper_ex(zend_string *str, boo
29302916
#endif
29312917

29322918
while (p < end) {
2933-
if (*p != i_zend_toupper_ascii(*p)) {
2919+
if (*p != zend_toupper_ascii(*p)) {
29342920
zend_string *res = zend_string_alloc(length, persistent);
29352921
memcpy(ZSTR_VAL(res), ZSTR_VAL(str), p - (unsigned char*) ZSTR_VAL(str));
29362922

29372923
unsigned char *q = p + (ZSTR_VAL(res) - ZSTR_VAL(str));
29382924
while (p < end) {
2939-
*q++ = i_zend_toupper_ascii(*p++);
2925+
*q++ = zend_toupper_ascii(*p++);
29402926
}
29412927
ZSTR_VAL(res)[length] = '\0';
29422928
return res;
@@ -2991,8 +2977,8 @@ ZEND_API int ZEND_FASTCALL zend_binary_strcasecmp(const char *s1, size_t len1, c
29912977

29922978
len = MIN(len1, len2);
29932979
while (len--) {
2994-
c1 = i_zend_tolower_ascii(*(unsigned char *)s1++);
2995-
c2 = i_zend_tolower_ascii(*(unsigned char *)s2++);
2980+
c1 = zend_tolower_ascii(*(unsigned char *)s1++);
2981+
c2 = zend_tolower_ascii(*(unsigned char *)s2++);
29962982
if (c1 != c2) {
29972983
return c1 - c2;
29982984
}
@@ -3012,8 +2998,8 @@ ZEND_API int ZEND_FASTCALL zend_binary_strncasecmp(const char *s1, size_t len1,
30122998
}
30132999
len = MIN(length, MIN(len1, len2));
30143000
while (len--) {
3015-
c1 = i_zend_tolower_ascii(*(unsigned char *)s1++);
3016-
c2 = i_zend_tolower_ascii(*(unsigned char *)s2++);
3001+
c1 = zend_tolower_ascii(*(unsigned char *)s1++);
3002+
c2 = zend_tolower_ascii(*(unsigned char *)s2++);
30173003
if (c1 != c2) {
30183004
return c1 - c2;
30193005
}

Zend/zend_operators.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,12 @@ ZEND_API int ZEND_FASTCALL string_locale_compare_function(zval *op1, zval *op2);
436436
ZEND_API int ZEND_FASTCALL zend_tolower_ascii(int c);
437437
ZEND_API int ZEND_FASTCALL zend_toupper_ascii(int c);
438438

439+
ZEND_API extern const unsigned char zend_tolower_map[256];
440+
ZEND_API extern const unsigned char zend_toupper_map[256];
441+
442+
#define zend_tolower_ascii(c) (zend_tolower_map[(unsigned char)(c)])
443+
#define zend_toupper_ascii(c) (zend_toupper_map[(unsigned char)(c)])
444+
439445
ZEND_API void ZEND_FASTCALL zend_str_tolower(char *str, size_t length);
440446
ZEND_API void ZEND_FASTCALL zend_str_toupper(char *str, size_t length);
441447
ZEND_API char* ZEND_FASTCALL zend_str_tolower_copy(char *dest, const char *source, size_t length);

0 commit comments

Comments
 (0)