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

Refactor to use more idiomatic Rust patterns #14

Merged
merged 7 commits into from
Jul 6, 2023
Merged

Refactor to use more idiomatic Rust patterns #14

merged 7 commits into from
Jul 6, 2023

Conversation

Yossipossi1
Copy link
Contributor

I thought I'd help contribute to the crate some more, since I'm using it for an application and it doesn't seem to get frequent contributions despite its high quality and utility.

This PR helps to refactor some code to use more idiomatic Rust patterns. Some of these are more opinionated than others, so feel free to comment on any you don't think are appropriate:

  • Restructures some control flow to remove unnecessary else statements or check negations.
  • Removes wildcards that match to one statement (which helps reduce some cognitive overhead).
  • Changes a series of if statements on the same variable to a matches! statement, which is cleaner and slightly improves speed.
  • Changes an instance of unwrap_or() to unwrap_or_else() (which doesn't compute the enclosed value unless needed).
  • Removes an unnecessary closure.
  • Removes an unnecessary binding.
  • Removes a "where" statement that can be written more concisely.
  • Adds underscores to masks to improve readability.

Additionally, I do have some further questions/comments about the crate that I couldn't address myself:

  • The use of unsafe methods in this crate are quite masterful, though as good practice it would be nice to comment the invariants that are assumed for each block. I couldn't figure out what some of the unsafe blocks were doing (particularly in regards to the pointer arithmetic), so I wasn't able to do this myself.
    • As an aside, the optimizations in the unsafe blocks do improve the speed, but on the scale of around 20-70 ms per 10,000,000 iterations (which I don't think this crate will be dealing with often) from what I've been able to test. While normally I'd go with the safe option to avoid invariants in the code, I assume the purpose of these unsafe blocks were to optimize for speed as much as possible, so I haven't suggested removing them despite there also being fast safe alternatives for many operations.
  • The into_str() method in the module string seems to have a few peculiarities:
    • Despite being named into_str(), it returns a String. Since str is its own data type, this could lead to some confusion.
    • This method uses std::mem::transmute() to transform a CuredString(String) into just a String. This method could instead just return the inner value by doing self.0. From what I can gather, any speed difference (if it exists) is negligible. The only drawback to this is that the function would not be const anymore, though I'm not sure when this would be used in const contexts as cure is not const.
  • It could be worthwhile to create some benchmarks to test future code performance changes, as right now my benchmarks are more ad hoc and not fit for production. If you'd like, I could try looking into this to add it to the crate.

I appreciate all the work you've done with this crate, and I hope that these contributions are helpful!

@null8626
Copy link
Owner

null8626 commented Jul 6, 2023

Thank you soo much for this pull request, i appreciate all of your changes and i'll take your suggestions and concerns into consideration for the next release ❤️

@null8626
Copy link
Owner

null8626 commented Jul 6, 2023

If you want to, you can give your concerns in the Issues tab if you like. Thanks!

@null8626 null8626 merged commit 8b88b82 into null8626:main Jul 6, 2023
@Yossipossi1
Copy link
Contributor Author

Of course! Happy to help out! 🙂

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.

None yet

2 participants