This repository has been archived by the owner on Nov 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
First part of multi-mining support #804
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
// Copyright 2015, 2016 Ethcore (UK) Ltd. | ||
// This file is part of Parity. | ||
|
||
// Parity is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Parity is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Parity. If not, see <http://www.gnu.org/licenses/>. | ||
//! Queue-like datastructure including notion of usage. | ||
|
||
/// Special queue-like datastructure that includes the notion of | ||
/// usage to avoid items that were queued but never used from making it into | ||
/// the queue. | ||
pub struct UsingQueue<T> where T: Clone { | ||
/// Not yet being sealed by a miner, but if one asks for work, we'd prefer they do this. | ||
pending: Option<T>, | ||
/// Currently being sealed by miners. | ||
in_use: Vec<T>, | ||
/// The maximum allowable number of items in_use. | ||
max_size: usize, | ||
} | ||
|
||
impl<T> UsingQueue<T> where T: Clone { | ||
/// Create a new struct with a maximum size of `max_size`. | ||
pub fn new(max_size: usize) -> UsingQueue<T> { | ||
UsingQueue { | ||
pending: None, | ||
in_use: vec![], | ||
max_size: max_size, | ||
} | ||
} | ||
|
||
/// Return a reference to the item at the top of the queue (or `None` if the queue is empty); | ||
/// it doesn't constitute noting that the item is used. | ||
pub fn peek_last_ref(&self) -> Option<&T> { | ||
self.pending.as_ref().or(self.in_use.last()) | ||
} | ||
|
||
/// Return a reference to the item at the top of the queue (or `None` if the queue is empty); | ||
/// this constitutes using the item and will remain in the queue for at least another | ||
/// `max_size` invocations of `push()`. | ||
pub fn use_last_ref(&mut self) -> Option<&T> { | ||
if let Some(x) = self.pending.take() { | ||
self.in_use.push(x); | ||
if self.in_use.len() > self.max_size { | ||
self.in_use.remove(0); | ||
} | ||
} | ||
self.in_use.last() | ||
} | ||
|
||
/// Place an item on the end of the queue. The previously `push()`ed item will be removed | ||
/// if `use_last_ref()` since it was `push()`ed. | ||
pub fn push(&mut self, b: T) { | ||
self.pending = Some(b); | ||
} | ||
|
||
/// Clears everything; the queue is entirely reset. | ||
pub fn reset(&mut self) { | ||
self.pending = None; | ||
self.in_use.clear(); | ||
} | ||
|
||
/// Returns `Some` reference to first block that `f` returns `true` with it as a parameter | ||
/// or `None` if no such block exists in the queue. | ||
pub fn find_if<P>(&self, predicate: P) -> Option<&T> where P: Fn(&T) -> bool { | ||
if self.pending.as_ref().map(|r| predicate(r)).unwrap_or(false) { | ||
self.pending.as_ref() | ||
} else { | ||
self.in_use.iter().find(|r| predicate(r)) | ||
} | ||
} | ||
|
||
/// Returns the most recently pushed block if `f` returns `true` with a reference to it as | ||
/// a parameter, otherwise `None`. | ||
/// Will not destroy a block if a reference to it has previously been returned by `use_last_ref`, | ||
/// but rather clone it. | ||
pub fn pop_if<P>(&mut self, predicate: P) -> Option<T> where P: Fn(&T) -> bool { | ||
// a bit clumsy - TODO: think about a nicer way of expressing this. | ||
if let Some(x) = self.pending.take() { | ||
if predicate(&x) { | ||
Some(x) | ||
} else { | ||
self.pending = Some(x); | ||
None | ||
} | ||
} else { | ||
self.in_use.last().into_iter().filter(|x| predicate(x)).next().cloned() | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn should_find_when_pushed() { | ||
let mut q = UsingQueue::new(2); | ||
q.push(1); | ||
assert!(q.find_if(|i| i == &1).is_some()); | ||
} | ||
|
||
#[test] | ||
fn should_find_when_pushed_and_used() { | ||
let mut q = UsingQueue::new(2); | ||
q.push(1); | ||
q.use_last_ref(); | ||
assert!(q.find_if(|i| i == &1).is_some()); | ||
} | ||
|
||
#[test] | ||
fn should_find_when_others_used() { | ||
let mut q = UsingQueue::new(2); | ||
q.push(1); | ||
q.use_last_ref(); | ||
q.push(2); | ||
q.use_last_ref(); | ||
assert!(q.find_if(|i| i == &1).is_some()); | ||
} | ||
|
||
#[test] | ||
fn should_not_find_when_too_many_used() { | ||
let mut q = UsingQueue::new(1); | ||
q.push(1); | ||
q.use_last_ref(); | ||
q.push(2); | ||
q.use_last_ref(); | ||
assert!(q.find_if(|i| i == &1).is_none()); | ||
} | ||
|
||
#[test] | ||
fn should_not_find_when_not_used_and_then_pushed() { | ||
let mut q = UsingQueue::new(3); | ||
q.push(1); | ||
q.push(2); | ||
q.use_last_ref(); | ||
assert!(q.find_if(|i| i == &1).is_none()); | ||
} | ||
|
||
#[test] | ||
fn should_peek_correctly_after_push() { | ||
let mut q = UsingQueue::new(3); | ||
q.push(1); | ||
assert_eq!(q.peek_last_ref(), Some(&1)); | ||
q.push(2); | ||
assert_eq!(q.peek_last_ref(), Some(&2)); | ||
} | ||
|
||
#[test] | ||
fn should_inspect_correctly() { | ||
let mut q = UsingQueue::new(3); | ||
q.push(1); | ||
assert_eq!(q.use_last_ref(), Some(&1)); | ||
assert_eq!(q.peek_last_ref(), Some(&1)); | ||
q.push(2); | ||
assert_eq!(q.use_last_ref(), Some(&2)); | ||
assert_eq!(q.peek_last_ref(), Some(&2)); | ||
} | ||
|
||
#[test] | ||
fn should_not_find_when_not_used_peeked_and_then_pushed() { | ||
let mut q = UsingQueue::new(3); | ||
q.push(1); | ||
q.peek_last_ref(); | ||
q.push(2); | ||
q.use_last_ref(); | ||
assert!(q.find_if(|i| i == &1).is_none()); | ||
} | ||
|
||
#[test] | ||
fn should_pop_used() { | ||
let mut q = UsingQueue::new(3); | ||
q.push(1); | ||
q.use_last_ref(); | ||
let popped = q.pop_if(|i| i == &1); | ||
assert_eq!(popped, Some(1)); | ||
} | ||
|
||
#[test] | ||
fn should_pop_unused() { | ||
let mut q = UsingQueue::new(3); | ||
q.push(1); | ||
assert_eq!(q.pop_if(|i| i == &1), Some(1)); | ||
assert_eq!(q.pop_if(|i| i == &1), None); | ||
} | ||
|
||
#[test] | ||
fn should_not_pop_unused_before_used() { | ||
let mut q = UsingQueue::new(3); | ||
q.push(1); | ||
q.push(2); | ||
let popped = q.pop_if(|i| i == &1); | ||
assert_eq!(popped, None); | ||
} | ||
|
||
#[test] | ||
fn should_not_remove_used_popped() { | ||
let mut q = UsingQueue::new(3); | ||
q.push(1); | ||
q.use_last_ref(); | ||
assert_eq!(q.pop_if(|i| i == &1), Some(1)); | ||
assert_eq!(q.pop_if(|i| i == &1), Some(1)); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Functions accepting predicates (below) are always returning only single value. So I guess it's possible to just fetch using some key.
LinkedHashMap
would be perfect here.Something like this?
https://contain-rs.github.io/linked-hash-map/linked_hash_map/struct.LinkedHashMap.html
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.
might be a sensible optimisation to do later, yeah.