-
Notifications
You must be signed in to change notification settings - Fork 35
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
Refactor header parsing and data structure #61
Conversation
@bgreni this is very impressive!! Love to see how the ~940 line header implementation is condense to ~80 lines. And the code now feels much more pythonic, too. Definitely onboard with moving towards a more "idiomatic Mojo" approach, gojo was helpful to move fast but ideally I'd like to gradually refactor the whole codebase to a more Mojo-native code style. Although how "idiomatic" is defined in Mojo is still uncertain -- but I think there stdlib code and the Python ecosystem could be a good reference. Also good point about the importance of control; I'm a bit surprised by the performance increase though, intuitively I'd expect a Dict to be slower than directly accessing a struct. Do you think the performance gain is due to switching to a different Reader implementation, or am I missing something? |
Yeah the idiomatic ways of doing things in Mojo is unfortunately still evolving, an example being that using
I think it comes down to skipping the work of actually parsing the fields, the original code does a lot string comparisons whereas now we simply split on the colon char and dump the key/value into a dict. And note in this case we're measuring in nanoseconds so the difference isn't that big, but it could be meaningful for server-side performance. |
c00d7fa
to
4ed5ec9
Compare
@saviorand I've just pushed some new changes, let me know what you think!
I've done away with most of the I haven't touch |
@bgreni I added both the Re: the string vs bytes, I understand there's a lot of work in the stdlib happening around string optimizations, curious what the performance difference is for the changes you made compared to previous implementation. Previously in an end-to-end benchmark with |
4602255
to
4cea92b
Compare
@saviorand Cool thanks for bringing Using
EDIT:
|
With these changes they don't build anymore so in that case maybe we have to remove them entirely? |
4cea92b
to
4f87531
Compare
@bgreni sure, I can remove the Python stuff, will push some changes here tomorrow.
Could be a good baseline to compare against . Or we can just try running the same against main, |
@saviorand Ah looks like part of the problem was I didn't notice the default
|
4f87531
to
70a7307
Compare
@bgreni that's great! Marked increase to the previous performance 👏 👏 |
@saviorand Ok awesome, I'll do some housekeeping (proper comments and such) and undraft the PR when it's ready |
a21430f
to
97666f3
Compare
…e more Pythonic Signed-off-by: Brian Grenier <grenierb96@gmail.com>
97666f3
to
cf22edc
Compare
|
@bgreni everything looks great, I can feel the quality of this codebase improving with every commit you're making ! Looks like you were faster than me with removing the Python Client/Server, thanks for that. |
var value = r.read_line() | ||
self._inner[to_string(key^).lower()] = to_string(value^) | ||
|
||
return (to_string(first^), to_string(second^), to_string(third^)) |
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.
Here I think I caught a bug when I uncommented test_client.mojo
- it hangs indefinitely when trying to return this tuple. I'm not 100% sure but my first guess is it's related to a bug in Mojo I was experiencing before where returning tuples hang and the program never exits.
We could file an issue in stdlib and for now replace this return with something else? What do you think
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.
There was an error in the creation of the HTTPRequest in that test which Seemingly caused httpbin to return nothing, which broke the parsing and caused an infinite loop, so still a bug that should be addressed at some point
Thank you for indulging me on this refactor, I'm glad you like the result! |
@bgreni pushed one more fix for the |
Merged! Huge thanks again for the contribution 🔥 |
@saviorand I figured I might post a draft PR for this so I can see what you think about my approach before I get too much deeper into applying the changes and doing more thorough unit testing
Main points to review:
RequestHeader
andResponseHeader
with a commonHeaders
structHeaders
is essentially aDict
wrapper with parsing logic includedHTTPRequest
andHTTPResponse
Reader
with an internal implementation. I have opinions about the usage of gojo in general in terms of mixing Go style semantics into a Mojo codebase, but also for something as performance sensitive as request parsing we probably want full control of it to make specific optimizations later?Obviously this isn't done yet so it doesn't mean much but so far some light benchmarking I've done shows significant performance improvements
Fixes: #44