From 1401fe46b0d3ef6b4a3358db012f89e61238dcc7 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Tue, 10 Dec 2024 20:06:43 -0300 Subject: [PATCH 1/8] inline unnecessary helper functions for List Signed-off-by: martinvuyk --- stdlib/src/collections/list.mojo | 40 +++++++------------------- stdlib/test/collections/test_list.mojo | 2 +- stdlib/test/python/my_module.py | 3 +- 3 files changed, 14 insertions(+), 31 deletions(-) diff --git a/stdlib/src/collections/list.mojo b/stdlib/src/collections/list.mojo index bcfca0c2fa..1373318ebc 100644 --- a/stdlib/src/collections/list.mojo +++ b/stdlib/src/collections/list.mojo @@ -470,22 +470,23 @@ struct List[T: CollectionElement, hint_trivial_type: Bool = False]( # Methods # ===-------------------------------------------------------------------===# - fn bytecount(self) -> Int: - """Gets the bytecount of the List. + fn byte_length(self) -> Int: + """Gets the byte length of the List. Returns: - The bytecount of the List. + The byte length of the List. """ return len(self) * sizeof[T]() fn _realloc(mut self, new_capacity: Int): var new_data = UnsafePointer[T].alloc(new_capacity) - _move_pointee_into_many_elements[hint_trivial_type]( - dest=new_data, - src=self.data, - size=self.size, - ) + @parameter + if hint_trivial_type: + memcpy(new_data, self.data, self.byte_length()) + else: + for i in range(size): + (src + i).move_pointee_into(dest + i) if self.data: self.data.free() @@ -737,8 +738,8 @@ struct List[T: CollectionElement, hint_trivial_type: Bool = False]( if stop_normalized < 0: stop_normalized += len(self) - start_normalized = _clip(start_normalized, 0, len(self)) - stop_normalized = _clip(stop_normalized, 0, len(self)) + start_normalized = max(start_normalized, min(0, len(self))) + stop_normalized = max(stop_normalized, min(0, len(self))) for i in range(start_normalized, stop_normalized): if self[i] == value: @@ -932,22 +933,3 @@ struct List[T: CollectionElement, hint_trivial_type: Bool = False]( The UnsafePointer to the underlying memory. """ return self.data - - -fn _clip(value: Int, start: Int, end: Int) -> Int: - return max(start, min(value, end)) - - -fn _move_pointee_into_many_elements[ - T: CollectionElement, //, hint_trivial_type: Bool -](dest: UnsafePointer[T], src: UnsafePointer[T], size: Int): - @parameter - if hint_trivial_type: - memcpy( - dest=dest.bitcast[Int8](), - src=src.bitcast[Int8](), - count=size * sizeof[T](), - ) - else: - for i in range(size): - (src + i).move_pointee_into(dest + i) diff --git a/stdlib/test/collections/test_list.mojo b/stdlib/test/collections/test_list.mojo index 56dab6510b..826a72a814 100644 --- a/stdlib/test/collections/test_list.mojo +++ b/stdlib/test/collections/test_list.mojo @@ -39,7 +39,7 @@ def test_list(): list.append(i) assert_equal(5, len(list)) - assert_equal(5 * sizeof[Int](), list.bytecount()) + assert_equal(5 * sizeof[Int](), list.byte_length()) assert_equal(0, list[0]) assert_equal(1, list[1]) assert_equal(2, list[2]) diff --git a/stdlib/test/python/my_module.py b/stdlib/test/python/my_module.py index 8147b0a382..c78c39556e 100644 --- a/stdlib/test/python/my_module.py +++ b/stdlib/test/python/my_module.py @@ -25,7 +25,8 @@ def __init__(self, bar): class AbstractPerson(ABC): @abstractmethod - def method(self): ... + def method(self): + ... def my_function(name): From a3d27fe37346cb35ae70e36fae61719746bfc83d Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Tue, 10 Dec 2024 20:10:48 -0300 Subject: [PATCH 2/8] fix detail Signed-off-by: martinvuyk --- stdlib/src/collections/list.mojo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/src/collections/list.mojo b/stdlib/src/collections/list.mojo index 1373318ebc..d2fe1a6496 100644 --- a/stdlib/src/collections/list.mojo +++ b/stdlib/src/collections/list.mojo @@ -485,7 +485,7 @@ struct List[T: CollectionElement, hint_trivial_type: Bool = False]( if hint_trivial_type: memcpy(new_data, self.data, self.byte_length()) else: - for i in range(size): + for i in range(len(self)): (src + i).move_pointee_into(dest + i) if self.data: From 7893ca351fa8045841531d492da3f587ba6515e0 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Tue, 10 Dec 2024 20:11:05 -0300 Subject: [PATCH 3/8] fix detail Signed-off-by: martinvuyk --- stdlib/src/collections/list.mojo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/src/collections/list.mojo b/stdlib/src/collections/list.mojo index d2fe1a6496..20131e8044 100644 --- a/stdlib/src/collections/list.mojo +++ b/stdlib/src/collections/list.mojo @@ -486,7 +486,7 @@ struct List[T: CollectionElement, hint_trivial_type: Bool = False]( memcpy(new_data, self.data, self.byte_length()) else: for i in range(len(self)): - (src + i).move_pointee_into(dest + i) + (self.data + i).move_pointee_into(dest + i) if self.data: self.data.free() From da50923fb079fd4896121f13bbc926f09cded562 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Tue, 10 Dec 2024 20:12:27 -0300 Subject: [PATCH 4/8] fix detail Signed-off-by: martinvuyk --- stdlib/src/collections/list.mojo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/src/collections/list.mojo b/stdlib/src/collections/list.mojo index 20131e8044..232e31f305 100644 --- a/stdlib/src/collections/list.mojo +++ b/stdlib/src/collections/list.mojo @@ -486,7 +486,7 @@ struct List[T: CollectionElement, hint_trivial_type: Bool = False]( memcpy(new_data, self.data, self.byte_length()) else: for i in range(len(self)): - (self.data + i).move_pointee_into(dest + i) + (self.data + i).move_pointee_into(new_data + i) if self.data: self.data.free() From 3095ae6a07c1550b60277ed599a2d6593687a211 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Tue, 10 Dec 2024 20:47:54 -0300 Subject: [PATCH 5/8] fix detail Signed-off-by: martinvuyk --- stdlib/src/collections/list.mojo | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/src/collections/list.mojo b/stdlib/src/collections/list.mojo index 232e31f305..777a42173b 100644 --- a/stdlib/src/collections/list.mojo +++ b/stdlib/src/collections/list.mojo @@ -738,8 +738,8 @@ struct List[T: CollectionElement, hint_trivial_type: Bool = False]( if stop_normalized < 0: stop_normalized += len(self) - start_normalized = max(start_normalized, min(0, len(self))) - stop_normalized = max(stop_normalized, min(0, len(self))) + start_normalized = max(0, min(start_normalized, len(self))) + stop_normalized = max(0, min(stop_normalized, len(self))) for i in range(start_normalized, stop_normalized): if self[i] == value: From 0da6049f8953c52f361858ce9d0487ac2b2eab92 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Tue, 10 Dec 2024 21:09:16 -0300 Subject: [PATCH 6/8] fix detail Signed-off-by: martinvuyk --- stdlib/src/collections/list.mojo | 15 ++++++++------- stdlib/src/memory/memory.mojo | 3 +-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/stdlib/src/collections/list.mojo b/stdlib/src/collections/list.mojo index 777a42173b..8b7ba22a52 100644 --- a/stdlib/src/collections/list.mojo +++ b/stdlib/src/collections/list.mojo @@ -483,7 +483,7 @@ struct List[T: CollectionElement, hint_trivial_type: Bool = False]( @parameter if hint_trivial_type: - memcpy(new_data, self.data, self.byte_length()) + memcpy(new_data, self.data, len(self)) else: for i in range(len(self)): (self.data + i).move_pointee_into(new_data + i) @@ -724,22 +724,23 @@ struct List[T: CollectionElement, hint_trivial_type: Bool = False]( Raises: ValueError: If the value is not found in the list. """ - var start_normalized = start + var s_len = len(self) + var start_normalized = start var stop_normalized: Int if stop is None: # Default end - stop_normalized = len(self) + stop_normalized = s_len else: stop_normalized = stop.value() if start_normalized < 0: - start_normalized += len(self) + start_normalized += s_len if stop_normalized < 0: - stop_normalized += len(self) + stop_normalized += s_len - start_normalized = max(0, min(start_normalized, len(self))) - stop_normalized = max(0, min(stop_normalized, len(self))) + start_normalized = max(0, min(start_normalized, s_len)) + stop_normalized = max(0, min(stop_normalized, s_len)) for i in range(start_normalized, stop_normalized): if self[i] == value: diff --git a/stdlib/src/memory/memory.mojo b/stdlib/src/memory/memory.mojo index 8d3a525cb4..2639835437 100644 --- a/stdlib/src/memory/memory.mojo +++ b/stdlib/src/memory/memory.mojo @@ -251,11 +251,10 @@ fn memcpy[ src: The source pointer. count: The number of elements to copy. """ - var n = count * sizeof[dest.type]() _memcpy_impl( dest.bitcast[Byte, origin=MutableAnyOrigin](), src.bitcast[Byte, origin=MutableAnyOrigin](), - n, + count * sizeof[T](), ) From 4a9d4daf5ac4b19a9bc317aec4955e39f758305a Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Wed, 18 Dec 2024 21:14:18 -0300 Subject: [PATCH 7/8] remove bytecount changes Signed-off-by: martinvuyk --- stdlib/src/collections/list.mojo | 6 +++--- stdlib/test/collections/test_list.mojo | 2 +- stdlib/test/python/my_module.py | 3 +-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/stdlib/src/collections/list.mojo b/stdlib/src/collections/list.mojo index f7a9c73af9..c22a251d32 100644 --- a/stdlib/src/collections/list.mojo +++ b/stdlib/src/collections/list.mojo @@ -469,11 +469,11 @@ struct List[T: CollectionElement, hint_trivial_type: Bool = False]( # Methods # ===-------------------------------------------------------------------===# - fn byte_length(self) -> Int: - """Gets the byte length of the List. + fn bytecount(self) -> Int: + """Gets the bytecount of the List. Returns: - The byte length of the List. + The bytecount of the List. """ return len(self) * sizeof[T]() diff --git a/stdlib/test/collections/test_list.mojo b/stdlib/test/collections/test_list.mojo index 826a72a814..56dab6510b 100644 --- a/stdlib/test/collections/test_list.mojo +++ b/stdlib/test/collections/test_list.mojo @@ -39,7 +39,7 @@ def test_list(): list.append(i) assert_equal(5, len(list)) - assert_equal(5 * sizeof[Int](), list.byte_length()) + assert_equal(5 * sizeof[Int](), list.bytecount()) assert_equal(0, list[0]) assert_equal(1, list[1]) assert_equal(2, list[2]) diff --git a/stdlib/test/python/my_module.py b/stdlib/test/python/my_module.py index c78c39556e..8147b0a382 100644 --- a/stdlib/test/python/my_module.py +++ b/stdlib/test/python/my_module.py @@ -25,8 +25,7 @@ def __init__(self, bar): class AbstractPerson(ABC): @abstractmethod - def method(self): - ... + def method(self): ... def my_function(name): From 36abc30446d86a195c8f016bd9a4c36ee20ccb7d Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Sat, 1 Feb 2025 12:28:43 -0300 Subject: [PATCH 8/8] mojo format Signed-off-by: martinvuyk --- stdlib/src/collections/list.mojo | 1 - 1 file changed, 1 deletion(-) diff --git a/stdlib/src/collections/list.mojo b/stdlib/src/collections/list.mojo index a0bcb965e2..28025a5ba3 100644 --- a/stdlib/src/collections/list.mojo +++ b/stdlib/src/collections/list.mojo @@ -1076,4 +1076,3 @@ struct List[T: CollectionElement, hint_trivial_type: Bool = False]( return List[T, hint_trivial_type]( ptr=data, length=size, capacity=capacity ) -