feat: improve perf 4x by not incrementing the atomic counter every time
This commit is contained in:
parent
b352b0dc10
commit
bed946b4a0
1 changed files with 38 additions and 23 deletions
47
src/main.rs
47
src/main.rs
|
|
@ -62,8 +62,8 @@ fn get_pixel(
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn increment_counter() {
|
fn increment_counter(amount: u64) {
|
||||||
COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
COUNTER.fetch_add(amount, std::sync::atomic::Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
|
|
@ -156,6 +156,7 @@ where
|
||||||
writer: BufWriter<W>,
|
writer: BufWriter<W>,
|
||||||
grids: Arc<[Flut<u32>]>,
|
grids: Arc<[Flut<u32>]>,
|
||||||
parser: ParserTypes,
|
parser: ParserTypes,
|
||||||
|
counter: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R, W> FlutClient<R, W>
|
impl<R, W> FlutClient<R, W>
|
||||||
|
|
@ -205,7 +206,7 @@ where
|
||||||
Color::W8(white) => u32::from_be_bytes([*white, *white, *white, 0xff]),
|
Color::W8(white) => u32::from_be_bytes([*white, *white, *white, 0xff]),
|
||||||
};
|
};
|
||||||
set_pixel_rgba(self.grids.as_ref(), canvas, x, y, c);
|
set_pixel_rgba(self.grids.as_ref(), canvas, x, y, c);
|
||||||
increment_counter();
|
self.counter += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn change_canvas_command(&mut self, canvas: Canvas) -> io::Result<()> {
|
fn change_canvas_command(&mut self, canvas: Canvas) -> io::Result<()> {
|
||||||
|
|
@ -225,25 +226,24 @@ where
|
||||||
writer: BufWriter::new(writer),
|
writer: BufWriter::new(writer),
|
||||||
grids,
|
grids,
|
||||||
parser: ParserTypes::TextParser(TextParser::new(0)),
|
parser: ParserTypes::TextParser(TextParser::new(0)),
|
||||||
|
counter: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn process_socket(&mut self) -> io::Result<()> {
|
async fn parse_command(&mut self, command: Result<Command, std::io::Error>) -> io::Result<()> {
|
||||||
loop {
|
match command {
|
||||||
let parsed = match &self.parser {
|
Ok(Command::Help) => self.help_command().await,
|
||||||
ParserTypes::TextParser(parser) => parser.parse(&mut self.reader).await,
|
Ok(Command::Size(canvas)) => self.size_command(canvas).await,
|
||||||
ParserTypes::BinaryParser(parser) => parser.parse(&mut self.reader).await,
|
Ok(Command::GetPixel(canvas, x, y)) => self.get_pixel_command(canvas, x, y).await,
|
||||||
};
|
|
||||||
|
|
||||||
match parsed {
|
|
||||||
Ok(Command::Help) => self.help_command().await?,
|
|
||||||
Ok(Command::Size(canvas)) => self.size_command(canvas).await?,
|
|
||||||
Ok(Command::GetPixel(canvas, x, y)) => self.get_pixel_command(canvas, x, y).await?,
|
|
||||||
Ok(Command::SetPixel(canvas, x, y, color)) => {
|
Ok(Command::SetPixel(canvas, x, y, color)) => {
|
||||||
self.set_pixel_command(canvas, x, y, &color);
|
self.set_pixel_command(canvas, x, y, &color);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Ok(Command::ChangeCanvas(canvas)) => self.change_canvas_command(canvas),
|
||||||
|
Ok(Command::ChangeProtocol(protocol)) => {
|
||||||
|
self.change_protocol(&protocol);
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
Ok(Command::ChangeCanvas(canvas)) => self.change_canvas_command(canvas)?,
|
|
||||||
Ok(Command::ChangeProtocol(protocol)) => self.change_protocol(&protocol),
|
|
||||||
|
|
||||||
Err(err) if err.kind() == ErrorKind::UnexpectedEof => {
|
Err(err) if err.kind() == ErrorKind::UnexpectedEof => {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
|
@ -253,6 +253,21 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn process_socket(&mut self) -> io::Result<()> {
|
||||||
|
loop {
|
||||||
|
for _ in 0..1000 {
|
||||||
|
let parsed = match &self.parser {
|
||||||
|
ParserTypes::TextParser(parser) => parser.parse(&mut self.reader).await,
|
||||||
|
ParserTypes::BinaryParser(parser) => parser.parse(&mut self.reader).await,
|
||||||
|
};
|
||||||
|
|
||||||
|
self.parse_command(parsed).await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
increment_counter(self.counter);
|
||||||
|
self.counter = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue