Skip to content

remove itertools - #1294

Merged
Sebastian Thiel (Byron) merged 3 commits into
GitoxideLabs:mainfrom
benmkw:iter
Feb 16, 2024
Merged

remove itertools#1294
Sebastian Thiel (Byron) merged 3 commits into
GitoxideLabs:mainfrom
benmkw:iter

Conversation

@benmkw

Copy link
Copy Markdown
Contributor

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.

Comment thread gitoxide-core/src/hours/util.rs Outdated
.iter()
// BStr does not impl slice::Join
.map(|s| s.to_string())
.collect::<Vec<_>>()

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

}
}

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

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yeah sure 👍

@Byron Sebastian Thiel (Byron) left a comment

Copy link
Copy Markdown
Member

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!

}
}

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

Copy link
Copy Markdown
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
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