Commit 6750bb6
perf(iconv): DBCS encode direct tables — Big5 1.7x, all 7 encoders O(1)
The 2-byte DBCS encoders (Shift-JIS, Big5, GBK, EUC-KR, CP949, GB2312, Johab) all
binary-searched their sorted (cp, packed) table per char (thousands of entries:
Big5 14030, EUC-KR/etc), running ~3x SLOWER than glibc on UTF-8 -> DBCS. Same
direct-table lever as the decode side.
LEVER: encode_dbcs2 now takes a direct `code point -> packed` table
(`[u32; 0x10000]`, storing `packed + 1` so 0 = unrepresentable — packed 0 is a
valid single output byte and cannot be the sentinel), one O(1) index instead of
the binary search. Each codec builds its table lazily once (OnceLock) via
build_enc_direct from the same sorted array (byte-for-byte identical); astral code
points are not in any 2-byte DBCS so they stay Unrepresentable, matching the
binary search missing them. A small dbcs_encoder! macro generates the 5 codec
wrappers (GBK / EUC-KR / CP949 / GB2312 / Johab); Shift-JIS / Big5 keep theirs.
Before -> after (release, UTF-8 -> DBCS, vs glibc):
Big5: 5.70 -> 3.30 ns/byte (1.73x faster; 3.69x -> 2.87x vs glibc)
Shift-JIS: 5.52 -> 4.65 ns/byte (1.19x; smaller table = less binary-search cost)
EUC-JP encode is already ~parity with glibc (its own encoder, untouched).
Isomorphism: iconv_differential_fuzz (covers Shift-JIS/Big5 vs live glibc) +
conformance_diff_iconv + iconv_abi_test (49) all green. The direct table yields the
same packed value as the binary search for every code point.
Residual (the ~2.9-3x still vs glibc): the scalar UTF-8 source decode + per-char
encode call — a SIMD-decode-feeds-DBCS-encode lever is the next step.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>1 parent 229157c commit 6750bb6
1 file changed
Lines changed: 49 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8518 | 8518 | | |
8519 | 8519 | | |
8520 | 8520 | | |
8521 | | - | |
| 8521 | + | |
| 8522 | + | |
| 8523 | + | |
| 8524 | + | |
| 8525 | + | |
| 8526 | + | |
| 8527 | + | |
| 8528 | + | |
| 8529 | + | |
| 8530 | + | |
| 8531 | + | |
| 8532 | + | |
| 8533 | + | |
| 8534 | + | |
| 8535 | + | |
| 8536 | + | |
| 8537 | + | |
| 8538 | + | |
8522 | 8539 | | |
8523 | | - | |
8524 | | - | |
8525 | | - | |
| 8540 | + | |
| 8541 | + | |
| 8542 | + | |
8526 | 8543 | | |
8527 | 8544 | | |
8528 | 8545 | | |
| |||
8538 | 8555 | | |
8539 | 8556 | | |
8540 | 8557 | | |
8541 | | - | |
| 8558 | + | |
| 8559 | + | |
8542 | 8560 | | |
8543 | 8561 | | |
8544 | 8562 | | |
| |||
8554 | 8572 | | |
8555 | 8573 | | |
8556 | 8574 | | |
8557 | | - | |
| 8575 | + | |
| 8576 | + | |
| 8577 | + | |
8558 | 8578 | | |
8559 | 8579 | | |
8560 | 8580 | | |
| |||
8569 | 8589 | | |
8570 | 8590 | | |
8571 | 8591 | | |
8572 | | - | |
| 8592 | + | |
| 8593 | + | |
| 8594 | + | |
8573 | 8595 | | |
8574 | 8596 | | |
8575 | 8597 | | |
| |||
9317 | 9339 | | |
9318 | 9340 | | |
9319 | 9341 | | |
9320 | | - | |
9321 | | - | |
9322 | | - | |
9323 | | - | |
| 9342 | + | |
| 9343 | + | |
| 9344 | + | |
| 9345 | + | |
9324 | 9346 | | |
9325 | | - | |
| 9347 | + | |
9326 | 9348 | | |
9327 | 9349 | | |
9328 | 9350 | | |
| 9351 | + | |
| 9352 | + | |
| 9353 | + | |
| 9354 | + | |
| 9355 | + | |
| 9356 | + | |
| 9357 | + | |
| 9358 | + | |
| 9359 | + | |
| 9360 | + | |
| 9361 | + | |
| 9362 | + | |
| 9363 | + | |
| 9364 | + | |
| 9365 | + | |
9329 | 9366 | | |
9330 | 9367 | | |
9331 | 9368 | | |
| |||
0 commit comments