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

Remove format! allocations during parsing #47

Closed
wants to merge 1 commit into from
Closed

Remove format! allocations during parsing #47

wants to merge 1 commit into from

Conversation

nickelc
Copy link
Contributor

@nickelc nickelc commented Jan 27, 2023

The boundary is wrapped in a newtype with access methods for the different needs.

Fixes #36

Comment on lines +96 to +101
let cap = constants::CRLF.len() + constants::BOUNDARY_EXT.len() + boundary.len();
let mut buf = String::with_capacity(cap);
buf.push_str(constants::CRLF);
buf.push_str(constants::BOUNDARY_EXT);
buf.push_str(&boundary);
Self(buf)
Copy link
Member

@SergioBenitez SergioBenitez Jan 31, 2023

Choose a reason for hiding this comment

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

Though this might be better than using format!() directly, the hope would be to remove all unnecessary allocations along the parsing path. Instead of using format!() to create a string to compare, perform the comparison directly. That is, instead of:

"ab" == format!("{}{}", "a", "b");

Do something closer to:

"ab"[..1] == "a" && "ab"[1..] == "b"

But safely, of course, considering all bounds and possible string lengths.

We could imagine a generalized function:

fn string_is_parts(string: &str, parts: &[&str]) -> bool;

string_is_parts("ab", &["a", "b"]);
string_is_parts(maybe_boundary, &[constants::CRLF, constants::BOUNDARY_EXT, boundary]);

@SergioBenitez
Copy link
Member

Closing due to inactivity.

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.

Remove format! allocations during parsing
2 participants