Skip to content

Commit b3d197d

Browse files
committed
Optimize SJIS decoder for speed
While benchmarking the new implementation of mb_substr, I found it was slower than the old one only when the selected encoding was SJIS. Investigation showed that the new text conversion filter for SJIS was a touch slower than the old one. With this optimization, the new SJIS decoder is about 20% faster than the old one.
1 parent 0c0774f commit b3d197d

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

ext/mbstring/libmbfl/filters/mbfilter_sjis.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,14 @@ int mbfl_filt_conv_wchar_sjis(int c, mbfl_convert_filter *filter)
250250
return 0;
251251
}
252252

253+
static unsigned short sjis_decode_tbl1[] = {
254+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 188, 376, 564, 752, 940, 1128, 1316, 1504, 1692, 1880, 2068, 2256, 2444, 2632, 2820, 3008, 3196, 3384, 3572, 3760, 3948, 4136, 4324, 4512, 4700, 4888, 5076, 5264, 5452, 5640, -6204, -6016, -5828, -5640, -5452, -5264, -5076, -4888, -4700, -4512, -4324, -4136, -3948, -3760, -3572, -3384, -3196, -3008, -2820, -2632, -2444, -2256, -2068, -1880, -1692, -1504, -1316, -1128, -940, -752, -564, -376, -188, 0, 188, 376, 564, 752, 940, 1128, 1316, 1504, 1692, 1880, 2068, 2256, 2444, 2632, 2820, 3008, 3196, 3384, 3572, 3760, 3948, 4136, 4324, 4512, 4700, 4888, 5076, 5264, 5452, 5640, 5828, 6016, 6204, 6392, 6580, 6768, 6956, 7144, 7332, 7520, 7708, 7896, 8084, 8272, 8460, 8648
255+
};
256+
257+
static unsigned short sjis_decode_tbl2[] = {
258+
0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 0xFFFF, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 0xFFFF, 0xFFFF, 0xFFFF
259+
};
260+
253261
static size_t mb_sjis_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state)
254262
{
255263
unsigned char *p = *in, *e = p + *in_len;
@@ -264,18 +272,16 @@ static size_t mb_sjis_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf
264272
*out++ = 0xFEC0 + c;
265273
} else if (c > 0x80 && c <= 0xEF && c != 0xA0 && p < e) {
266274
unsigned char c2 = *p++;
267-
if (c2 >= 0x40 && c2 <= 0xFC && c2 != 0x7F) {
268-
unsigned int s1, s2;
269-
SJIS_DECODE(c, c2, s1, s2);
270-
uint32_t w = (s1 - 0x21)*94 + s2 - 0x21;
271-
if (w < jisx0208_ucs_table_size) {
272-
w = jisx0208_ucs_table[w];
273-
if (!w)
274-
w = MBFL_BAD_INPUT;
275-
*out++ = w;
276-
} else {
277-
*out++ = MBFL_BAD_INPUT;
278-
}
275+
/* This is only legal if c2 >= 0x40 && c2 <= 0xFC && c2 != 0x7F
276+
* But the values in the above conversion tables have been chosen such that
277+
* illegal values of c2 will always result in w > jisx0208_ucs_table_size,
278+
* so we don't need to do a separate bounds check on c2 */
279+
uint32_t w = sjis_decode_tbl1[c] + sjis_decode_tbl2[c2];
280+
if (w < jisx0208_ucs_table_size) {
281+
w = jisx0208_ucs_table[w];
282+
if (!w)
283+
w = MBFL_BAD_INPUT;
284+
*out++ = w;
279285
} else {
280286
*out++ = MBFL_BAD_INPUT;
281287
}

0 commit comments

Comments
 (0)