Skip to content

Commit 8947f09

Browse files
committed
tighter code layout
1 parent 901a957 commit 8947f09

File tree

1 file changed

+15
-35
lines changed

1 file changed

+15
-35
lines changed

ext/json/json_encoder.c

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ zend_always_inline int php_json_sse42_compute_escape_intersection_real(const __m
519519
}
520520
#endif
521521

522-
#ifdef ZEND_INTRIN_SSE4_2_FUNC_PROTO
522+
#if defined(JSON_USE_SIMD) && defined(ZEND_INTRIN_SSE4_2_FUNC_PROTO)
523523
static int php_json_sse42_compute_escape_intersection(const __m128i mask, const __m128i input) __attribute__((ifunc("php_json_resolve_escape_intersection")));
524524

525525
typedef int (*php_json_compute_escape_intersection_t)(const __m128i mask, const __m128i input);
@@ -534,13 +534,13 @@ static php_json_compute_escape_intersection_t php_json_resolve_escape_intersecti
534534
}
535535
#endif
536536

537-
#ifdef JSON_USE_SIMD
538537
typedef enum php_json_simd_result {
539538
PHP_JSON_STOP,
540539
PHP_JSON_SLOW,
541540
PHP_JSON_NON_ASCII,
542541
} php_json_simd_result;
543542

543+
#ifdef JSON_USE_SIMD
544544
static zend_always_inline php_json_simd_result php_json_process_simd_block(
545545
smart_str *buf,
546546
const __m128i sse_escape_mask,
@@ -585,40 +585,18 @@ static zend_always_inline php_json_simd_result php_json_process_simd_block(
585585
*s += *pos;
586586
const char *s_backup = *s;
587587

588-
do {
589-
/* Note that we shift the input forward, so we have to shift the mask as well,
590-
* beyond the to-be-escaped character */
591-
int len = zend_ulong_ntz(mask);
592-
#if 1
593-
mask >>= len + 1;
594-
595-
php_json_append(buf, *s, len);
596-
597-
unsigned char us = (unsigned char)(*s)[len];
598-
*s += len + 1; /* skip 'us' too */
599-
600-
bool handled = php_json_printable_ascii_escape(buf, us, options);
601-
ZEND_ASSERT(handled == true);
602-
#else
603-
mask >>= len;
604-
605-
php_json_append(buf, *s, len);
606-
607-
*s += len; /* skip 'us' too */
608-
609-
/* Mitigate long run performance */
610-
do {
611-
unsigned char us = (unsigned char)(*s)[0];
612-
(*s)++;
613-
bool handled = php_json_printable_ascii_escape(buf, us, options);
614-
ZEND_ASSERT(handled == true);
615-
} while ((mask >>= 1) & 1);
616-
#endif
617-
} while (mask != 0);
588+
for (; mask; mask >>= 1, *s += 1) {
589+
if (UNEXPECTED(mask & 1)) {
590+
bool handled = php_json_printable_ascii_escape(buf, (*s)[0], options);
591+
ZEND_ASSERT(handled);
592+
} else {
593+
smart_str_appendc(buf, (*s)[0]);
594+
}
595+
}
618596

619597
*pos = sizeof(__m128i) - (*s - s_backup);
620598
} else {
621-
if (/*UNEXPECTED*/(max_shift < sizeof(__m128i))) {
599+
if (max_shift < sizeof(__m128i)) {
622600
*pos += max_shift;
623601
*len -= max_shift;
624602
return PHP_JSON_SLOW;
@@ -670,10 +648,12 @@ zend_result php_json_escape_string(
670648

671649
pos = 0;
672650

673-
#if defined(ZEND_INTRIN_SSE4_2_NATIVE) || defined(ZEND_INTRIN_SSE4_2_FUNC_PROTO)
651+
#ifdef JSON_USE_SIMD
652+
# if defined(ZEND_INTRIN_SSE4_2_NATIVE) || defined(ZEND_INTRIN_SSE4_2_FUNC_PROTO)
674653
const __m128i sse_escape_mask = php_json_create_sse_escape_mask(options);
675-
#elif defined(JSON_USE_SIMD)
654+
# else
676655
const __m128i sse_escape_mask = _mm_setzero_si128();
656+
# endif
677657
#endif
678658

679659
do {

0 commit comments

Comments
 (0)