Skip to content

Commit 067df26

Browse files
committed
Use memrchr() when available
On x86_64 glibc memrchr() uses SSE/AVX CPU extensions and works much faster then naive loop. On x86 32-bit we still use inlined version. memrchr() is a GNU extension. Its prototype becomes available when <string.h> is included with defined _GNU_SOURCE macro. Previously, we defined it in "php_config.h", but some sources may include <string.h> befire it. To avod mess we also pass -D_GNU_SOURCE to C compiler.
1 parent d312a0c commit 067df26

File tree

11 files changed

+39
-9
lines changed

11 files changed

+39
-9
lines changed

Zend/zend_operators.h

+5
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ zend_memnstr(const char *haystack, const char *needle, size_t needle_len, const
211211

212212
static zend_always_inline const void *zend_memrchr(const void *s, int c, size_t n)
213213
{
214+
#if defined(HAVE_MEMRCHR) && !defined(i386)
215+
/* On x86 memrchr() doesn't use SSE/AVX, so inlined version is faster */
216+
return (const void*)memrchr(s, c, n);
217+
#else
214218
const unsigned char *e;
215219
if (0 == n) {
216220
return NULL;
@@ -222,6 +226,7 @@ static zend_always_inline const void *zend_memrchr(const void *s, int c, size_t
222226
}
223227
}
224228
return NULL;
229+
#endif
225230
}
226231

227232

Zend/zend_signal.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
All other licensing and usage conditions are those of the PHP Group.
2626
*/
2727

28-
#define _GNU_SOURCE
28+
#ifndef _GNU_SOURCE
29+
# define _GNU_SOURCE
30+
#endif
2931
#include <string.h>
3032

3133
#include "zend.h"

configure.ac

+7
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,12 @@ else
265265
AC_MSG_RESULT(no)
266266
fi
267267

268+
dnl The effect of _GNU_SOURCE defined in config.h depeds on includes order
269+
if test "$ac_cv_safe_to_define___extensions__" = yes ; then
270+
AC_MSG_CHECKING(whether to use -D_GNU_SOURCE cflag)
271+
CPPFLAGS="$CPPFLAGS -D_GNU_SOURCE"
272+
AC_MSG_RESULT(yes)
273+
fi
268274

269275
dnl Include Zend configurations.
270276
dnl ----------------------------------------------------------------------------
@@ -609,6 +615,7 @@ vasprintf \
609615
asprintf \
610616
nanosleep \
611617
memmem \
618+
memrchr \
612619
)
613620

614621
AX_FUNC_WHICH_GETHOSTBYNAME_R

ext/pdo_firebird/firebird_driver.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
#include "config.h"
1919
#endif
2020

21-
#define _GNU_SOURCE
21+
#ifndef _GNU_SOURCE
22+
# define _GNU_SOURCE
23+
#endif
2224

2325
#include "php.h"
2426
#include "zend_exceptions.h"

ext/zlib/zlib_fopen_wrapper.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
+----------------------------------------------------------------------+
1616
*/
1717

18-
#define _GNU_SOURCE
18+
#ifndef _GNU_SOURCE
19+
# define _GNU_SOURCE
20+
#endif
1921

2022
#include "php.h"
2123
#include "php_zlib.h"

main/snprintf.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
+----------------------------------------------------------------------+
1515
*/
1616

17-
#define _GNU_SOURCE
17+
#ifndef _GNU_SOURCE
18+
# define _GNU_SOURCE
19+
#endif
1820
#include "php.h"
1921

2022
#include <zend_strtod.h>

main/spprintf.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@
7272
* SIO stdio-replacement strx_* functions by Panos Tsirigotis
7373
* <panos@alumni.cs.colorado.edu> for xinetd.
7474
*/
75-
#define _GNU_SOURCE
75+
#ifndef _GNU_SOURCE
76+
# define _GNU_SOURCE
77+
#endif
7678
#include "php.h"
7779

7880
#include <stddef.h>

main/streams/cast.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
+----------------------------------------------------------------------+
1515
*/
1616

17-
#define _GNU_SOURCE
17+
#ifndef _GNU_SOURCE
18+
# define _GNU_SOURCE
19+
#endif
1820
#include "php.h"
1921
#include "php_globals.h"
2022
#include "php_network.h"

main/streams/memory.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
+----------------------------------------------------------------------+
1515
*/
1616

17-
#define _GNU_SOURCE
17+
#ifndef _GNU_SOURCE
18+
# define _GNU_SOURCE
19+
#endif
1820
#include "php.h"
1921
#include "ext/standard/base64.h"
2022

main/streams/streams.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
+----------------------------------------------------------------------+
1818
*/
1919

20-
#define _GNU_SOURCE
20+
#ifndef _GNU_SOURCE
21+
# define _GNU_SOURCE
22+
#endif
2123
#include "php.h"
2224
#include "php_globals.h"
2325
#include "php_memory_streams.h"

sapi/fpm/fpm/fpm_trace_pread.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* (c) 2007,2008 Andrei Nigmatulin */
22

3-
#define _GNU_SOURCE
3+
#ifndef _GNU_SOURCE
4+
# define _GNU_SOURCE
5+
#endif
46
#define _FILE_OFFSET_BITS 64
57

68
#include "fpm_config.h"

0 commit comments

Comments
 (0)