Optimize SJIS-Mobile#DOCOMO decoder for speed

From my microbenchmarks, the new decoder makes encoding conversion
from SJIS-Mobile#DOCOMO about 15-20% faster.
This commit is contained in:
Alex Dowad 2022-12-03 14:06:36 +02:00
parent 4ebfddfad4
commit 43cdfa3190

View File

@ -409,11 +409,11 @@ int mbfl_filt_conv_wchar_sjis(int c, mbfl_convert_filter *filter)
return 0;
}
static unsigned short sjis_decode_tbl1[] = {
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
static const unsigned short sjis_decode_tbl1[] = {
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, 8836, 9024, 9212, 9400, 9588, 9776, 9964, 10152, 10340, 10528, 10716, 10904, 11092
};
static unsigned short sjis_decode_tbl2[] = {
static const unsigned short sjis_decode_tbl2[] = {
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
};
@ -2117,59 +2117,55 @@ static size_t mb_sjis_docomo_to_wchar(unsigned char **in, size_t *in_len, uint32
break;
}
unsigned char c2 = *p++;
uint32_t w = sjis_decode_tbl1[c] + sjis_decode_tbl2[c2];
if (c2 >= 0x40 && c2 <= 0xFC && c2 != 0x7F) {
uint32_t w = 0;
unsigned int s1, s2;
SJIS_DECODE(c, c2, s1, s2);
unsigned int s = ((s1 - 0x21) * 94) + s2 - 0x21;
if (s <= 137) {
if (s == 31) {
w = 0xFF3C; /* FULLWIDTH REVERSE SOLIDUS */
} else if (s == 32) {
w = 0xFF5E; /* FULLWIDTH TILDE */
} else if (s == 33) {
w = 0x2225; /* PARALLEL TO */
} else if (s == 60) {
w = 0xFF0D; /* FULLWIDTH HYPHEN-MINUS */
} else if (s == 80) {
w = 0xFFE0; /* FULLWIDTH CENT SIGN */
} else if (s == 81) {
w = 0xFFE1; /* FULLWIDTH POUND SIGN */
} else if (s == 137) {
w = 0xFFE2; /* FULLWIDTH NOT SIGN */
}
if (w <= 137) {
if (w == 31) {
*out++ = 0xFF3C; /* FULLWIDTH REVERSE SOLIDUS */
continue;
} else if (w == 32) {
*out++ = 0xFF5E; /* FULLWIDTH TILDE */
continue;
} else if (w == 33) {
*out++ = 0x2225; /* PARALLEL TO */
continue;
} else if (w == 60) {
*out++ = 0xFF0D; /* FULLWIDTH HYPHEN-MINUS */
continue;
} else if (w == 80) {
*out++ = 0xFFE0; /* FULLWIDTH CENT SIGN */
continue;
} else if (w == 81) {
*out++ = 0xFFE1; /* FULLWIDTH POUND SIGN */
continue;
} else if (w == 137) {
*out++ = 0xFFE2; /* FULLWIDTH NOT SIGN */
continue;
}
}
if (!w) {
if (s >= mb_tbl_code2uni_docomo1_min && s <= mb_tbl_code2uni_docomo1_max) {
int snd = 0;
w = mbfilter_sjis_emoji_docomo2unicode(s, &snd);
if (snd) {
*out++ = snd;
}
} else if (s >= cp932ext1_ucs_table_min && s < cp932ext1_ucs_table_max) {
w = cp932ext1_ucs_table[s - cp932ext1_ucs_table_min];
} else if (s < jisx0208_ucs_table_size) {
w = jisx0208_ucs_table[s];
} else if (s >= cp932ext2_ucs_table_min && s < cp932ext2_ucs_table_max) {
w = cp932ext2_ucs_table[s - cp932ext2_ucs_table_min];
}
if (!w) {
if (s >= cp932ext3_ucs_table_min && s < cp932ext3_ucs_table_max) {
w = cp932ext3_ucs_table[s - cp932ext3_ucs_table_min];
} else if (s >= (94*94) && s < (114*94)) {
w = s - (94*94) + 0xE000;
}
}
if (w >= mb_tbl_code2uni_docomo1_min && w <= mb_tbl_code2uni_docomo1_max) {
int snd = 0;
w = mbfilter_sjis_emoji_docomo2unicode(w, &snd);
if (snd) {
*out++ = snd;
}
*out++ = w ? w : MBFL_BAD_INPUT;
} else if (w >= cp932ext1_ucs_table_min && w < cp932ext1_ucs_table_max) {
w = cp932ext1_ucs_table[w - cp932ext1_ucs_table_min];
} else if (w < jisx0208_ucs_table_size) {
w = jisx0208_ucs_table[w];
} else if (w >= cp932ext2_ucs_table_min && w < cp932ext2_ucs_table_max) {
w = cp932ext2_ucs_table[w - cp932ext2_ucs_table_min];
} else if (w >= cp932ext3_ucs_table_min && w < cp932ext3_ucs_table_max) {
w = cp932ext3_ucs_table[w - cp932ext3_ucs_table_min];
} else if (w >= (94*94) && w < (114*94)) {
w = w - (94*94) + 0xE000;
} else {
*out++ = MBFL_BAD_INPUT;
continue;
}
*out++ = w ? w : MBFL_BAD_INPUT;
} else {
*out++ = MBFL_BAD_INPUT;
}