[Proposal] std/mem: add a more generic eql function#7848
Closed
ifreund wants to merge 1 commit intoziglang:masterfrom
Closed
[Proposal] std/mem: add a more generic eql function#7848ifreund wants to merge 1 commit intoziglang:masterfrom
ifreund wants to merge 1 commit intoziglang:masterfrom
Conversation
c3ef60c to
6df91f4
Compare
Member
Author
|
One other feature we should consider here is easy and efficient comparison of sentinel terminated pointers and slices. The current pattern is to use for example: mem.eql(u8, my_slice, mem.span(my_sentinel_terminated_ptr));However this is inefficient as the full length of the sentinel terminated pointer is always iterated even when the slice is shorter. Ideally the comparison would look something like this: fn better(a: []const u8, b: [*:0]const u8) bool {
for (a) |a_elem, idx| {
if (b[idx] == 0 or b[idx] != a_elem) return false;
}
return b[a.len] == 0;
}Comparison in godbolt |
Member
|
Closing old drafts; please feel free to re-open if you decide to pick this back up. |
Member
|
I see this was intended to be a proposal; filed #8414 on your behalf |
IOKG04
added a commit
to IOKG04/zig
that referenced
this pull request
Aug 3, 2025
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)
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.
I had to write some very ugly code recently to compare two optional, sentinel-terminated pointers in river: https://github.com/ifreund/river/blob/cd005e15f8bcb6852b35fd3d89248e72611a5d5f/river/Option.zig#L87-L90
As I wrote in the comment, this made me feel like we needed a function to do a better job of this in
std.mem. I've Implemented such a function in this patch as a proposal. I'm not sure if this function should replace the currentstd.mem.eql()as that would be a widely breaking change and theanytypebased API is not as simple when working with slices. However, this would match the API ofstd.meta.eql()so perhaps simply replacingstd.mem.eql()is worth considering.The new function is temporarily named
eqlGeneric(), but if we decide not to replace currentmem.eql()the new function should have a better name. Naming suggestions welcome.Edit:
std.meta.deepEql()or similar would also be a reasonable place to put this function.