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 itertools #1294

Merged
merged 3 commits into from
Feb 16, 2024
Merged

remove itertools #1294

merged 3 commits into from
Feb 16, 2024

Conversation

benmkw
Copy link
Contributor

@benmkw benmkw commented Feb 16, 2024

follow up from #1293 (comment) and #1293 (comment)

Removes around 9s of cpu compilation time at the exchange of some allocations for displaying mails (which should not be critical) and a for loop instead of a fold.

.iter()
// BStr does not impl slice::Join
.map(|s| s.to_string())
.collect::<Vec<_>>()
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you could write an iterator for BStr to avoid this allocation (and the .to_string())?

Given that gix uses BStr a lot, I think it might worth the trouble.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was not yet sure what the precise missing bound was as to why BStr does not impl Join. Given that the results are probably short and are directly written to the user it seemed fine to convert them to Strings but that might be wrong (?).

Copy link
Contributor

Choose a reason for hiding this comment

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

I was not yet sure what the precise missing bound was as to why BStr does not impl Join.

std::slice::join only accept types that impl Borrow<str>, BStr doesn't implement it.

Given that the results are probably short and are directly written to the user it seemed fine to convert them to Strings but that might be wrong (?).

True, though I still think achieving zero-allocation is better.

It probably won't take that much effect given that BStr implements std::fmt::Display, so basically you just need to re-implement join from itertool.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I inlined the join impl which was fairly compact.

@@ -43,6 +42,29 @@ impl<'a> From<&'a WorkByEmail> for WorkByPerson {
}
}

fn join<I>(mut iter: I, sep: &str) -> String
Copy link
Contributor

Choose a reason for hiding this comment

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

IMO you can avoid the String creation by turning this into a struct, with iter and sep stored in it.

Then impl fmt::Display on it, and you will achieve zero-allocation joining.

Copy link
Member

Choose a reason for hiding this comment

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

I think it's fine to allocate here, it's similar to what was available before with std::slice::join().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I took it from here https://github.com/rust-itertools/itertools/blob/762643f1be2217140a972745cf4d6ed69435f722/src/lib.rs#L2295-L2324

I also think its fine as is but one could certainly think of ways to improve it. The function could also be made non generic and just accept a slice as that would have less indirections in this case but I figured the way it is now it is easier to move it to utils in case it happens to be needed somewhere else.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, if you find the time, maybe even in the other PR, could you add a note to say where it's from?
I'd like to keep track of any code that isn't specifically written for gitoxide to be sure I can comply with licensing terms.
Thanks in advance.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah sure 👍

Copy link
Member

@Byron Byron left a comment

Choose a reason for hiding this comment

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

Thanks a lot, that's great value!

@@ -43,6 +42,29 @@ impl<'a> From<&'a WorkByEmail> for WorkByPerson {
}
}

fn join<I>(mut iter: I, sep: &str) -> String
Copy link
Member

Choose a reason for hiding this comment

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

I think it's fine to allocate here, it's similar to what was available before with std::slice::join().

- expect says what it expects to be true (but isn't).
- avoid unwraps
@Byron Byron merged commit ccb607d into GitoxideLabs:main Feb 16, 2024
3 checks passed
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