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

Various improvements around the index method #318

Merged
merged 1 commit into from
Nov 25, 2022

Conversation

AntoineRR
Copy link
Collaborator

Description

Hello, I'm back with a new suggestion for the Rust code of Robyn.
I tried to simplify the code around the index function in server.rs:

  • Removed unnecessary Rc<RefCell<>>
  • Removed some clone() calls (this is mostly possible due to the use of the to_object method, which takes a reference to the object, instead of the into_py method which takes ownership of the object)
  • Simplified some chunks of code

Let me know if this is ok for you 🙂

@netlify
Copy link

netlify bot commented Nov 21, 2022

Deploy Preview for robyn canceled.

Name Link
🔨 Latest commit 1a58f19
🔍 Latest deploy log https://app.netlify.com/sites/robyn/deploys/637bfc699c810c0009321dbb

@sansyrox
Copy link
Member

Thank you for the PR @AntoineRR 😄

Looks great at the first glance. I will give it a thorough review by tomorrow evening. 😄

Comment on lines +60 to +61
request.insert("params", route_params.to_object(py));
request.insert("queries", queries.to_object(py));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AntoineRR , I have one question - What benefit does to_object bring over into_py?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the docs of pyo3 here:
ToPyObject is a conversion trait that allows various objects to be converted into PyObject. IntoPy serves the same purpose, except that it consumes self.

I used to_object because it doesn't consume self. This means I was able to use references instead of taking ownership of the object, and I didn't need to clone the object to use it twice in the code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, alrighty. Thank you! :D

I have a feeling that this will result in some performance increase!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may lead to performance increase, but I am not sure about it because the Rust compiler is smart enough to remove unnecessary clone calls most of the time ^^
Btw how do you test the performances of Robyn ? For testing the Rust code it is possible to write benchmarks using the criterion crate, I could try to do it if you are interested :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AntoineRR , I just compile it and run it through oha. (https://github.com/hatoo/oha)

I had a look at criterion before but was unable to figure out how would I bench the requests. What approach would you take for this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I used oha to run some performance tests too, and saw no difference on my machine sadly :( But I think it still makes the code more readable ^^
To benchmark with criterion, we would have to first add routes to the routers, then generate a few requests we should test, and I guess we could benchmark the index method synchronously by sending all the requests to it? If there is enough variations in the requests it would be representative of the average speed of the Rust core of Robyn.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AntoineRR , that sounds complicated. What do you think about python/bash script that runs a dummy app and runs oha on it.

Your approach will give us more granular results, but then I have a question how much of it can we actually control? As we use pyo3 and then the next steps would be writing unsafe CFFI ourselves.

Let me know what you think of it. And feel free to correct me if I am wrong or very wrong 😆

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a python or bash script to run performance tests on Robyn as a whole would be a good idea I think, it would let us easily check if a change in the code has an impact on performances!

I don't think it would require that much work to write a benchmark with criterion, but my pyo3 knowledge is quite limited for now. I may try it and submit a PR if I find it useful enough tho ^^

For now a script running oha is surely good enough, and easier to setup! I can open an issue for this if you are interested

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now a script running oha is surely good enough, and easier to setup! I can open an issue for this if you are interested

Yes, please. Thank you! :D

I don't think it would require that much work to write a benchmark with criterion, but my pyo3 knowledge is quite limited for now. I may try it and submit a PR if I find it useful enough tho ^^

Perfect. Do lmk if I can be of any help here. :D

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I'll let you know if I got something to work with criterion ;)
Here is the issue for a follow up on the oha benchmark: #320

Copy link
Member

@sansyrox sansyrox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Great work! :D

@sansyrox sansyrox merged commit cc21a3a into sparckles:main Nov 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants