Skip to content

faster byte_serialized_unchanged #1014

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
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
20 changes: 19 additions & 1 deletion form_urlencoded/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,26 @@
bytes: &'a [u8],
}

/// This is a precomputed table of which chars match and which don't.
const fn glyphless_mask() -> u128 {
let mut magic = 0_u128;
let mut c = 0;
while c < 128 {
magic |= (matches!(c, b'*' | b'-' | b'.' | b'0' ..= b'9' | b'A' ..= b'Z' | b'_' | b'a' ..= b'z')

Check warning on line 136 in form_urlencoded/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

form_urlencoded/src/lib.rs#L132-L136

Added lines #L132 - L136 were not covered by tests
as u128)
<< c;
c += 1;

Check warning on line 139 in form_urlencoded/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

form_urlencoded/src/lib.rs#L138-L139

Added lines #L138 - L139 were not covered by tests
}
magic

Check warning on line 141 in form_urlencoded/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

form_urlencoded/src/lib.rs#L141

Added line #L141 was not covered by tests
}
const GLYPHLESS_MASK: u128 = glyphless_mask();

#[inline]
fn byte_serialized_unchanged(byte: u8) -> bool {
matches!(byte, b'*' | b'-' | b'.' | b'0' ..= b'9' | b'A' ..= b'Z' | b'_' | b'a' ..= b'z')
if byte > b'z' {
return false;
}
((GLYPHLESS_MASK >> byte) & 1) == 1
}

impl<'a> Iterator for ByteSerialize<'a> {
Expand Down
Loading