From 8b2b7edb38d00769d1986347e5496c81649adb6f Mon Sep 17 00:00:00 2001 From: Seth Stadick Date: Tue, 7 Jan 2025 21:49:08 -0500 Subject: [PATCH] fix: improve perf of zero based range iterator --- stdlib/src/builtin/range.mojo | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/stdlib/src/builtin/range.mojo b/stdlib/src/builtin/range.mojo index 2af6fb942d..4244680a18 100644 --- a/stdlib/src/builtin/range.mojo +++ b/stdlib/src/builtin/range.mojo @@ -46,13 +46,13 @@ fn _sign(x: Int) -> Int: @register_passable("trivial") struct _ZeroStartingRange(Sized, ReversibleRange, _IntIterable): - var curr: Int + var start: Int var end: Int @always_inline @implicit fn __init__(out self, end: Int): - self.curr = max(0, end) + self.start = 0 self.end = self.curr @always_inline @@ -61,9 +61,9 @@ struct _ZeroStartingRange(Sized, ReversibleRange, _IntIterable): @always_inline fn __next__(mut self) -> Int: - var curr = self.curr - self.curr -= 1 - return self.end - curr + var start = self.start + self.start += 1 + return start @always_inline fn __has_next__(self) -> Bool: @@ -71,16 +71,16 @@ struct _ZeroStartingRange(Sized, ReversibleRange, _IntIterable): @always_inline fn __len__(self) -> Int: - return self.curr + return max(0, self.end - self.start) @always_inline fn __getitem__(self, idx: Int) -> Int: debug_assert(idx < self.__len__(), "index out of range") - return index(idx) + return self.start + index(idx) @always_inline fn __reversed__(self) -> _StridedRange: - return range(self.end - 1, -1, -1) + return range(self.end - 1, self.start - 1, -1) @value