Skip to content
This repository was archived by the owner on Feb 14, 2025. It is now read-only.

Commit ac4f1ad

Browse files
committed
tried parallel memcpy, didnt' see much
1 parent 2f87be8 commit ac4f1ad

File tree

12 files changed

+146
-47
lines changed

12 files changed

+146
-47
lines changed

gojo/bufio/scan.mojo

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,11 @@ struct Scanner[R: io.Reader]():
286286
# The function is never called with an empty data slice unless at_eof
287287
# is True. If at_eof is True, however, data may be non-empty and,
288288
# as always, holds unprocessed text.
289-
alias SplitFunction = fn (data: List[Byte], at_eof: Bool) -> (Int, List[Byte], Error)
289+
alias SplitFunction = fn (data: List[Byte], at_eof: Bool) -> (
290+
Int,
291+
List[Byte],
292+
Error,
293+
)
290294

291295
# # Errors returned by Scanner.
292296
alias ERR_TOO_LONG = Error("bufio.Scanner: token too long")

gojo/builtins/errors.mojo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from sys import exit
22

3+
34
fn panic[T: Stringable](message: T, code: Int = 1):
45
"""Panics the program with the given message and exit code.
56

gojo/bytes/buffer.mojo

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import ..io
22
from ..builtins import cap, copy, Byte, panic, index_byte
3-
from algorithm.memory import parallel_memcpy
43

54

65
alias Rune = Int32
@@ -56,7 +55,7 @@ struct Buffer(
5655
fn __init__(inout self):
5756
self.capacity = 4096
5857
self.size = 0
59-
self.data = DTypePointer[DType.uint8]().alloc(4096)
58+
self.data = DTypePointer[DType.uint8]().alloc(self.capacity)
6059
self.offset = 0
6160
self.last_read = OP_INVALID
6261

@@ -89,7 +88,11 @@ struct Buffer(
8988
"""Returns a list of bytes holding a copy of the unread portion of the buffer."""
9089
var copy = UnsafePointer[UInt8]().alloc(self.size)
9190
memcpy(copy, self.data.offset(self.offset), self.size)
92-
return List[UInt8](unsafe_pointer=copy, size=self.size - self.offset, capacity=self.size - self.offset)
91+
return List[UInt8](
92+
unsafe_pointer=copy,
93+
size=self.size - self.offset,
94+
capacity=self.size - self.offset,
95+
)
9396

9497
@always_inline
9598
fn _resize(inout self, capacity: Int) -> None:
@@ -100,7 +103,6 @@ struct Buffer(
100103
capacity: The new capacity of the string builder buffer.
101104
"""
102105
var new_data = UnsafePointer[UInt8]().alloc(capacity)
103-
# parallel_memcpy(new_data, self.data, self.size, 1, 5)
104106
memcpy(new_data, self.data, self.size)
105107
self.data.free()
106108
self.data = new_data
@@ -131,7 +133,9 @@ struct Buffer(
131133
return StringRef(copy, self.size)
132134

133135
@always_inline
134-
fn render(self: Reference[Self]) -> StringSlice[self.is_mutable, self.lifetime]:
136+
fn render(
137+
self: Reference[Self],
138+
) -> StringSlice[self.is_mutable, self.lifetime]:
135139
"""
136140
Return a StringSlice view of the data owned by the builder.
137141
Slightly faster than __str__, 10-20% faster in limited testing.
@@ -237,7 +241,11 @@ struct Buffer(
237241

238242
# Copy the data of the internal buffer from offset to len(buf) into the destination buffer at the given index.
239243
var bytes_read = copy(
240-
target=dest, source=self.data, source_start=self.offset, source_end=self.size, target_start=len(dest)
244+
target=dest,
245+
source=self.data,
246+
source_start=self.offset,
247+
source_end=self.size,
248+
target_start=len(dest),
241249
)
242250
self.offset += bytes_read
243251

@@ -324,7 +332,11 @@ struct Buffer(
324332

325333
var copy = UnsafePointer[UInt8]().alloc(end - self.offset)
326334
memcpy(copy, self.data.offset(self.offset), end - self.offset)
327-
var line = List[Byte](unsafe_pointer=copy, size=end - self.offset, capacity=end - self.offset)
335+
var line = List[Byte](
336+
unsafe_pointer=copy,
337+
size=end - self.offset,
338+
capacity=end - self.offset,
339+
)
328340
self.offset = end
329341
self.last_read = OP_READ
330342

@@ -529,7 +541,8 @@ struct LegacyBuffer(
529541
fn try_grow_by_reslice(inout self, n: Int) -> (Int, Bool):
530542
"""Inlineable version of grow for the fast-case where the
531543
internal buffer only needs to be resliced.
532-
It returns the index where bytes should be written and whether it succeeded."""
544+
It returns the index where bytes should be written and whether it succeeded.
545+
"""
533546
var buffer_already_used = len(self.buf)
534547

535548
if n <= self.buf.capacity - buffer_already_used:

gojo/fmt/fmt.mojo

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,24 @@ fn find_first_verb(s: String, verbs: List[String]) -> String:
7777
return verb
7878

7979

80-
alias BASE10_TO_BASE16 = List[String]("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f")
80+
alias BASE10_TO_BASE16 = List[String](
81+
"0",
82+
"1",
83+
"2",
84+
"3",
85+
"4",
86+
"5",
87+
"6",
88+
"7",
89+
"8",
90+
"9",
91+
"a",
92+
"b",
93+
"c",
94+
"d",
95+
"e",
96+
"f",
97+
)
8198

8299

83100
fn convert_base10_to_base16(value: Int) -> String:

gojo/net/fd.mojo

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ struct FileDescriptor(FileDescriptorBase):
4747
fn read(inout self, inout dest: List[Byte]) -> (Int, Error):
4848
"""Receive data from the file descriptor and write it to the buffer provided."""
4949
var bytes_received = recv(
50-
self.fd, DTypePointer[DType.uint8](dest.unsafe_ptr()).offset(dest.size), dest.capacity, 0
50+
self.fd,
51+
DTypePointer[DType.uint8](dest.unsafe_ptr()).offset(dest.size),
52+
dest.capacity,
53+
0,
5154
)
5255
if bytes_received == -1:
5356
return 0, Error("Failed to receive message from socket.")

gojo/net/socket.mojo

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,11 @@ struct Socket(FileDescriptorBase):
168168
"""
169169
var their_addr_ptr = UnsafePointer[sockaddr].alloc(1)
170170
var sin_size = socklen_t(sizeof[socklen_t]())
171-
var new_sockfd = accept(self.sockfd.fd, their_addr_ptr, UnsafePointer[socklen_t].address_of(sin_size))
171+
var new_sockfd = accept(
172+
self.sockfd.fd,
173+
their_addr_ptr,
174+
UnsafePointer[socklen_t].address_of(sin_size),
175+
)
172176
if new_sockfd == -1:
173177
raise Error("Failed to accept connection")
174178

@@ -302,7 +306,13 @@ struct Socket(FileDescriptorBase):
302306
"""
303307
var option_value_pointer = UnsafePointer[c_void].address_of(option_value)
304308
var option_len = sizeof[socklen_t]()
305-
var status = setsockopt(self.sockfd.fd, SOL_SOCKET, option_name, option_value_pointer, option_len)
309+
var status = setsockopt(
310+
self.sockfd.fd,
311+
SOL_SOCKET,
312+
option_name,
313+
option_value_pointer,
314+
option_len,
315+
)
306316
if status == -1:
307317
raise Error("Socket.set_sock_opt failed with status: " + str(status))
308318

gojo/strings/builder.mojo

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
from algorithm.functional import vectorize
21
import ..io
32
from ..builtins import Byte
43

54

6-
# TODO: Commented out writer until the Span trait issue is fixed. https://github.com/modularml/mojo/issues/2917
75
@value
86
struct StringBuilder[growth_factor: Float32 = 2](
97
Stringable,
108
Sized,
11-
# io.Writer,
9+
io.Writer,
1210
io.StringWriter,
11+
io.ByteWriter,
1312
):
1413
"""
1514
A string builder class that allows for efficient string management and concatenation.
@@ -25,14 +24,14 @@ struct StringBuilder[growth_factor: Float32 = 2](
2524
builder and appending the strings is not worth the performance gain.
2625
2726
Example:
28-
```
29-
from strings.builder import StringBuilder
30-
31-
var sb = StringBuilder()
32-
sb.write_string("Hello ")
33-
sb.write_string("World!")
34-
print(sb) # Hello World!
35-
```
27+
```
28+
from strings.builder import StringBuilder
29+
30+
var sb = StringBuilder()
31+
sb.write_string("Hello ")
32+
sb.write_string("World!")
33+
print(sb) # Hello World!
34+
```
3635
"""
3736

3837
var data: DTypePointer[DType.uint8]
@@ -57,7 +56,7 @@ struct StringBuilder[growth_factor: Float32 = 2](
5756
Returns the length of the string builder.
5857
5958
Returns:
60-
The length of the string builder.
59+
The length of the string builder.
6160
"""
6261
return self.size
6362

@@ -67,21 +66,23 @@ struct StringBuilder[growth_factor: Float32 = 2](
6766
Converts the string builder to a string.
6867
6968
Returns:
70-
The string representation of the string builder. Returns an empty
71-
string if the string builder is empty.
69+
The string representation of the string builder. Returns an empty
70+
string if the string builder is empty.
7271
"""
7372
var copy = DTypePointer[DType.uint8]().alloc(self.size)
7473
memcpy(copy, self.data, self.size)
7574
return StringRef(copy, self.size)
7675

7776
@always_inline
78-
fn render(self: Reference[Self]) -> StringSlice[self.is_mutable, self.lifetime]:
77+
fn render(
78+
self: Reference[Self],
79+
) -> StringSlice[self.is_mutable, self.lifetime]:
7980
"""
8081
Return a StringSlice view of the data owned by the builder.
8182
Slightly faster than __str__, 10-20% faster in limited testing.
8283
8384
Returns:
84-
The string representation of the string builder. Returns an empty string if the string builder is empty.
85+
The string representation of the string builder. Returns an empty string if the string builder is empty.
8586
"""
8687
return StringSlice[self.is_mutable, self.lifetime](unsafe_from_utf8_strref=StringRef(self[].data, self[].size))
8788

@@ -91,7 +92,7 @@ struct StringBuilder[growth_factor: Float32 = 2](
9192
Resizes the string builder buffer.
9293
9394
Args:
94-
capacity: The new capacity of the string builder buffer.
95+
capacity: The new capacity of the string builder buffer.
9596
"""
9697
var new_data = DTypePointer[DType.uint8]().alloc(capacity)
9798
memcpy(new_data, self.data, self.size)
@@ -107,7 +108,7 @@ struct StringBuilder[growth_factor: Float32 = 2](
107108
Appends a byte Span to the builder buffer.
108109
109110
Args:
110-
src: The byte array to append.
111+
src: The byte array to append.
111112
"""
112113
if len(src) > self.capacity - self.size:
113114
var new_capacity = int(self.capacity * growth_factor)
@@ -126,7 +127,7 @@ struct StringBuilder[growth_factor: Float32 = 2](
126127
Appends a byte Span to the builder buffer.
127128
128129
Args:
129-
src: The byte array to append.
130+
src: The byte array to append.
130131
"""
131132
var span = Span(src)
132133
return self._write(span)
@@ -137,6 +138,11 @@ struct StringBuilder[growth_factor: Float32 = 2](
137138
Appends a string to the builder buffer.
138139
139140
Args:
140-
src: The string to append.
141+
src: The string to append.
141142
"""
142143
return self._write(src.as_bytes_slice())
144+
145+
@always_inline
146+
fn write_byte(inout self, byte: UInt8) -> (Int, Error):
147+
var span = Span(List[UInt8](byte))
148+
return self._write(span)

gojo/strings/reader.mojo

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@ from ..builtins import Byte, copy, panic
33

44

55
@value
6-
struct Reader(Sized, io.Reader, io.ReaderAt, io.ByteReader, io.ByteScanner, io.Seeker, io.WriterTo):
6+
struct Reader(
7+
Sized,
8+
io.Reader,
9+
io.ReaderAt,
10+
io.ByteReader,
11+
io.ByteScanner,
12+
io.Seeker,
13+
io.WriterTo,
14+
):
715
"""A Reader that implements the [io.Reader], [io.ReaderAt], [io.ByteReader], [io.ByteScanner], [io.Seeker], and [io.WriterTo] traits
816
by reading from a string. The zero value for Reader operates like a Reader of an empty string.
917
"""

gojo/syscall/net.mojo

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,10 @@ fn ntohs(netshort: c_ushort) -> c_ushort:
462462

463463

464464
fn inet_ntop(
465-
af: c_int, src: DTypePointer[DType.uint8], dst: DTypePointer[DType.uint8], size: socklen_t
465+
af: c_int,
466+
src: DTypePointer[DType.uint8],
467+
dst: DTypePointer[DType.uint8],
468+
size: socklen_t,
466469
) -> DTypePointer[DType.uint8]:
467470
"""Libc POSIX `inet_ntop` function
468471
Reference: https://man7.org/linux/man-pages/man3/inet_ntop.3p.html.
@@ -600,7 +603,11 @@ fn getsockopt(
600603
](socket, level, option_name, option_value, option_len)
601604

602605

603-
fn getsockname(socket: c_int, address: UnsafePointer[sockaddr], address_len: UnsafePointer[socklen_t]) -> c_int:
606+
fn getsockname(
607+
socket: c_int,
608+
address: UnsafePointer[sockaddr],
609+
address_len: UnsafePointer[socklen_t],
610+
) -> c_int:
604611
"""Libc POSIX `getsockname` function
605612
Reference: https://man7.org/linux/man-pages/man3/getsockname.3p.html
606613
Fn signature: int getsockname(int socket, struct sockaddr *restrict address, socklen_t *restrict address_len).
@@ -619,7 +626,11 @@ fn getsockname(socket: c_int, address: UnsafePointer[sockaddr], address_len: Uns
619626
](socket, address, address_len)
620627

621628

622-
fn getpeername(sockfd: c_int, addr: UnsafePointer[sockaddr], address_len: UnsafePointer[socklen_t]) -> c_int:
629+
fn getpeername(
630+
sockfd: c_int,
631+
addr: UnsafePointer[sockaddr],
632+
address_len: UnsafePointer[socklen_t],
633+
) -> c_int:
623634
"""Libc POSIX `getpeername` function
624635
Reference: https://man7.org/linux/man-pages/man2/getpeername.2.html
625636
Fn signature: int getpeername(int socket, struct sockaddr *restrict addr, socklen_t *restrict address_len).
@@ -660,7 +671,11 @@ fn listen(socket: c_int, backlog: c_int) -> c_int:
660671
return external_call["listen", c_int, c_int, c_int](socket, backlog)
661672

662673

663-
fn accept(socket: c_int, address: UnsafePointer[sockaddr], address_len: UnsafePointer[socklen_t]) -> c_int:
674+
fn accept(
675+
socket: c_int,
676+
address: UnsafePointer[sockaddr],
677+
address_len: UnsafePointer[socklen_t],
678+
) -> c_int:
664679
"""Libc POSIX `accept` function
665680
Reference: https://man7.org/linux/man-pages/man3/accept.3p.html
666681
Fn signature: int accept(int socket, struct sockaddr *restrict address, socklen_t *restrict address_len).
@@ -694,7 +709,12 @@ fn connect(socket: c_int, address: UnsafePointer[sockaddr], address_len: socklen
694709
)
695710

696711

697-
fn recv(socket: c_int, buffer: DTypePointer[DType.uint8], length: c_size_t, flags: c_int) -> c_ssize_t:
712+
fn recv(
713+
socket: c_int,
714+
buffer: DTypePointer[DType.uint8],
715+
length: c_size_t,
716+
flags: c_int,
717+
) -> c_ssize_t:
698718
"""Libc POSIX `recv` function
699719
Reference: https://man7.org/linux/man-pages/man3/recv.3p.html
700720
Fn signature: ssize_t recv(int socket, void *buffer, size_t length, int flags).
@@ -709,7 +729,12 @@ fn recv(socket: c_int, buffer: DTypePointer[DType.uint8], length: c_size_t, flag
709729
](socket, buffer, length, flags)
710730

711731

712-
fn send(socket: c_int, buffer: DTypePointer[DType.uint8], length: c_size_t, flags: c_int) -> c_ssize_t:
732+
fn send(
733+
socket: c_int,
734+
buffer: DTypePointer[DType.uint8],
735+
length: c_size_t,
736+
flags: c_int,
737+
) -> c_ssize_t:
713738
"""Libc POSIX `send` function
714739
Reference: https://man7.org/linux/man-pages/man3/send.3p.html
715740
Fn signature: ssize_t send(int socket, const void *buffer, size_t length, int flags).

gojo/unicode/utf8/string.mojo

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ struct UnicodeString(Stringable, Sized):
7878
return len(self.inner)
7979

8080
@always_inline
81-
fn __iter__(self: Reference[Self]) -> _StringIter[self.is_mutable, self.lifetime]:
81+
fn __iter__(
82+
self: Reference[Self],
83+
) -> _StringIter[self.is_mutable, self.lifetime]:
8284
return _StringIter(self[].inner)
8385

8486

0 commit comments

Comments
 (0)