-
Notifications
You must be signed in to change notification settings - Fork 44
Remove unnecessary string conversions #50
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
Conversation
| fn read(self, inout buf: Bytes) raises -> Int: | ||
| var new_buf = Pointer[UInt8]().alloc(default_buffer_size) | ||
| var new_buf = UnsafePointer[UInt8]().alloc(default_buffer_size) | ||
| var bytes_recv = recv(self.fd, new_buf, default_buffer_size, 0) | ||
| if bytes_recv == -1: | ||
| return 0 | ||
| if bytes_recv == 0: | ||
| return 0 | ||
| var bytes_str = String(new_buf.bitcast[Int8](), bytes_recv) | ||
| buf = bytes_str._buffer | ||
| var bytes_str = String(new_buf.bitcast[UInt8](), bytes_recv + 1) | ||
| buf = bytes(bytes_str, pop=False) | ||
| return bytes_recv | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not handle large payloads very well in combination with the request parsing logic in the sys/server. Needs to be refactored
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could try reading directly into the ptr owned by buf if you want to try avoiding the additional string allocation. I'm not sure how much time it would save, but that's how I've been doing it for my net file descriptor implementation. Hasn't blown up yet, might be worth a try: https://github.com/thatstoasty/gojo/blob/main/gojo/net/fd.mojo#L47
fn read(inout self, inout dest: List[Byte]) -> (Int, Error):
"""Receive data from the file descriptor and write it to the buffer provided."""
var bytes_received = recv(
self.fd,
DTypePointer[DType.uint8](dest.unsafe_ptr()).offset(dest.size),
dest.capacity - dest.size,
0,
)
if bytes_received == -1:
return 0, Error("Failed to receive message from socket.")
dest.size += bytes_received
if bytes_received < dest.capacity:
return bytes_received, Error(io.EOF)
return bytes_received, Error()There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, cool! Thanks! @alainrollejr going to try this out today
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, seems to work nicely
|
To get a feel for the problem, I basically searched for all magic number occurences equal to 4096 (there are quite a lot) and replaced them all by 1048576. Might be not all of them needed enlarging but then I have not grown with the code so hard for me to go analyse that. |
|
@alainrollejr ah, good to know, thanks. I'm refactoring more today, expecting some major improvements both in reliability in performance after it's done. Will keep you posted |
|
@alainrollejr made some really major refactoring this weekend on this branch. Below is a selection of a couple test runs I made. I don't see a steep increase anymore with the size of the packet, although as you can see overall the rate is slower now. This is with |
|
@saviorand It's cool to see you're using some of the other structs from Gojo! I'm just going through the code to see if I spot anything else that might help speed up the code.
|
No description provided.