From 3ef5b80d2c359c94ec2fa14bde492a6c9774d536 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Sat, 13 Jan 2024 18:43:50 +0100 Subject: [PATCH] std: use simple eqlBytes for spirv The SPIR-V backend doesn't support the advanced eqlBytes yet, and when it does, it likely that it will be detrimental. --- lib/std/mem.zig | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/std/mem.zig b/lib/std/mem.zig index f15773170a170af6ea11f1db8997739537f5aac2..55c51d595985cf5ea817f2468ca1d6172e3e1c7a 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -632,10 +632,16 @@ test "lessThan" { try testing.expect(lessThan(u8, "", "a")); } +const backend_can_use_eql_bytes = switch (builtin.zig_backend) { + // The SPIR-V backend does not support the optimized path yet. + .stage2_spirv64 => false, + else => true, +}; + /// Compares two slices and returns whether they are equal. pub fn eql(comptime T: type, a: []const T, b: []const T) bool { if (@sizeOf(T) == 0) return true; - if (!@inComptime() and std.meta.hasUniqueRepresentation(T)) return eqlBytes(sliceAsBytes(a), sliceAsBytes(b)); + if (!@inComptime() and std.meta.hasUniqueRepresentation(T) and backend_can_use_eql_bytes) return eqlBytes(sliceAsBytes(a), sliceAsBytes(b)); if (a.len != b.len) return false; if (a.len == 0 or a.ptr == b.ptr) return true; @@ -648,6 +654,10 @@ pub fn eql(comptime T: type, a: []const T, b: []const T) bool { /// std.mem.eql heavily optimized for slices of bytes. fn eqlBytes(a: []const u8, b: []const u8) bool { + if (!backend_can_use_eql_bytes) { + return eql(u8, a, b); + } + if (a.len != b.len) return false; if (a.len == 0 or a.ptr == b.ptr) return true; -- 2.54.0