Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stdlib] Inline unnecessary helper functions for List #3857

Open
wants to merge 8 commits into
base: nightly
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 16 additions & 33 deletions stdlib/src/collections/list.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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:
martinvuyk marked this conversation as resolved.
Show resolved Hide resolved
"""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, len(self))
else:
for i in range(len(self)):
(self.data + i).move_pointee_into(new_data + i)

if self.data:
self.data.free()
Expand Down Expand Up @@ -723,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 = _clip(start_normalized, 0, len(self))
stop_normalized = _clip(stop_normalized, 0, len(self))
start_normalized = max(0, min(start_normalized, s_len))
martinvuyk marked this conversation as resolved.
Show resolved Hide resolved
stop_normalized = max(0, min(stop_normalized, s_len))

for i in range(start_normalized, stop_normalized):
if self[i] == value:
Expand Down Expand Up @@ -932,22 +934,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)
3 changes: 1 addition & 2 deletions stdlib/src/memory/memory.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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](),
)


Expand Down
2 changes: 1 addition & 1 deletion stdlib/test/collections/test_list.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
3 changes: 2 additions & 1 deletion stdlib/test/python/my_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def __init__(self, bar):

class AbstractPerson(ABC):
@abstractmethod
def method(self): ...
def method(self):
...


def my_function(name):
Expand Down
Loading