Skip to content

std.mem: add namespace for comparing sentinel-terminated pointers #24683

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

IOKG04
Copy link
Contributor

@IOKG04 IOKG04 commented Aug 3, 2025

This adds a new namespace to std.mem for 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() and max() functions.
len() and span() link to the implementation currently in std.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.mem do currently.

I tested the speed of eql() and eqlSlice(). Both are faster than using std.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 to std.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.

IOKG04 added 5 commits August 2, 2025 00:47
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
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.

1 participant