Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increased Decode Performance #102

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Select decode intermediates by input length
malvidin committed Nov 28, 2022
commit cfbb599034db7c100cedbee083facc769d478c55
133 changes: 106 additions & 27 deletions src/decode.rs
Original file line number Diff line number Diff line change
@@ -287,44 +287,123 @@ impl<'a, I: AsRef<[u8]>> DecodeBuilder<'a, I> {
}
}


fn alpha_decode(index: usize, input_char: u8, alpha: &Alphabet) -> Result<u8> {
if input_char > 127 {
return Err(Error::NonAsciiCharacter { index });
};
let val = alpha.decode[input_char as usize];
if val >= 58 {
return Err(Error::InvalidCharacter {
character: input_char as char,
index,
});
}
return Ok(val);
}

fn decode_into(input: &[u8], output: &mut [u8], alpha: &Alphabet) -> Result<usize> {
let mut index = 0;
let zero = alpha.encode[0];

for (i, c) in input.iter().enumerate() {
if *c > 127 {
return Err(Error::NonAsciiCharacter { index: i });
}
for (_, _) in input.iter().enumerate().take_while(|(_, c)| **c == zero) {
let byte = output.get_mut(index).ok_or(Error::BufferTooSmall)?;
*byte = 0;
index += 1;
}

let mut val = alpha.decode[*c as usize] as usize;
if val == 0xFF {
return Err(Error::InvalidCharacter {
character: *c as char,
index: i,
});
}
let index_0 = index;
let input_len = input.len() - index_0;

for byte in &mut output[..index] {
val += (*byte as usize) * 58;
*byte = (val & 0xFF) as u8;
val >>= 8;
if input_len > 0 && input_len <= 10 {
let mut output_uint = 0u64;
for (i, c) in input.iter().enumerate().skip(index_0) {
let val = alpha_decode(i, *c, alpha)? as u64;
output_uint = 58 * output_uint + val;
}

while val > 0 {
while output_uint > 0 {
let byte = output.get_mut(index).ok_or(Error::BufferTooSmall)?;
*byte = (val & 0xFF) as u8;
*byte = output_uint as u8;
index += 1;
val >>= 8
output_uint >>= 8
}
output[index_0..index].reverse();
} else if input_len <= 21 {
let mut output_uint = 0u128;
for (i, c) in input.iter().enumerate().skip(index_0) {
let val = alpha_decode(i, *c, alpha)? as u128;
output_uint = 58 * output_uint + val;
}
while output_uint > 0 {
let byte = output.get_mut(index).ok_or(Error::BufferTooSmall)?;
*byte = output_uint as u8;
index += 1;
output_uint >>= 8
}
output[index_0..index].reverse();
} else if input_len <= 43 {
let mut output_uints = [0u64; 4];
let mut ll_index = 0;
for (i, c) in input.iter().enumerate().skip(index_0) {
let mut val = alpha_decode(i, *c, alpha)? as u128;

for ll in &mut output_uints[..ll_index] {
val += *ll as u128 * 58;
*ll = val as u64;
val >>= 64;
}
while val > 0 {
let ll = output_uints.get_mut(ll_index).expect("Base58 input under 43 chars fit into [u64;4]");
*ll = val as u64;
ll_index += 1;
val >>= 64
}
}
output_uints.reverse();
let mut leading_0 = true;
for ll in output_uints {
for be_byte in ll.to_be_bytes() {
if leading_0 && be_byte == 0 {
continue;
} else {
leading_0 = false;
}
let byte = output.get_mut(index).ok_or(Error::BufferTooSmall)?;
*byte = be_byte;
index += 1;
}
}
} else {
let mut output_uints: Vec<u64> = Vec::with_capacity(1 + (7_323 * input_len) / 80_000 ); // [0u64; 4];
let mut ll_index = 0;
for (i, c) in input.iter().enumerate().skip(index_0) {
let mut val = alpha_decode(i, *c, alpha)? as u128;
for ll in &mut output_uints[..ll_index] {
val += *ll as u128 * 58;
*ll = val as u64;
val >>= 64;
}
while val > 0 {
ll_index += 1;
output_uints.push(val as u64);
val >>= 64
}
}
output_uints.reverse();
let mut leading_0 = true;
for ll in output_uints {
for be_byte in ll.to_be_bytes() {
if leading_0 && be_byte == 0 {
continue;
} else {
leading_0 = false;
}
let byte = output.get_mut(index).ok_or(Error::BufferTooSmall)?;
*byte = be_byte;
index += 1;
}
}
}

for _ in input.iter().take_while(|c| **c == zero) {
let byte = output.get_mut(index).ok_or(Error::BufferTooSmall)?;
*byte = 0;
index += 1;
}

output[..index].reverse();
Ok(index)
}