Skip to content

Commit

Permalink
Add List[Scalar[D]] append SIMD and Span[Scalar[D]]
Browse files Browse the repository at this point in the history
Signed-off-by: martinvuyk <martin.vuyklop@gmail.com>
  • Loading branch information
martinvuyk committed Dec 10, 2024
1 parent 2ace785 commit 1f05a6b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 51 deletions.
28 changes: 2 additions & 26 deletions stdlib/src/base64/_b64encode.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -195,21 +195,6 @@ fn load_incomplete_simd[
return result


fn store_incomplete_simd[
simd_width: Int
](
pointer: UnsafePointer[UInt8],
owned simd_vector: SIMD[DType.uint8, simd_width],
nb_of_elements_to_store: Int,
):
var tmp_buffer_pointer = UnsafePointer.address_of(simd_vector).bitcast[
UInt8
]()

memcpy(dest=pointer, src=tmp_buffer_pointer, count=nb_of_elements_to_store)
_ = simd_vector # We make it live long enough


# TODO: Use Span instead of List as input when Span is easier to use
@no_inline
fn b64encode_with_buffers(
Expand All @@ -229,11 +214,7 @@ fn b64encode_with_buffers(

var input_vector = start_of_input_chunk.load[width=simd_width]()

result_vector = _to_b64_ascii(input_vector)

(result.unsafe_ptr() + len(result)).store(result_vector)

result.size += simd_width
result.append(_to_b64_ascii(input_vector))
input_index += input_simd_width

# We handle the last 0, 1 or 2 chunks
Expand Down Expand Up @@ -268,12 +249,7 @@ fn b64encode_with_buffers(
](
nb_of_elements_to_load
)
store_incomplete_simd(
result.unsafe_ptr() + len(result),
result_vector_with_equals,
nb_of_elements_to_store,
)
result.size += nb_of_elements_to_store
result.append(result_vector_with_equals, nb_of_elements_to_store)
input_index += input_simd_width


Expand Down
61 changes: 56 additions & 5 deletions stdlib/src/collections/list.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -493,15 +493,66 @@ struct List[T: CollectionElement, hint_trivial_type: Bool = False](
self.capacity = new_capacity

fn append(mut self, owned value: T):
"""Appends a value to this list.
"""Appends a value to this list. If there is no capacity left, resizes
to twice the current capacity. Except for 0 capacity where it sets 1.
Args:
value: The value to append.
"""
if self.size >= self.capacity:
self._realloc(max(1, self.capacity * 2))
(self.data + self.size).init_pointee_move(value^)
self.size += 1
if len(self) >= self.capacity:
self._realloc(self.capacity * 2 + int(self.capacity == 0))
(self.data + len(self)).init_pointee_move(value^)
self._len += 1

fn append[
D: DType, //
](mut self: List[Scalar[D], *_, **_], value: SIMD[D, _]):
"""Appends a vector to this list. If there is no capacity left, resizes
to `len(self) + value.size`.
Parameters:
D: The DType.
Args:
value: The value to append.
"""
self.reserve(len(self) + value.size)
(self.data + len(self)).store(value)
self._len += value.size

fn append[
D: DType, //
](mut self: List[Scalar[D], *_, **_], value: SIMD[D, _], count: Int):
"""Appends a vector to this list. If there is no capacity left, resizes
to `len(self) + count`.
Parameters:
D: The DType.
Args:
value: The value to append.
count: The ammount of items to append.
"""
self.reserve(len(self) + count)
var v_ptr = UnsafePointer.address_of(value).bitcast[Scalar[D]]()
memcpy(self.data + len(self), v_ptr, count)
self._len += count

fn append[
D: DType, //
](mut self: List[Scalar[D], *_, **_], value: Span[Scalar[D]]):
"""Appends a Span to this list. If there is no capacity left, resizes
to `len(self) + len(value)`.
Parameters:
D: The DType.
Args:
value: The value to append.
"""
self.reserve(len(self) + len(value))
memcpy(self.data + len(self), value.unsafe_ptr(), len(value))
self._len += len(value)

fn insert(mut self, i: Int, owned value: T):
"""Inserts a value to the list at the given index.
Expand Down
25 changes: 6 additions & 19 deletions stdlib/src/utils/inline_string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -147,28 +147,15 @@ struct InlineString(Sized, Stringable, CollectionElement, CollectionElementNew):
# Begin by heap allocating enough space to store the combined
# string.
var buffer = List[UInt8](capacity=total_len)

# Copy the bytes from the current small string layout
memcpy(
dest=buffer.unsafe_ptr(),
src=self._storage[_FixedString[Self.SMALL_CAP]].unsafe_ptr(),
count=len(self),
var span_self = Span[Byte, __origin_of(self)](
ptr=self._storage[_FixedString[Self.SMALL_CAP]].unsafe_ptr(),
length=len(self),
)

buffer.append(span_self)
# Copy the bytes from the additional string.
memcpy(
dest=buffer.unsafe_ptr() + len(self),
src=str_slice.unsafe_ptr(),
count=str_slice.byte_length(),
)

# Record that we've initialized `total_len` count of elements
# in `buffer`
buffer.size = total_len

# Add the NUL byte
buffer.append(0)

buffer.append(str_slice.as_bytes())
buffer.append(0) # Add the NUL byte
self._storage = Self.Layout(String(buffer^))

fn __add__(self, other: StringLiteral) -> Self:
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

0 comments on commit 1f05a6b

Please sign in to comment.