Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

lib/base64: optimize base64_decode() with reverse lookup tables

Replace the use of strchr() in base64_decode() with precomputed reverse
lookup tables for each variant. This avoids repeated string scans and
improves performance. Use -1 in the tables to mark invalid characters.

Decode:
64B ~1530ns -> ~80ns (~19.1x)
1KB ~27726ns -> ~1239ns (~22.4x)

[akpm@linux-foundation.org: fix kernedoc]
Link: https://lkml.kernel.org/r/20251114060107.89026-1-409411716@gms.tku.edu.tw
Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
Co-developed-by: Guan-Chun Wu <409411716@gms.tku.edu.tw>
Signed-off-by: Guan-Chun Wu <409411716@gms.tku.edu.tw>
Reviewed-by: David Laight <david.laight.linux@gmail.com>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Eric Biggers <ebiggers@kernel.org>
Cc: Ilya Dryomov <idryomov@gmail.com>
Cc: Jaegeuk Kim <jaegeuk@kernel.org>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Keith Busch <kbusch@kernel.org>
Cc: Sagi Grimberg <sagi@grimberg.me>
Cc: "Theodore Y. Ts'o" <tytso@mit.edu>
Cc: Viacheslav Dubeyko <Slava.Dubeyko@ibm.com>
Cc: Xiubo Li <xiubli@redhat.com>
Cc: Yu-Sheng Huang <home7438072@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Kuan-Wei Chiu and committed by
Andrew Morton
c4eb7ad3 f1e2ca80

+48 -4
+48 -4
lib/base64.c
··· 21 21 [BASE64_IMAP] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,", 22 22 }; 23 23 24 + /* 25 + * Initialize the base64 reverse mapping for a single character 26 + * This macro maps a character to its corresponding base64 value, 27 + * returning -1 if the character is invalid. 28 + * char 'A'-'Z' maps to 0-25, 'a'-'z' maps to 26-51, '0'-'9' maps to 52-61, 29 + * ch_62 maps to 62, ch_63 maps to 63, and other characters return -1 30 + */ 31 + #define INIT_1(v, ch_62, ch_63) \ 32 + [v] = (v) >= 'A' && (v) <= 'Z' ? (v) - 'A' \ 33 + : (v) >= 'a' && (v) <= 'z' ? (v) - 'a' + 26 \ 34 + : (v) >= '0' && (v) <= '9' ? (v) - '0' + 52 \ 35 + : (v) == (ch_62) ? 62 : (v) == (ch_63) ? 63 : -1 36 + 37 + /* 38 + * Recursive macros to generate multiple Base64 reverse mapping table entries. 39 + * Each macro generates a sequence of entries in the lookup table: 40 + * INIT_2 generates 2 entries, INIT_4 generates 4, INIT_8 generates 8, and so on up to INIT_32. 41 + */ 42 + #define INIT_2(v, ...) INIT_1(v, __VA_ARGS__), INIT_1((v) + 1, __VA_ARGS__) 43 + #define INIT_4(v, ...) INIT_2(v, __VA_ARGS__), INIT_2((v) + 2, __VA_ARGS__) 44 + #define INIT_8(v, ...) INIT_4(v, __VA_ARGS__), INIT_4((v) + 4, __VA_ARGS__) 45 + #define INIT_16(v, ...) INIT_8(v, __VA_ARGS__), INIT_8((v) + 8, __VA_ARGS__) 46 + #define INIT_32(v, ...) INIT_16(v, __VA_ARGS__), INIT_16((v) + 16, __VA_ARGS__) 47 + 48 + #define BASE64_REV_INIT(ch_62, ch_63) { \ 49 + [0 ... 0x1f] = -1, \ 50 + INIT_32(0x20, ch_62, ch_63), \ 51 + INIT_32(0x40, ch_62, ch_63), \ 52 + INIT_32(0x60, ch_62, ch_63), \ 53 + [0x80 ... 0xff] = -1 } 54 + 55 + static const s8 base64_rev_maps[][256] = { 56 + [BASE64_STD] = BASE64_REV_INIT('+', '/'), 57 + [BASE64_URLSAFE] = BASE64_REV_INIT('-', '_'), 58 + [BASE64_IMAP] = BASE64_REV_INIT('+', ',') 59 + }; 60 + 61 + #undef BASE64_REV_INIT 62 + #undef INIT_32 63 + #undef INIT_16 64 + #undef INIT_8 65 + #undef INIT_4 66 + #undef INIT_2 67 + #undef INIT_1 24 68 /** 25 69 * base64_encode() - Base64-encode some binary data 26 70 * @src: the binary data to encode ··· 128 84 int bits = 0; 129 85 int i; 130 86 u8 *bp = dst; 131 - const char *base64_table = base64_tables[variant]; 87 + s8 ch; 132 88 133 89 for (i = 0; i < srclen; i++) { 134 - const char *p = strchr(base64_table, src[i]); 135 90 if (padding) { 136 91 if (src[i] == '=') { 137 92 ac = (ac << 6); ··· 140 97 continue; 141 98 } 142 99 } 143 - if (p == NULL || src[i] == 0) 100 + ch = base64_rev_maps[variant][(u8)src[i]]; 101 + if (ch == -1) 144 102 return -1; 145 - ac = (ac << 6) | (p - base64_table); 103 + ac = (ac << 6) | ch; 146 104 bits += 6; 147 105 if (bits >= 8) { 148 106 bits -= 8;