#Optimization of u128 to base62 encoding

2 messages · Page 1 of 1 (latest)

pine mist
opaque blaze
#

Nice one! I tried a simple thing to make the loop unroll, but the performance really is all in the div instruction! Good finding. I managed to improve on your code just a tiny bit, getting maybe 5-10% more throughput by replacing your single loop encode into a dual encode step. https://godbolt.org/z/GfeTbYjYP

i -= 2;
let nlow2 = nlow % BASE62_N2;
let nlowhi = nlow2 / (BASE62_N as u64);
let nlowlo = nlow2 % (BASE62_N as u64);
b62_str[i] = BASE62_DIGITS[nlowhi as usize];
b62_str[i + 1] = BASE62_DIGITS[nlowlo as usize];
nlow /= BASE62_N2;

The improvement here comes from removing some dependency chains in the registers which allows the CPU to pipeline the operations more aggresively, even if it's a little bit more work overall