From 909f3d76d87d924fe059cea7e5987e40ea35ec3c Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Fri, 12 May 2017 17:25:02 +0200 Subject: [PATCH] optimize back-reference filling --- ethcore/light/src/on_demand/mod.rs | 4 +++- ethcore/light/src/types/request/builder.rs | 13 +++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/ethcore/light/src/on_demand/mod.rs b/ethcore/light/src/on_demand/mod.rs index 78767f015..35aa8c590 100644 --- a/ethcore/light/src/on_demand/mod.rs +++ b/ethcore/light/src/on_demand/mod.rs @@ -481,7 +481,7 @@ impl Handler for OnDemand { // for each incoming response // 1. ensure verification data filled. (still TODO since on_demand doesn't use back-references yet) // 2. pending.requests.supply_response - // 3. if extracted on-demand response + // 3. if extracted on-demand response, keep it for later. for response in responses { match pending.requests.supply_response(&*self.cache, response) { Ok(response) => { @@ -497,12 +497,14 @@ impl Handler for OnDemand { } } + pending.requests.fill_unanswered(); if pending.requests.is_complete() { let _ = pending.sender.send(pending.responses); return; } + // update network requests (unless we're done, in which case fulfill the future.) let mut builder = basic_request::RequestBuilder::default(); let num_answered = pending.requests.num_answered(); diff --git a/ethcore/light/src/types/request/builder.rs b/ethcore/light/src/types/request/builder.rs index 27875dc7b..dff33513a 100644 --- a/ethcore/light/src/types/request/builder.rs +++ b/ethcore/light/src/types/request/builder.rs @@ -115,6 +115,15 @@ impl Requests { .expect("All outputs checked as invariant of `Requests` object; qed")) } } + + /// Sweep through all unanswered requests, filling them as necessary. + pub fn fill_unanswered(&mut self) { + let outputs = &mut self.outputs; + + for req in self.requests.iter_mut().skip(self.answered) { + req.fill(|req_idx, out_idx| outputs.get(&(req_idx, out_idx)).cloned().ok_or(NoSuchOutput)) + } + } } impl Requests { @@ -141,8 +150,8 @@ impl Requests { self.answered += 1; - // fill as much of each remaining request as we can. - for req in self.requests.iter_mut().skip(self.answered) { + // fill as much of the next request as we can. + if let Some(ref mut req) = self.requests.get_mut(self.answered) { req.fill(|req_idx, out_idx| outputs.get(&(req_idx, out_idx)).cloned().ok_or(NoSuchOutput)) }