3 ms·
Nice article! Also, this might be a stupid question (I'm a Zig newbie) but… instead of calling std.mem.eql() in the while loop to look at each potential match
by codethief 1y ago
Nice article!
Also, this might be a stupid question (I'm a Zig newbie) but… instead of calling std.mem.eql() in the while loop to look at each potential match individually, couldn't you repeat the same trick as before? That is, use SIMD to search for the second and second-to-last character of the needle, then third and third-to-last, and so on, and finally take a bitwise AND of all the resulting bit masks? This way, one would avoid looking at each potential match one by one, and instead look at all of them at the same time.
Even if that doesn't work for some reason and you still need to loop over all potential matches individually, couldn't you use SIMD inside the while loop to replace std.mem.eql and thereby speed up string comparison? My understanding was that std.mem.eql loops over bytes one by one and compares them?
- ncruces 1y agoKnowing little about zig, std.mem.eql very likely already uses SIMD. This is about using SIMD to avoid even calling std.mem.eql for 99% of the possible attempts.
- llimllib 1y agostd.mem.eql is here, super easy to read: https://github.com/ziglang/zig/blob/master/lib/std/mem.zig#L702-L704 https://github.com/ziglang/zig/blob/master/lib/std/mem.zig#L... My read is it would use SIMD if T is @Vector, and not otherwise? But I'm neither a zig nor SIMD expert
- ozgrakkurt 1y agoPretty sure that compiles into assembly for any primitive type like integers, floats etc.
- IshKebab 1y agoWhat do you mean it "compiles into assembly"?
- ozgrakkurt 1y agoI meant to write SIMD