Skip to content

Store HumanReadableNames in-object rather than on the heap #3733

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 1 commit into
base: main
Choose a base branch
from

Conversation

TheBlueMatt
Copy link
Collaborator

Since the full encoded domain name of an HRN cannot exceed the maximum length of a DNS name (255 octets), there's not a lot of reason to store the user and domain parts of an HRN on the heap via two Strings.

Instead, here, we store one byte array with the maximum size of both labels as well as the length of the user and domain parts.

Because we're now avoiding heap allocations this also implies making HumanReadableName::new take the user and domain parts by reference as &strs, rather than by value as Strings.

@ldk-reviews-bot
Copy link

ldk-reviews-bot commented Apr 12, 2025

I've assigned @valentinewallace as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

Since the full encoded domain name of an HRN cannot exceed the
maximum length of a DNS name (255 octets), there's not a lot of
reason to store the `user` and `domain` parts of an HRN on the heap
via two `String`s.

Instead, here, we store one byte array with the maximum size of
both labels as well as the length of the `user` and `domain` parts.

Because we're now avoiding heap allocations this also implies
making `HumanReadableName::new` take the `user` and `domain` parts
by reference as `&str`s, rather than by value as `String`s.
@ldk-reviews-bot
Copy link

🔔 1st Reminder

Hey @valentinewallace! This PR has been waiting for your review.
Please take a look when you have a chance. If you're unable to review, please let us know so we can find another reviewer.

Copy link
Contributor

@valentinewallace valentinewallace left a comment

Choose a reason for hiding this comment

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

Basically LGTM


/// Check if the chars in `s` are allowed to be included in a hostname.
pub(crate) fn str_chars_allowed(s: &str) -> bool {
s.chars().all(|c| c.is_ascii_alphanumeric() || c == '.' || c == '_' || c == '-')
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we call this method from Hostname::str_is_valid_hostname or vice versa?

Ok(user) => user,
Err(_) => return Err(DecodeError::InvalidValue),
};

let mut domain_bytes = [0; 255];
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason to not use the 231 value mentioned above?

@ldk-reviews-bot
Copy link

👋 The first review has been submitted!

Do you think this PR is ready for a second reviewer? If so, click here to assign a second reviewer.

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.

3 participants