Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
valid_utf8_to_uvchr() unroll loop; avoid array lookup
This partially unrolls the loop in this function to achieve a slight speed up for UTF8 characters containing multiple bytes. The loop is retained for non-Unicode code points, as those should rarely be seen. The commit also avoids the array lookup in the UTF8SKIP macro; the array may not be in the cache. Since we know the start byte is well-formed, we can use a few shift/mask instructions along with the clz instruction to calculate this value. If the platform doesn't have a fast clz method, the array lookup is cheaper and is retained. Benchmarks are: Key: Ir Instruction read Dr Data read Dw Data write COND conditional branches IND indirect branches _m branch predict miss The numbers represent raw counts per loop iteration. code point 127; single byte UTF-8: blead new Ratio % ------ ------ ------- Ir 1850.0 1866.0 99.1 Dr 613.0 614.0 99.8 Dw 428.0 429.0 99.8 COND 194.0 194.0 100.0 IND 22.0 23.0 95.7 COND_m 3.0 2.0 150.0 IND_m 9.0 9.0 100.0 code point 2047; 2 byte UTF-8: blead new Ratio % ------ ------ ------- Ir 1865.0 1870.0 99.7 Dr 614.0 615.0 99.8 Dw 428.0 429.0 99.8 COND 196.0 194.0 101.0 IND 22.0 23.0 95.7 COND_m 3.0 2.0 150.0 IND_m 9.0 9.0 100.0 code point 65535; 3 byte UTF-8: blead new Ratio % ------ ------ ------- Ir 1872.0 1876.0 99.8 Dr 615.0 616.0 99.8 Dw 428.0 429.0 99.8 COND 197.0 194.0 101.5 IND 22.0 23.0 95.7 COND_m 3.0 2.0 150.0 IND_m 9.0 9.0 100.0 code point 2097151; 4 byte UTF-8: blead new Ratio % ------ ------ ------- Ir 1879.0 1880.0 99.9 Dr 616.0 617.0 99.8 Dw 428.0 429.0 99.8 COND 198.0 194.0 102.1 IND 22.0 23.0 95.7 COND_m 3.0 2.0 150.0 IND_m 9.0 9.0 100.0 code point 67108863; 5 byte UTF-8: blead new Ratio % ------ ------ ------- Ir 1886.0 1897.0 99.4 Dr 617.0 620.0 99.5 Dw 428.0 429.0 99.8 COND 199.0 195.0 102.1 IND 22.0 23.0 95.7 COND_m 3.0 2.0 150.0 IND_m 9.0 9.0 100.0 code point 134217727; 6 byte UTF-8: blead new Ratio % ------ ------ ------- Ir 1893.0 1904.0 99.4 Dr 618.0 621.0 99.5 Dw 428.0 429.0 99.8 COND 200.0 196.0 102.0 IND 22.0 23.0 95.7 COND_m 3.0 2.0 150.0 IND_m 9.0 9.0 100.0
- Loading branch information