bat icon indicating copy to clipboard operation
bat copied to clipboard

glob is called twice

Open rtczza opened this issue 1 year ago • 2 comments
trafficstars

In this context, matcher.glob() already returns a Cow<'a, str> type, so there is no need to call the glob method again. The glob method has been mistakenly called twice in a row here. The correct code should directly convert the Cow<'a, str> to a String type and then push it into Vec<String>. The to_string() method can be used here (which can accept Cow<'a, str> and clone or borrow as needed) to complete the conversion.

The above is my own understanding, not necessarily correct, welcome to discuss.

rtczza avatar Apr 08 '24 02:04 rtczza

Thank you for your contribution!

From my understanding of the original code, matcher is a GlobMatcher from the globset crate, and the first glob() function returns a reference to the Glob struct which created the matcher. The second glob() function returns an &str to the original glob pattern, and into() calls to_owned() on that.

I have concerns that replacing it with matcher.glob().to_string() would be a tiny performance regression. Glob does not implement the ToString trait directly, which means that to_string() is provided by a blanket implementation for the Display trait. The blanket implementation has to go through Display::fmt, which has more overhead than calling to_owned() on &str.

I also noticed that this adds a new syntax as well as your changes to get_syntax_mapping_to_paths, so you might want to rebase it to remove that.

eth-p avatar Apr 08 '24 19:04 eth-p

has been rebased and this submission has been removed.

rtczza avatar Apr 09 '24 03:04 rtczza