Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into simd-digit-parsing
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/parser.rs
  • Loading branch information
fabi321 committed Jun 19, 2023
2 parents cc229f5 + a0d86d0 commit 76c495b
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 8 deletions.
48 changes: 48 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ vncserver = { version ="0.2", optional = true}
[dev-dependencies]
criterion = {version = "0.5", features = ["async_tokio"]}
rstest = "0.17"
rand = "0.8.5"

[features]
default = ["vnc"]
Expand Down
29 changes: 25 additions & 4 deletions benches/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use breakwater::{
use criterion::{
BenchmarkId, Criterion, {criterion_group, criterion_main},
};
use rand::seq::SliceRandom;
use rand::thread_rng;
use std::{sync::Arc, time::Duration};

const FRAMEBUFFER_WIDTH: usize = 1920;
Expand All @@ -21,15 +23,34 @@ async fn invoke_parse_pixelflut_commands(
}

fn from_elem(c: &mut Criterion) {
let draw_commands = get_commands_to_draw_rect(FRAMEBUFFER_WIDTH, FRAMEBUFFER_HEIGHT, 0x123456);
let draw_commands = draw_commands.as_bytes();
let mut draw_commands =
get_commands_to_draw_rect(FRAMEBUFFER_WIDTH, FRAMEBUFFER_HEIGHT, 0x123456);
let ordered_draw_commands = draw_commands.join("");

c.bench_with_input(
BenchmarkId::new(
"parse_draw_commands",
"parse_draw_commands_ordered",
format!("{FRAMEBUFFER_WIDTH} x {FRAMEBUFFER_HEIGHT}"),
),
&draw_commands,
&ordered_draw_commands.as_bytes(),
|b, input| {
let fb = Arc::new(FrameBuffer::new(FRAMEBUFFER_WIDTH, FRAMEBUFFER_HEIGHT));
let parser_state = ParserState::default();
b.to_async(tokio::runtime::Runtime::new().unwrap())
.iter(|| invoke_parse_pixelflut_commands(input, &fb, parser_state.clone()));
},
);

let mut rng = thread_rng();
draw_commands.shuffle(&mut rng);
let shuffled_draw_commands = draw_commands.join("");

c.bench_with_input(
BenchmarkId::new(
"parse_draw_commands_shuffled",
format!("{FRAMEBUFFER_WIDTH} x {FRAMEBUFFER_HEIGHT}"),
),
&shuffled_draw_commands.as_bytes(),
|b, input| {
let fb = Arc::new(FrameBuffer::new(FRAMEBUFFER_WIDTH, FRAMEBUFFER_HEIGHT));
let parser_state = ParserState::default();
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
let args = Args::parse();

breakwater::parser::check_cpu_support();

let fb = Arc::new(FrameBuffer::new(args.width, args.height));

// If we make the channel to big, stats will start to lag behind
Expand Down
21 changes: 20 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::framebuffer::FrameBuffer;
use const_format::formatcp;
use std::simd::{u32x4, u32x8, Simd, SimdPartialOrd, SimdUint, ToBitMask, u8x16};
use std::simd::{u32x8, Simd, SimdUint};
use std::sync::Arc;
use tokio::io::AsyncWriteExt;

Expand Down Expand Up @@ -299,6 +299,25 @@ fn simd_unhex(value: &[u8]) -> u32 {
shifted.reduce_or()
}

pub fn check_cpu_support() {
#[cfg(target_arch = "x86_64")]
{
if !is_x86_feature_detected!("avx2") {
warn!("Your CPU does not support AVX2. Consider using a CPU supporting AVX2 for best performance");
} else if !is_x86_feature_detected!("avx") {
warn!("Your CPU does not support AVX. Consider using a CPU supporting AVX2 (or at least AVX) for best performance");
} else {
// At this point the CPU should support AVX und AVX2
// Warn the user when he has compiled breakwater without the needed target features
if cfg!(all(target_feature = "avx", target_feature = "avx2")) {
info!("Using AVX and AVX2 support");
} else {
warn!("Your CPU does support AVX and AVX2, but you have not enabled avx and avx2 support. Please re-compile using RUSTFLAGS='-C target-cpu=native' cargo build --release`");
}
}
}
}

const SIMD_POS: Simd<u8, 16> = u8x16::from_array([
255, 251, 251, 251, // interesting data
254, 251, 251, 251, // just zero em all
Expand Down
6 changes: 3 additions & 3 deletions src/test/helpers/pixelflut_commands.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pub fn get_commands_to_draw_rect(width: usize, height: usize, color: u32) -> String {
let mut draw_commands = String::new();
pub fn get_commands_to_draw_rect(width: usize, height: usize, color: u32) -> Vec<String> {
let mut draw_commands = Vec::new();

for x in 0..width {
for y in 0..height {
draw_commands += &format!("PX {x} {y} {color:06x}\n");
draw_commands.push(format!("PX {x} {y} {color:06x}\n"));
}
}

Expand Down

0 comments on commit 76c495b

Please sign in to comment.