From e6e8cacab904f10d6e5a773867826570783d0bf4 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 19 Jun 2023 12:05:40 -0700 Subject: [PATCH] std.mem.order: use for loop syntax Not only does it look nicer, it's a performance enhancement for debug builds since the safety checks are eliminated. --- lib/std/mem.zig | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/std/mem.zig b/lib/std/mem.zig index 23e24b0c09..b320f579c6 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -597,9 +597,8 @@ pub fn sortUnstableContext(a: usize, b: usize, context: anytype) void { /// Compares two slices of numbers lexicographically. O(n). pub fn order(comptime T: type, lhs: []const T, rhs: []const T) math.Order { const n = @min(lhs.len, rhs.len); - var i: usize = 0; - while (i < n) : (i += 1) { - switch (math.order(lhs[i], rhs[i])) { + for (lhs[0..n], rhs[0..n]) |lhs_elem, rhs_elem| { + switch (math.order(lhs_elem, rhs_elem)) { .eq => continue, .lt => return .lt, .gt => return .gt,