Skip to content

Commit c36d0b6

Browse files
committed
Skip the UTF-8 loop for ASCII identifiers in the lexer
Nearly all identifiers are ASCII, but each one entered the UTF-8 loop and left it on the first pass. The loop now runs only when the byte after the ASCII sequence can start a multibyte character. The accepted input doesn't change. With the tracing JIT on PHP 8.5, this makes lexing the corpus about 14% faster, and lexing and parsing about 5% faster. Without the JIT, the speed doesn't change measurably.
1 parent 3998ff1 commit c36d0b6

1 file changed

Lines changed: 76 additions & 63 deletions

File tree

‎packages/mysql-parser/src/class-wp-mysql-lexer.php‎

Lines changed: 76 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,74 +1059,87 @@ private function get_current_token_bytes(): string {
10591059
*/
10601060
private function read_identifier(): ?int {
10611061
$started_at = $this->bytes_already_read;
1062-
while ( true ) {
1063-
// First, let's try to parse an ASCII sequence.
1064-
$this->bytes_already_read += strspn(
1065-
$this->sql,
1066-
self::IDENTIFIER_MASK,
1067-
$this->bytes_already_read
1068-
);
10691062

1070-
// Check if the following byte can be part of a multibyte character
1071-
// in the range of U+0080 to U+10FFFF before looking at further bytes.
1072-
// If it can't, bail out early to avoid unnecessary UTF-8 decoding.
1073-
// Identifiers are usually ASCII-only, so we can optimize for that.
1074-
$byte_1 = ord(
1075-
$this->sql[ $this->bytes_already_read ] ?? "\0"
1076-
);
1077-
if ( $byte_1 < 0xC2 || $byte_1 > 0xF4 ) {
1078-
break;
1079-
}
1063+
// Identifiers are usually ASCII-only, so only enter the loop below when the
1064+
// byte after the ASCII sequence can start a multibyte character. With the
1065+
// tracing JIT, entering the loop for every identifier slows down the lexer.
1066+
$this->bytes_already_read += strspn(
1067+
$this->sql,
1068+
self::IDENTIFIER_MASK,
1069+
$this->bytes_already_read
1070+
);
1071+
$byte_1 = ord(
1072+
$this->sql[ $this->bytes_already_read ] ?? "\0"
1073+
);
1074+
if ( $byte_1 >= 0xC2 && $byte_1 <= 0xF4 ) {
1075+
while ( true ) {
1076+
// First, let's try to parse an ASCII sequence.
1077+
$this->bytes_already_read += strspn(
1078+
$this->sql,
1079+
self::IDENTIFIER_MASK,
1080+
$this->bytes_already_read
1081+
);
10801082

1081-
// Look for a valid 2-byte UTF-8 symbol. Covers range U+0080 - U+07FF.
1082-
$byte_2 = ord(
1083-
$this->sql[ $this->bytes_already_read + 1 ] ?? "\0"
1084-
);
1085-
if (
1086-
$byte_1 <= 0xDF
1087-
&& $byte_2 >= 0x80 && $byte_2 <= 0xBF
1088-
) {
1089-
$this->bytes_already_read += 2;
1090-
continue;
1091-
}
1083+
// Check if the following byte can be part of a multibyte character
1084+
// in the range of U+0080 to U+10FFFF before looking at further bytes.
1085+
// If it can't, bail out early to avoid unnecessary UTF-8 decoding.
1086+
$byte_1 = ord(
1087+
$this->sql[ $this->bytes_already_read ] ?? "\0"
1088+
);
1089+
if ( $byte_1 < 0xC2 || $byte_1 > 0xF4 ) {
1090+
break;
1091+
}
10921092

1093-
// Look for a valid 3-byte UTF-8 symbol in range U+0800 - U+FFFF.
1094-
$byte_3 = ord(
1095-
$this->sql[ $this->bytes_already_read + 2 ] ?? "\0"
1096-
);
1097-
if (
1098-
$byte_1 <= 0xEF
1099-
&& $byte_2 >= 0x80 && $byte_2 <= 0xBF
1100-
&& $byte_3 >= 0x80 && $byte_3 <= 0xBF
1101-
// Exclude surrogate range U+D800 to U+DFFF:
1102-
&& ! ( 0xED === $byte_1 && $byte_2 >= 0xA0 )
1103-
// Exclude overlong encodings:
1104-
&& ! ( 0xE0 === $byte_1 && $byte_2 < 0xA0 )
1105-
) {
1106-
$this->bytes_already_read += 3;
1107-
continue;
1108-
}
1093+
// Look for a valid 2-byte UTF-8 symbol. Covers range U+0080 - U+07FF.
1094+
$byte_2 = ord(
1095+
$this->sql[ $this->bytes_already_read + 1 ] ?? "\0"
1096+
);
1097+
if (
1098+
$byte_1 <= 0xDF
1099+
&& $byte_2 >= 0x80 && $byte_2 <= 0xBF
1100+
) {
1101+
$this->bytes_already_read += 2;
1102+
continue;
1103+
}
11091104

1110-
// Look for a valid 4-byte UTF-8 symbol in range U+10000 - U+10FFFF.
1111-
$byte_4 = ord(
1112-
$this->sql[ $this->bytes_already_read + 3 ] ?? "\0"
1113-
);
1114-
if (
1115-
$byte_1 >= 0xF0
1116-
&& $byte_2 >= 0x80 && $byte_2 <= 0xBF
1117-
&& $byte_3 >= 0x80 && $byte_3 <= 0xBF
1118-
&& $byte_4 >= 0x80 && $byte_4 <= 0xBF
1119-
// Exclude overlong encodings:
1120-
&& ! ( 0xF0 === $byte_1 && $byte_2 < 0x90 )
1121-
// Exclude codepoints above U+10FFFF:
1122-
&& ! ( 0xF4 === $byte_1 && $byte_2 > 0x8F )
1123-
) {
1124-
$this->bytes_already_read += 4;
1125-
continue;
1126-
}
1105+
// Look for a valid 3-byte UTF-8 symbol in range U+0800 - U+FFFF.
1106+
$byte_3 = ord(
1107+
$this->sql[ $this->bytes_already_read + 2 ] ?? "\0"
1108+
);
1109+
if (
1110+
$byte_1 <= 0xEF
1111+
&& $byte_2 >= 0x80 && $byte_2 <= 0xBF
1112+
&& $byte_3 >= 0x80 && $byte_3 <= 0xBF
1113+
// Exclude surrogate range U+D800 to U+DFFF:
1114+
&& ! ( 0xED === $byte_1 && $byte_2 >= 0xA0 )
1115+
// Exclude overlong encodings:
1116+
&& ! ( 0xE0 === $byte_1 && $byte_2 < 0xA0 )
1117+
) {
1118+
$this->bytes_already_read += 3;
1119+
continue;
1120+
}
11271121

1128-
// Not a valid identifier character.
1129-
break;
1122+
// Look for a valid 4-byte UTF-8 symbol in range U+10000 - U+10FFFF.
1123+
$byte_4 = ord(
1124+
$this->sql[ $this->bytes_already_read + 3 ] ?? "\0"
1125+
);
1126+
if (
1127+
$byte_1 >= 0xF0
1128+
&& $byte_2 >= 0x80 && $byte_2 <= 0xBF
1129+
&& $byte_3 >= 0x80 && $byte_3 <= 0xBF
1130+
&& $byte_4 >= 0x80 && $byte_4 <= 0xBF
1131+
// Exclude overlong encodings:
1132+
&& ! ( 0xF0 === $byte_1 && $byte_2 < 0x90 )
1133+
// Exclude codepoints above U+10FFFF:
1134+
&& ! ( 0xF4 === $byte_1 && $byte_2 > 0x8F )
1135+
) {
1136+
$this->bytes_already_read += 4;
1137+
continue;
1138+
}
1139+
1140+
// Not a valid identifier character.
1141+
break;
1142+
}
11301143
}
11311144

11321145
if ( $this->bytes_already_read === $started_at ) {

0 commit comments

Comments
 (0)