std.mem: add namespace for comparing sentinel-terminated pointers#24683
Closed
IOKG04 wants to merge 5 commits intoziglang:masterfrom
Closed
std.mem: add namespace for comparing sentinel-terminated pointers#24683IOKG04 wants to merge 5 commits intoziglang:masterfrom
std.mem: add namespace for comparing sentinel-terminated pointers#24683IOKG04 wants to merge 5 commits intoziglang:masterfrom
Conversation
probably still gonna change the naming. TODO: more functions, maybe link to `std.mem.len()` from there I am happy to report though, that I tested the speed of these, even if in a very bad way, but still conclusive enough, and the versions I wrote/stole from ifreund are faster :3 note that I didn't test the `allEquals()` function, but I'll just assume it's faster too.. ziglang#7848 (comment)
also i tested the `eql` and `eqlSlice` functions for speed, and.. its all definitely faster than using `span()`, but the unexpected result is that if you have a sentinel-terminated slice, it is faster to compare the pointers, than it is to do the slice (at least for small (`< 32` or so) slice size).
just to ensure i didn't turn the wrong `0` into a `sentinel` in `order()`
closes ziglang#22618 (from where i stole the code) i don't think this syntax will work with pull requests, but i can try at least :3
found some cases where these functions are useful the test finished with some errors on my system, but it always does, even if a commit is confirmed to work, probably just something wrong on my end. the compiler still builds tho and test-std also finishes cleanly, so ill just hope there isn't too much wrong
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds a new namespace to
std.memfor comparing sentinel-terminated pointers:std.mem.sentinel_terminated.This includes both a function for comparing two such pointers and a function for comparing one to a slice (latter addresses #7848 (comment)).
Ordering is also provided, which also works if the sentinel is not the minimal possible value in the slice (copied from and closes #22618).
I also included
allEqual(),min()andmax()functions.len()andspan()link to the implementation currently instd.mem, though if this get accepted, moving the implementations and deprecating the original functions might be a possibility.The naming I chose isn't final, if a better one is suggested, I can change it.
Similarly, I am not quite sure on the explicitness of the sentinel value in the function calls, though it does follow a similar pattern as many functions in
std.memdo currently.I tested the speed of
eql()andeqlSlice(). Both are faster than usingstd.mem.eql()with sentinel-terminated pointer to slice conversion, especially at smaller lengths.If operating on sentinel-terminated slices it is actually sometimes faster to use
std.mem.sentinel_terminated.eql()on the pointers than it is to pass the slices tostd.mem.eql(), though more testing should be done before this is used.Lastly, if there are any more functions I should add, or any usecase in the standard library or the compiler where these functions could help, tell me and I will implement them to the best of my ability.