From b4517461cfa04e6dc1bd72ab81c1b474ea2ffacc Mon Sep 17 00:00:00 2001 From: Linus Heckemann Date: Mon, 21 Aug 2017 22:59:46 +0100 Subject: [PATCH] Avoid sorting routes on every add. --- lib/src/router/mod.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/src/router/mod.rs b/lib/src/router/mod.rs index 7b3700ce..324aa24d 100644 --- a/lib/src/router/mod.rs +++ b/lib/src/router/mod.rs @@ -25,10 +25,8 @@ impl Router { pub fn add(&mut self, route: Route) { let selector = route.method; let entries = self.routes.entry(selector).or_insert_with(|| vec![]); - // TODO: We really just want an insertion at the correct spot here, - // instead of pushing to the end and _then_ sorting. - entries.push(route); - entries.sort_by(|a, b| a.rank.cmp(&b.rank)); + let i = entries.binary_search_by_key(&route.rank, |r| r.rank).unwrap_or_else(|i| i); + entries.insert(i, route); } pub fn route<'b>(&'b self, req: &Request) -> Vec<&'b Route> {