Skip to content

Commit 6fe75aa

Browse files
committed
Optimization for fast path.
1 parent e0403eb commit 6fe75aa

File tree

1 file changed

+83
-80
lines changed

1 file changed

+83
-80
lines changed

ext/json/json_encoder.c

Lines changed: 83 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -329,90 +329,93 @@ static int php_json_escape_string(
329329
smart_str_appendc(buf, digits[(us & 0xf0) >> 4]);
330330
smart_str_appendc(buf, digits[(us & 0xf)]);
331331
} else {
332-
pos++;
333-
334-
switch (us) {
335-
case '"':
336-
if (options & PHP_JSON_HEX_QUOT) {
337-
smart_str_appendl(buf, "\\u0022", 6);
338-
} else {
339-
smart_str_appendl(buf, "\\\"", 2);
340-
}
341-
break;
342-
343-
case '\\':
344-
smart_str_appendl(buf, "\\\\", 2);
345-
break;
346-
347-
case '/':
348-
if (options & PHP_JSON_UNESCAPED_SLASHES) {
349-
smart_str_appendc(buf, '/');
350-
} else {
351-
smart_str_appendl(buf, "\\/", 2);
352-
}
353-
break;
354-
355-
case '\b':
356-
smart_str_appendl(buf, "\\b", 2);
357-
break;
358-
359-
case '\f':
360-
smart_str_appendl(buf, "\\f", 2);
361-
break;
362-
363-
case '\n':
364-
smart_str_appendl(buf, "\\n", 2);
365-
break;
366-
367-
case '\r':
368-
smart_str_appendl(buf, "\\r", 2);
369-
break;
370-
371-
case '\t':
372-
smart_str_appendl(buf, "\\t", 2);
373-
break;
374-
375-
case '<':
376-
if (options & PHP_JSON_HEX_TAG) {
377-
smart_str_appendl(buf, "\\u003C", 6);
378-
} else {
379-
smart_str_appendc(buf, '<');
380-
}
381-
break;
382-
383-
case '>':
384-
if (options & PHP_JSON_HEX_TAG) {
385-
smart_str_appendl(buf, "\\u003E", 6);
386-
} else {
387-
smart_str_appendc(buf, '>');
388-
}
389-
break;
332+
static const uint32_t charmap[4] = {
333+
0xffffffff, 0x500080c4, 0x10000000, 0x00000000};
390334

391-
case '&':
392-
if (options & PHP_JSON_HEX_AMP) {
393-
smart_str_appendl(buf, "\\u0026", 6);
394-
} else {
395-
smart_str_appendc(buf, '&');
396-
}
397-
break;
398-
399-
case '\'':
400-
if (options & PHP_JSON_HEX_APOS) {
401-
smart_str_appendl(buf, "\\u0027", 6);
402-
} else {
403-
smart_str_appendc(buf, '\'');
404-
}
405-
break;
406-
407-
default:
408-
if (us >= ' ') {
409-
smart_str_appendc(buf, (unsigned char) us);
410-
} else {
335+
pos++;
336+
if (EXPECTED(!(charmap[us >> 5] & (1 << (us & 0x1f))))) {
337+
smart_str_appendc(buf, (unsigned char) us);
338+
} else {
339+
switch (us) {
340+
case '"':
341+
if (options & PHP_JSON_HEX_QUOT) {
342+
smart_str_appendl(buf, "\\u0022", 6);
343+
} else {
344+
smart_str_appendl(buf, "\\\"", 2);
345+
}
346+
break;
347+
348+
case '\\':
349+
smart_str_appendl(buf, "\\\\", 2);
350+
break;
351+
352+
case '/':
353+
if (options & PHP_JSON_UNESCAPED_SLASHES) {
354+
smart_str_appendc(buf, '/');
355+
} else {
356+
smart_str_appendl(buf, "\\/", 2);
357+
}
358+
break;
359+
360+
case '\b':
361+
smart_str_appendl(buf, "\\b", 2);
362+
break;
363+
364+
case '\f':
365+
smart_str_appendl(buf, "\\f", 2);
366+
break;
367+
368+
case '\n':
369+
smart_str_appendl(buf, "\\n", 2);
370+
break;
371+
372+
case '\r':
373+
smart_str_appendl(buf, "\\r", 2);
374+
break;
375+
376+
case '\t':
377+
smart_str_appendl(buf, "\\t", 2);
378+
break;
379+
380+
case '<':
381+
if (options & PHP_JSON_HEX_TAG) {
382+
smart_str_appendl(buf, "\\u003C", 6);
383+
} else {
384+
smart_str_appendc(buf, '<');
385+
}
386+
break;
387+
388+
case '>':
389+
if (options & PHP_JSON_HEX_TAG) {
390+
smart_str_appendl(buf, "\\u003E", 6);
391+
} else {
392+
smart_str_appendc(buf, '>');
393+
}
394+
break;
395+
396+
case '&':
397+
if (options & PHP_JSON_HEX_AMP) {
398+
smart_str_appendl(buf, "\\u0026", 6);
399+
} else {
400+
smart_str_appendc(buf, '&');
401+
}
402+
break;
403+
404+
case '\'':
405+
if (options & PHP_JSON_HEX_APOS) {
406+
smart_str_appendl(buf, "\\u0027", 6);
407+
} else {
408+
smart_str_appendc(buf, '\'');
409+
}
410+
break;
411+
412+
default:
413+
ZEND_ASSERT(us < ' ');
411414
smart_str_appendl(buf, "\\u00", sizeof("\\u00")-1);
412415
smart_str_appendc(buf, digits[(us & 0xf0) >> 4]);
413416
smart_str_appendc(buf, digits[(us & 0xf)]);
414-
}
415-
break;
417+
break;
418+
}
416419
}
417420
}
418421
} while (pos < len);

0 commit comments

Comments
 (0)