mirror of
https://github.com/mimblewimble/grin.git
synced 2025-02-02 01:11:09 +03:00
Merge pull request #2418 from cyclefortytwo/worker-str-alloc
Reduce number of String allocation in Stratum worker
This commit is contained in:
commit
74422efa5b
1 changed files with 8 additions and 9 deletions
|
@ -168,12 +168,11 @@ impl Worker {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get Message from the worker
|
// Get Message from the worker
|
||||||
fn read_message(&mut self) -> Option<String> {
|
fn read_message(&mut self, line: &mut String) -> Option<usize> {
|
||||||
// Read and return a single message or None
|
// Read and return a single message or None
|
||||||
let mut line = String::new();
|
match self.stream.read_line(line) {
|
||||||
match self.stream.read_line(&mut line) {
|
Ok(n) => {
|
||||||
Ok(_) => {
|
return Some(n);
|
||||||
return Some(line);
|
|
||||||
}
|
}
|
||||||
Err(ref e) if e.kind() == ErrorKind::WouldBlock => {
|
Err(ref e) if e.kind() == ErrorKind::WouldBlock => {
|
||||||
// Not an error, just no messages ready
|
// Not an error, just no messages ready
|
||||||
|
@ -191,9 +190,8 @@ impl Worker {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send Message to the worker
|
// Send Message to the worker
|
||||||
fn write_message(&mut self, message_in: String) {
|
fn write_message(&mut self, mut message: String) {
|
||||||
// Write and Flush the message
|
// Write and Flush the message
|
||||||
let mut message = message_in.clone();
|
|
||||||
if !message.ends_with("\n") {
|
if !message.ends_with("\n") {
|
||||||
message += "\n";
|
message += "\n";
|
||||||
}
|
}
|
||||||
|
@ -283,9 +281,10 @@ impl StratumServer {
|
||||||
// Handle an RPC request message from the worker(s)
|
// Handle an RPC request message from the worker(s)
|
||||||
fn handle_rpc_requests(&mut self, stratum_stats: &mut Arc<RwLock<StratumStats>>) {
|
fn handle_rpc_requests(&mut self, stratum_stats: &mut Arc<RwLock<StratumStats>>) {
|
||||||
let mut workers_l = self.workers.lock();
|
let mut workers_l = self.workers.lock();
|
||||||
|
let mut the_message = String::with_capacity(4096);
|
||||||
for num in 0..workers_l.len() {
|
for num in 0..workers_l.len() {
|
||||||
match workers_l[num].read_message() {
|
match workers_l[num].read_message(&mut the_message) {
|
||||||
Some(the_message) => {
|
Some(_) => {
|
||||||
// Decompose the request from the JSONRpc wrapper
|
// Decompose the request from the JSONRpc wrapper
|
||||||
let request: RpcRequest = match serde_json::from_str(&the_message) {
|
let request: RpcRequest = match serde_json::from_str(&the_message) {
|
||||||
Ok(request) => request,
|
Ok(request) => request,
|
||||||
|
|
Loading…
Reference in a new issue