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

Use underlying std::net::Ipv4Addr #26

Merged
merged 2 commits into from
Feb 7, 2023
Merged

Conversation

ckcr4lyf
Copy link
Owner

@ckcr4lyf ckcr4lyf commented Feb 7, 2023

Removes "parsing" entirely, so getting the [u8; 6] of IP & port is much faster

benchmark:

IP str to bytes/&str    time:   [32.792 ns 32.899 ns 33.033 ns]
IP str to bytes/std::net::Ipv4Addr
                        time:   [2.0008 ns 2.0097 ns 2.0194 ns]

Comment on lines -73 to 77
pub fn ip_str_port_u16_to_bytes(ip_str: &str, port: u16) -> [u8; 6] {
// Convert the ipv4 addr, port combo to a [u8; 6]
pub fn ip_str_port_u16_to_bytes(ip_addr: &std::net::Ipv4Addr, port: u16) -> [u8; 6] {
let mut result: [u8; 6] = [0; 6];
let mut parts = ip_str.split('.');
let ip_octets = ip_addr.octets();

for i in 0..4 {
result[i] = match parts.next() {
Some(v) => match v.parse::<u8>() {
Ok(v) => v,
Err(_) => 0,
}
None => 0,
}
}
result[i] = ip_octets[i];
}

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets fucking go

@ckcr4lyf ckcr4lyf merged commit d7722eb into master Feb 7, 2023
@ckcr4lyf ckcr4lyf deleted the feature/parse-ip-fast branch September 4, 2024 03:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant