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

Make Router faster to clone #1123

Merged
merged 5 commits into from Jun 27, 2022
Merged

Make Router faster to clone #1123

merged 5 commits into from Jun 27, 2022

Conversation

davidpdrsn
Copy link
Member

Motivation

Router is cloned once for every request in IntoMakeService::call so ideally it should be cheap to do so.

Solution

Wrap the Node in a Arc and rebuild it when calling Router::router. This is fine since it only happens during startup.

I did a simple benchmark where I cloned a Router with 1000 routes and this brought the time down from 176.916µs to 92.698µs. So almost 50% 📈

I also attempted to Arc the other fields but since they're !Sync the Arc becomes !Send which is a no-go. Using SyncWrapper doesn't help as that requires exclusive access which we cannot get through an Arc.

@davidpdrsn davidpdrsn enabled auto-merge (squash) June 26, 2022 21:05
axum/src/routing/mod.rs Outdated Show resolved Hide resolved
davidpdrsn and others added 2 commits June 27, 2022 10:11
Co-authored-by: Jonas Platte <jplatte+git@posteo.de>
axum/src/routing/mod.rs Outdated Show resolved Hide resolved
Co-authored-by: Jonas Platte <jplatte+git@posteo.de>
@davidpdrsn davidpdrsn disabled auto-merge June 27, 2022 09:00
@davidpdrsn davidpdrsn merged commit cb20747 into main Jun 27, 2022
@davidpdrsn davidpdrsn deleted the faster-router-clone branch June 27, 2022 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants